-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompany_name.go
97 lines (76 loc) · 2.19 KB
/
company_name.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
package maclookup
import (
"context"
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
//CompanyName returns company name from API.
func (c Client) CompanyName(mac string) (ResponseVendorName, error) {
url := c.prefixURI + apiMAC + cleanMac(mac) + companyNameSuffix
if c.apiKey != "" {
url += apiKeyParam + c.apiKey
}
return c.getCompanyName(url)
}
func (c Client) getCompanyName(url string) (ResponseVendorName, error) {
var response ResponseVendorName
start := time.Now()
timeout, cancell := context.WithTimeout(context.Background(), c.timeOut)
defer cancell()
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),
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, &HTTPClientError{Err: err}
}
body := string(bodyBytes)
response.RespTime = time.Since(start)
statusCode := resp.StatusCode
switch statusCode {
case http.StatusBadRequest:
msg := "client request error"
if body != "" {
msg = body
}
return response, &BadAPIRequest{Err: errors.New(strings.ToLower(msg))}
case http.StatusUnauthorized:
msg := "bad api key"
if body != "" {
msg = body
}
return response, &BadAPIKey{Err: errors.New(msg)}
case http.StatusTooManyRequests:
return response, &RateLimitsExceeded{
Limit: response.RateLimit.Limit,
Reset: response.RateLimit.Reset,
}
case http.StatusNotFound:
return response, &HTTPClientError{Err: errors.New("endpoint not found")}
case http.StatusOK:
response.Found = !(body == "*NO COMPANY*")
response.IsPrivate = body == "*PRIVATE*"
if response.Found && !response.IsPrivate {
response.Company = body
}
return response, nil
}
return response, &HTTPClientError{Err: errors.New("unexpected http status: " + strconv.Itoa(statusCode))}
}