-
Notifications
You must be signed in to change notification settings - Fork 3
/
lookup.go
116 lines (92 loc) · 2.74 KB
/
lookup.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
package maclookup
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"strings"
"time"
)
//Lookup retrieve MAC information from API.
func (c Client) Lookup(mac string) (ResponseMACInfo, error) {
url := c.prefixURI + apiMAC + cleanMac(mac)
if c.apiKey != "" {
url += apiKeyParam + c.apiKey
}
return c.getMacInfo(url)
}
func (c Client) getMacInfo(url string) (ResponseMACInfo, error) {
var response ResponseMACInfo
start := time.Now()
timeout, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()
req, err := http.NewRequestWithContext(timeout, "GET", url, nil)
if err != nil {
return response, &HTTPClientError{Err: err}
}
req.Header.Set("User-Agent", ua)
req.Header.Set("Accept", "*")
resp, err := c.client.Do(req)
if err != nil {
return response, &HTTPClientError{Err: err}
}
defer resp.Body.Close()
response.RateLimit = RateLimit{
Limit: parseLimit(resp.Header.Get(xRateLimit)),
Remaining: parseIntHeader(resp.Header, xRateRemaining),
Reset: parseTimeHeader(resp.Header, xRateReset),
}
if err := checkStatusMacInfo(resp.StatusCode, resp.Body, response.Limit, response.Reset); err != nil {
return response, err
}
var apiRespose maclookupResponseAPIV2
err = json.NewDecoder(resp.Body).Decode(&apiRespose)
response.RespTime = time.Since(start)
if err != nil || !apiRespose.Success {
return response, &BadAPIResponse{Err: err}
}
//Decoupling api response
response.Found = apiRespose.Found
response.MacPrefix = apiRespose.MacPrefix
response.Company = apiRespose.Company
response.Address = apiRespose.Address
response.Country = apiRespose.Country
response.BlockStart = apiRespose.BlockStart
response.BlockEnd = apiRespose.BlockEnd
response.BlockSize = apiRespose.BlockSize
response.BlockType = apiRespose.BlockType
response.Updated = apiRespose.Updated
response.IsRand = apiRespose.IsRand
response.IsPrivate = apiRespose.IsPrivate
return response, nil
}
func checkStatusMacInfo(statusCode int, body io.Reader, rateLimit int64, rateLimitReset time.Time) error {
switch statusCode {
case http.StatusBadRequest:
var e errorResponseAPIV2
err := json.NewDecoder(body).Decode(&e)
msg := "client request error"
if err == nil {
msg = e.Error
}
return &BadAPIRequest{Err: errors.New(strings.ToLower(msg))}
case http.StatusUnauthorized:
var e errorResponseAPIV2
err := json.NewDecoder(body).Decode(&e)
msg := "bad api key"
if err == nil {
msg = e.Error
}
return &BadAPIKey{Err: errors.New(msg)}
case http.StatusTooManyRequests:
return &RateLimitsExceeded{
Limit: rateLimit,
Reset: rateLimitReset,
}
case http.StatusOK:
return nil
}
return &HTTPClientError{Err: errors.New("unexpected http status: " + strconv.Itoa(statusCode))}
}