|
| 1 | +package info |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/netip" |
| 9 | +) |
| 10 | + |
| 11 | +func newIP2Location(client *http.Client) *ip2Location { |
| 12 | + return &ip2Location{ |
| 13 | + client: client, |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +type ip2Location struct { |
| 18 | + client *http.Client |
| 19 | +} |
| 20 | + |
| 21 | +func (p *ip2Location) get(ctx context.Context, ip netip.Addr) ( |
| 22 | + result Result, err error) { |
| 23 | + result.Source = string(Ipinfo) |
| 24 | + |
| 25 | + url := "https://api.ip2location.io/" |
| 26 | + if ip.IsValid() { |
| 27 | + url += "?ip=" + ip.String() |
| 28 | + } |
| 29 | + request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 30 | + if err != nil { |
| 31 | + return result, fmt.Errorf("creating request: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + response, err := p.client.Do(request) |
| 35 | + if err != nil { |
| 36 | + return result, fmt.Errorf("doing request: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + switch response.StatusCode { |
| 40 | + case http.StatusOK: |
| 41 | + case http.StatusForbidden, http.StatusTooManyRequests: |
| 42 | + bodyString := bodyToSingleLine(response.Body) |
| 43 | + _ = response.Body.Close() |
| 44 | + return result, fmt.Errorf("%w (%s)", ErrTooManyRequests, bodyString) |
| 45 | + default: |
| 46 | + bodyString := bodyToSingleLine(response.Body) |
| 47 | + _ = response.Body.Close() |
| 48 | + return result, fmt.Errorf("%w: %d %s (%s)", ErrBadHTTPStatus, |
| 49 | + response.StatusCode, response.Status, bodyString) |
| 50 | + } |
| 51 | + |
| 52 | + decoder := json.NewDecoder(response.Body) |
| 53 | + var data struct { |
| 54 | + IP netip.Addr `json:"ip"` |
| 55 | + RegionName string `json:"region_name"` |
| 56 | + CountryName string `json:"country_name"` |
| 57 | + CityName string `json:"city_name"` |
| 58 | + // More fields available see https://www.ip2location.io/ip2location-documentation |
| 59 | + } |
| 60 | + err = decoder.Decode(&data) |
| 61 | + if err != nil { |
| 62 | + return result, fmt.Errorf("decoding JSON response: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + result.IP = data.IP |
| 66 | + if data.RegionName != "" { |
| 67 | + result.Region = stringPtr(data.RegionName) |
| 68 | + } |
| 69 | + if data.CityName != "" { |
| 70 | + result.City = stringPtr(data.CityName) |
| 71 | + } |
| 72 | + if data.CountryName != "" { |
| 73 | + country := countryCodeToName(data.CountryName) |
| 74 | + result.Country = stringPtr(country) |
| 75 | + } |
| 76 | + |
| 77 | + return result, nil |
| 78 | +} |
0 commit comments