-
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
5 changed files
with
204 additions
and
184 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package client | ||
|
||
import ( | ||
"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. | ||
licenseKey string | ||
// verbose turns on/off debug logs. | ||
verbose bool | ||
} | ||
|
||
// NewHTTPReader creates a Reader that downloads database updates via | ||
// HTTP. | ||
func NewHTTPReader( | ||
path string, | ||
accountID int, | ||
licenseKey string, | ||
verbose bool, | ||
httpClient *http.Client, | ||
) *HTTPReader { | ||
return &HTTPReader{ | ||
client: httpClient, | ||
path: path, | ||
accountID: accountID, | ||
licenseKey: licenseKey, | ||
verbose: verbose, | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
|
||
"github.com/maxmind/geoipupdate/v6/internal" | ||
"github.com/maxmind/geoipupdate/v6/internal/vars" | ||
) | ||
|
||
const metadataEndpoint = "%s/geoip/updates/metadata?" | ||
|
||
// metadata represents the metadata content for a certain database returned by the | ||
// metadata endpoint. | ||
type metadata struct { | ||
Date string `json:"date"` | ||
EditionID string `json:"edition_id"` | ||
MD5 string `json:"md5"` | ||
} | ||
|
||
func (r *HTTPReader) getMetadata(ctx context.Context, editionID string) (*metadata, error) { | ||
params := url.Values{} | ||
params.Add("edition_id", editionID) | ||
|
||
metadataRequestURL := fmt.Sprintf(metadataEndpoint, r.path) + params.Encode() | ||
|
||
if r.verbose { | ||
log.Printf("Requesting metadata for %s: %s", editionID, metadataRequestURL) | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, metadataRequestURL, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating metadata request: %w", err) | ||
} | ||
req.Header.Add("User-Agent", "geoipupdate/"+vars.Version) | ||
req.SetBasicAuth(strconv.Itoa(r.accountID), r.licenseKey) | ||
|
||
response, err := r.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("performing metadata request: %w", err) | ||
} | ||
defer response.Body.Close() | ||
|
||
responseBody, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading metadata response body: %w", err) | ||
} | ||
|
||
if response.StatusCode != http.StatusOK { | ||
httpErr := internal.HTTPError{ | ||
Body: string(responseBody), | ||
StatusCode: response.StatusCode, | ||
} | ||
return nil, fmt.Errorf("unexpected HTTP status code: %w", httpErr) | ||
} | ||
|
||
var metadataResponse struct { | ||
Databases []metadata `json:"databases"` | ||
} | ||
|
||
if err := json.Unmarshal(responseBody, &metadataResponse); err != nil { | ||
return nil, fmt.Errorf("parsing metadata body: %w", err) | ||
} | ||
|
||
if len(metadataResponse.Databases) != 1 { | ||
return nil, fmt.Errorf("response does not contain edition %s", editionID) | ||
} | ||
|
||
edition := metadataResponse.Databases[0] | ||
|
||
return &edition, nil | ||
} |
Oops, something went wrong.