Skip to content

Commit be16a8e

Browse files
committed
add simple integration for map endpoint.
1 parent 0e66ea4 commit be16a8e

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.4.0
2+
3+
- Added support for IP Map API.
4+
15
# 2.3.2
26

37
- Added the `Core.Bogon` boolean field.

ipinfo/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
const (
1515
defaultBaseURL = "https://ipinfo.io/"
16-
defaultUserAgent = "IPinfoClient/Go/2.3.2"
16+
defaultUserAgent = "IPinfoClient/Go/2.4.0"
1717
)
1818

1919
// A Client is the main handler to communicate with the IPinfo API.

ipinfo/map.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ipinfo
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"net"
8+
)
9+
10+
// IPMap is the full JSON response from the IP Map API.
11+
type IPMap struct {
12+
Status string `json:"status"`
13+
ReportURL string `json:"reportUrl"`
14+
}
15+
16+
// GetIPMap returns an IPMap result for a group of IPs.
17+
//
18+
// `len(ips)` must not exceed 500,000.
19+
func GetIPMap(ips []net.IP) (*IPMap, error) {
20+
return DefaultClient.GetIPMap(ips)
21+
}
22+
23+
// GetIPMap returns an IPMap result for a group of IPs.
24+
//
25+
// `len(ips)` must not exceed 500,000.
26+
func (c *Client) GetIPMap(ips []net.IP) (*IPMap, error) {
27+
if len(ips) > 500000 {
28+
return nil, errors.New("ip count must be <500,000")
29+
}
30+
31+
jsonArrStr, err := json.Marshal(ips)
32+
if err != nil {
33+
return nil, err
34+
}
35+
jsonBuf := bytes.NewBuffer(jsonArrStr)
36+
37+
req, err := c.newRequest(nil, "POST", "map?cli=1", jsonBuf)
38+
if err != nil {
39+
return nil, err
40+
}
41+
req.Header.Set("Content-Type", "application/json")
42+
43+
result := new(IPMap)
44+
if _, err := c.do(req, result); err != nil {
45+
return nil, err
46+
}
47+
48+
return result, nil
49+
}

0 commit comments

Comments
 (0)