-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
126 lines (98 loc) · 2.25 KB
/
client.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
117
118
119
120
121
122
123
124
125
126
package maclookup
import (
"net"
"net/http"
"strconv"
"strings"
"time"
)
const (
timeOut = 5 * time.Second
ua = "MACLookupClient-go/1.0.0 (https://maclookup.app)"
xRateLimit = "X-RateLimit-Limit"
xRateRemaining = "X-RateLimit-Remaining"
xRateReset = "X-RateLimit-Reset"
)
type Client struct {
client *http.Client
apiKey string
prefixURI string
timeOut time.Duration
}
//New creates a new client for maclookup.app API.
func New() *Client {
client := http.DefaultClient
return &Client{
client: client,
prefixURI: apiURIPrefix,
timeOut: timeOut,
}
}
//WithAPIKey adds apiKey to client.
func (c *Client) WithAPIKey(apiKey string) {
c.apiKey = apiKey
}
//WithTimeout defines a new timeout value for every request.
func (c *Client) WithTimeout(timeout time.Duration) {
c.timeOut = timeout
}
//WithPrefixURI changes the default API prefix url.
func (c *Client) WithPrefixURI(prefixURI string) {
prefix := strings.TrimRight(prefixURI, "/")
if strings.HasPrefix(prefixURI, "http://") || strings.HasPrefix(prefixURI, "https://") {
c.prefixURI = prefix
return
}
c.prefixURI = "https://" + prefix
if isIP(prefix) {
c.prefixURI = "http://" + prefix
}
}
func isIP(host string) bool {
h := strings.Split(host, ":")
if len(h) <= 2 {
h = strings.Split(h[0], "/")
return net.ParseIP(h[0]) != nil
}
return net.ParseIP(host) != nil
}
func parseIntHeader(header http.Header, property string) int64 {
parseInt, err := strconv.ParseInt(header.Get(property), 10, 64)
if err != nil {
return -1
}
return parseInt
}
func parseLimit(limit string) int64 {
l := strings.Split(limit, ", ")
parseInt, err := strconv.ParseInt(l[0], 10, 64)
if err != nil {
return -1
}
return parseInt
}
func parseTimeHeader(header http.Header, property string) time.Time {
parseInt, err := strconv.ParseInt(header.Get(property), 10, 64)
if err != nil {
return time.Time{}
}
return time.Unix(parseInt, 0)
}
func cleanMac(mac string) string {
chars := []string{":", ".", "-", " "}
m := strings.TrimSpace(mac)
for _, c := range chars {
m = strings.Replace(m, c, "", -1)
}
m = strings.ToUpper(m)
if len(m) >= 9 {
return m[0:9]
}
if len(m) >= 7 {
return m[0:7]
}
if len(m) >= 6 {
return m[0:6]
}
return strings.ToUpper(m)
}