Skip to content

Commit f333fae

Browse files
committed
Merge pull request #7 from theckman/fix_api_support
Update client to support latest API behaviors / data; v0.5.0 Signed-off-by: Tim Heckman <t@heckman.io>
2 parents e73d11a + b14899b commit f333fae

8 files changed

Lines changed: 283 additions & 805 deletions

File tree

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
language: go
66
go:
77
- tip
8-
- 1.10beta1
9-
- 1.9.2
8+
- 1.11.x
9+
- 1.12.x
10+
env:
11+
- GO111MODULE=auto
1012
sudo: false
1113
notifications:
1214
email:

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
test: vet lint megacheck tests
66

77
prebuild:
8-
go get -v -u github.com/golang/dep/cmd/dep github.com/golang/lint/golint honnef.co/go/tools/cmd/megacheck
8+
go get -v -u github.com/golang/dep/cmd/dep \
9+
golang.org/x/lint/golint \
10+
honnef.co/go/tools/cmd/megacheck \
11+
golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
912

1013
vet:
11-
go vet -shadow ./...
14+
go vet ./...
15+
go vet -vettool=$(shell which shadow) ./...
1216

1317
lint:
1418
golint -set_exit_status

client.go

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import (
99
"io/ioutil"
1010
"net"
1111
"net/http"
12+
"net/url"
1213
"runtime"
1314
"time"
1415

1516
"github.com/pkg/errors"
1617
)
1718

1819
// Version is the package version
19-
const Version = "0.4.1"
20+
const Version = "0.5.0"
2021

2122
// fqpn is the Fully Qualified Package Name for use in the client's User-Agent
2223
const fqpn = "github.com/theckman/go-ipdata"
2324

2425
const (
25-
apiEndpoint = "https://api.ipdata.co/"
26-
apiAuthHeader = "api-key"
26+
apiEndpoint = "https://api.ipdata.co/"
27+
apiAuthParam = "api-key"
2728
)
2829

2930
var userAgent = fmt.Sprintf(
@@ -62,7 +63,7 @@ func (c Client) Request(ip string) (*http.Response, error) {
6263
// action request
6364
resp, err := c.c.Do(req)
6465
if err != nil {
65-
return nil, errors.Wrapf(err, "http request to %q failed", req.URL.String())
66+
return nil, errors.Wrapf(err, "http request to %q failed", req.URL.Scheme+"://"+req.URL.Host+req.URL.Path)
6667
}
6768

6869
switch resp.StatusCode {
@@ -82,7 +83,7 @@ func (c Client) Request(ip string) (*http.Response, error) {
8283

8384
if resp.StatusCode == http.StatusTooManyRequests {
8485
rerr := rateErr{r: true, m: string(body)}
85-
return nil, errors.Wrapf(rerr, "request for %q failed (ratelimited)")
86+
return nil, errors.Wrapf(rerr, "request for %q failed (ratelimited)", req.URL.String())
8687
}
8788

8889
return nil, errors.Errorf("request for %q failed: %s", ip, string(body))
@@ -92,27 +93,6 @@ func (c Client) Request(ip string) (*http.Response, error) {
9293
}
9394
}
9495

95-
// LookupRaw takes an IP address to look up the details for. An empty string
96-
// means you want the information about the current node's public IP address.
97-
//
98-
// This method is a little more performant than Lookup as it does not convert
99-
// the RawIP struct to an IP struct.
100-
func (c Client) LookupRaw(ip string) (RawIP, error) {
101-
resp, err := c.Request(ip)
102-
if err != nil {
103-
return RawIP{}, err
104-
}
105-
106-
defer resp.Body.Close()
107-
108-
rip, err := DecodeRawIP(resp.Body)
109-
if err != nil {
110-
return RawIP{}, err
111-
}
112-
113-
return rip, nil
114-
}
115-
11696
// Lookup takes an IP address to look up the details for. An empty string means
11797
// you want the information about the current node's pubilc IP address.
11898
func (c Client) Lookup(ip string) (IP, error) {
@@ -131,8 +111,8 @@ func (c Client) Lookup(ip string) (IP, error) {
131111
return pip, nil
132112
}
133113

134-
func newRequest(url, apiKey string) (*http.Request, error) {
135-
req, err := http.NewRequest(http.MethodGet, url, nil)
114+
func newRequest(urlStr, apiKey string) (*http.Request, error) {
115+
req, err := http.NewRequest(http.MethodGet, urlStr, nil)
136116
if err != nil {
137117
return nil, err
138118
}
@@ -141,7 +121,8 @@ func newRequest(url, apiKey string) (*http.Request, error) {
141121

142122
// set the api key header (if set)
143123
if len(apiKey) > 0 {
144-
req.Header.Set(apiAuthHeader, apiKey)
124+
q := url.Values{apiAuthParam: []string{apiKey}}
125+
req.URL.RawQuery = q.Encode()
145126
}
146127

147128
return req, nil

0 commit comments

Comments
 (0)