-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
231 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,65 @@ | ||
// Package client is a client for downloading GeoIP2 and GeoLite2 MMDB | ||
// databases. | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// HTTPReader is a Reader that uses an HTTP client to retrieve | ||
// databases. | ||
type HTTPReader struct { | ||
// client is an http client responsible of fetching database updates. | ||
client *http.Client | ||
// path is the request path. | ||
path string | ||
// accountID is used for request auth. | ||
accountID int | ||
// licenseKey is used for request auth. | ||
// Client downloads GeoIP2 and GeoLite2 MMDB databases. | ||
// | ||
// After creation, it is valid for concurrent use. | ||
type Client struct { | ||
accountID int | ||
endpoint string | ||
httpClient *http.Client | ||
licenseKey string | ||
// verbose turns on/off debug logs. | ||
verbose bool | ||
} | ||
|
||
// NewHTTPReader creates a Reader that downloads database updates via | ||
// HTTP. | ||
func NewHTTPReader( | ||
path string, | ||
// Option is an option for configuring Client. | ||
type Option func(*Client) | ||
|
||
// WithEndpoint sets the base endpoint to use. By default we use | ||
// https://updates.maxmind.com. | ||
func WithEndpoint(endpoint string) Option { | ||
return func(c *Client) { | ||
c.endpoint = endpoint | ||
} | ||
} | ||
|
||
// WithHTTPClient sets the HTTP client to use. By default we use | ||
// http.DefaultClient. | ||
func WithHTTPClient(httpClient *http.Client) Option { | ||
return func(c *Client) { | ||
c.httpClient = httpClient | ||
} | ||
} | ||
|
||
// New creates a Client. | ||
func New( | ||
accountID int, | ||
licenseKey string, | ||
verbose bool, | ||
httpClient *http.Client, | ||
) *HTTPReader { | ||
return &HTTPReader{ | ||
client: httpClient, | ||
path: path, | ||
options ...Option, | ||
) (Client, error) { | ||
if accountID <= 0 { | ||
return Client{}, fmt.Errorf("invalid account ID: %d", accountID) | ||
} | ||
|
||
if licenseKey == "" { | ||
return Client{}, fmt.Errorf("invalid license key: %s", licenseKey) | ||
} | ||
|
||
c := Client{ | ||
accountID: accountID, | ||
endpoint: "https://updates.maxmind.com", | ||
httpClient: http.DefaultClient, | ||
licenseKey: licenseKey, | ||
verbose: verbose, | ||
} | ||
|
||
for _, opt := range options { | ||
opt(&c) | ||
} | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.