Skip to content

Commit 41b6000

Browse files
committed
Fix api key issue
1 parent 46adbc7 commit 41b6000

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

ipdata/ipdata.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"net/http"
89
"net/url"
910
"strings"
@@ -13,7 +14,7 @@ type boolean bool
1314

1415
// Client represent the wrapper for ipdata.co
1516
type Client struct {
16-
baseURL *url.URL
17+
baseURL string
1718
httpClient *http.Client
1819
UserAgent string
1920
APIKey string
@@ -93,21 +94,20 @@ func NewClient(httpClient *http.Client) (*Client, error) {
9394
httpClient = http.DefaultClient
9495
}
9596

96-
baseURL, err := url.Parse("https://api.ipdata.co")
97-
if err != nil {
98-
return nil, err
99-
}
100-
10197
client := &Client{
102-
baseURL: baseURL,
98+
baseURL: "https://api.ipdata.co",
10399
httpClient: httpClient,
104100
}
105101
return client, nil
106102
}
107103

108104
func (c *Client) newRequest(method, path string) (*http.Request, error) {
109-
rel := &url.URL{Path: path}
110-
u := c.baseURL.ResolveReference(rel)
105+
params := url.Values{}
106+
params.Add("api-key", c.APIKey)
107+
108+
u, _ := url.ParseRequestURI(c.baseURL)
109+
u.Path = path
110+
u.RawQuery = params.Encode()
111111

112112
req, err := http.NewRequest(method, u.String(), nil)
113113
if err != nil {
@@ -117,7 +117,6 @@ func (c *Client) newRequest(method, path string) (*http.Request, error) {
117117
req.Header.Set("Content-Type", "application/json")
118118
req.Header.Set("Accept", "application/json")
119119
req.Header.Set("User-Agent", c.UserAgent)
120-
req.Header.Set("Api-Key", c.APIKey)
121120

122121
return req, nil
123122
}
@@ -150,17 +149,17 @@ func (c *Client) GetIPData(ip string) (*Data, error) {
150149
resp, body, err := c.do(req, data)
151150
if err != nil || resp.StatusCode != 200 {
152151
var errorResponse = &Error{}
153-
json.Unmarshal([]byte(*body), errorResponse)
152+
_ = json.Unmarshal([]byte(*body), errorResponse)
154153

155154
switch resp.StatusCode {
156155
case 400: // Bad Request
157-
return nil, errors.New(*body)
158-
case 403:
159156
return nil, errors.New(errorResponse.Message)
157+
case 401: // Unauthorized
158+
return nil, errors.New(fmt.Sprintf("Unauthorized: %v", errorResponse.Message))
160159
case 429: // Too Many Requests
161160
return nil, errors.New("you have exceeded requests limit. See https://ipdata.co")
162161
default:
163-
return nil, err
162+
return nil, errors.New(fmt.Sprintf("Unknown Error: %v", errorResponse.Message))
164163
}
165164
}
166165
data.JSON = body

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ COPYRIGHT:
130130

131131
sort.Sort(cli.FlagsByName(app.Flags))
132132

133-
app.Run(os.Args)
133+
err := app.Run(os.Args)
134+
if err != nil {
135+
fmt.Println(err)
136+
}
134137
}
135138

136139
func getFlagRepr(data ipdata.Data) string {

0 commit comments

Comments
 (0)