Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
language: go
go:
- tip
- 1.10beta1
- 1.9.2
- 1.11.x
- 1.12.x
env:
- GO111MODULE=auto
sudo: false
notifications:
email:
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
test: vet lint megacheck tests

prebuild:
go get -v -u github.com/golang/dep/cmd/dep github.com/golang/lint/golint honnef.co/go/tools/cmd/megacheck
go get -v -u github.com/golang/dep/cmd/dep \
golang.org/x/lint/golint \
honnef.co/go/tools/cmd/megacheck \
golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow

vet:
go vet -shadow ./...
go vet ./...
go vet -vettool=$(shell which shadow) ./...

lint:
golint -set_exit_status
Expand Down
39 changes: 10 additions & 29 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"runtime"
"time"

"github.com/pkg/errors"
)

// Version is the package version
const Version = "0.4.1"
const Version = "0.5.0"

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

const (
apiEndpoint = "https://api.ipdata.co/"
apiAuthHeader = "api-key"
apiEndpoint = "https://api.ipdata.co/"
apiAuthParam = "api-key"
)

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

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

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

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

// LookupRaw takes an IP address to look up the details for. An empty string
// means you want the information about the current node's public IP address.
//
// This method is a little more performant than Lookup as it does not convert
// the RawIP struct to an IP struct.
func (c Client) LookupRaw(ip string) (RawIP, error) {
resp, err := c.Request(ip)
if err != nil {
return RawIP{}, err
}

defer resp.Body.Close()

rip, err := DecodeRawIP(resp.Body)
if err != nil {
return RawIP{}, err
}

return rip, nil
}

// Lookup takes an IP address to look up the details for. An empty string means
// you want the information about the current node's pubilc IP address.
func (c Client) Lookup(ip string) (IP, error) {
Expand All @@ -131,8 +111,8 @@ func (c Client) Lookup(ip string) (IP, error) {
return pip, nil
}

func newRequest(url, apiKey string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
func newRequest(urlStr, apiKey string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, urlStr, nil)
if err != nil {
return nil, err
}
Expand All @@ -141,7 +121,8 @@ func newRequest(url, apiKey string) (*http.Request, error) {

// set the api key header (if set)
if len(apiKey) > 0 {
req.Header.Set(apiAuthHeader, apiKey)
q := url.Values{apiAuthParam: []string{apiKey}}
req.URL.RawQuery = q.Encode()
}

return req, nil
Expand Down
Loading