-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
127 lines (104 loc) · 2.76 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package geoip
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// Client is used to get the results from the server API
type Client struct {
url string
timeout time.Duration
lookupCache map[string]*IPLocation
}
// NewClient returns a new Client instance with the given URL
func NewClient(url string, timeout time.Duration) *Client {
return &Client{
url: url,
timeout: timeout,
lookupCache: map[string]*IPLocation{},
}
}
// ClearCache clears the cache for the lookups
func (client *Client) ClearCache() {
client.lookupCache = map[string]*IPLocation{}
}
// Lookup returns the full country information for a specific IP address
func (client *Client) Lookup(ipaddress string) (*IPLocation, error) {
type ErrorResponse struct {
Error string `json:"error"`
}
if location, cached := client.lookupCache[ipaddress]; cached {
location.IsCached = true
return location, nil
}
params := url.Values{}
params.Add("ip", ipaddress)
fullURL := client.url + "?" + params.Encode()
httpClient := &http.Client{}
httpClient.Timeout = client.timeout
resp, err := httpClient.Get(fullURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
rawData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var errResponse ErrorResponse
if err := json.Unmarshal(rawData, &errResponse); err != nil {
return nil, err
}
if errResponse.Error != "" {
return nil, errors.New(errResponse.Error)
}
location :=&IPLocation{}
if err := json.Unmarshal(rawData, location); err != nil {
return nil, err
}
location.IsCached = false
client.lookupCache[ipaddress] = location
return location, nil
}
// CountryCode returns the country code for a specific IP address
func (client *Client) CountryCode(ipaddress string) (string, error) {
location, err := client.Lookup(ipaddress)
if err != nil {
return "", err
}
return location.CountryCode(), nil
}
// CountryName returns the country name for a specific IP address
func (client *Client) CountryName(ipaddress string) (string, error) {
location, err := client.Lookup(ipaddress)
if err != nil {
return "", err
}
return location.CountryName(), nil
}
// RegionName returns the region name for a specific IP address
//
// Region can be:
// - west-us
// - south-brazil
// - japan-east
// - southeast-asia
// - west-europe (the default)
func (client *Client) RegionName(ipaddress string) (string, error) {
location, err := client.Lookup(ipaddress)
if err != nil {
return "", err
}
return location.RegionName(), nil
}
// TimeZone returns the timezone for a specific IP address
func (client *Client) TimeZone(ipaddress string) (string, error) {
location, err := client.Lookup(ipaddress)
if err != nil {
return "", err
}
return location.TimeZone(), nil
}