Skip to content

Commit

Permalink
add simple integration for map endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
UmanShahzad committed Apr 21, 2021
1 parent 0e66ea4 commit be16a8e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.4.0

- Added support for IP Map API.

# 2.3.2

- Added the `Core.Bogon` boolean field.
Expand Down
2 changes: 1 addition & 1 deletion ipinfo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
defaultBaseURL = "https://ipinfo.io/"
defaultUserAgent = "IPinfoClient/Go/2.3.2"
defaultUserAgent = "IPinfoClient/Go/2.4.0"
)

// A Client is the main handler to communicate with the IPinfo API.
Expand Down
49 changes: 49 additions & 0 deletions ipinfo/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ipinfo

import (
"bytes"
"encoding/json"
"errors"
"net"
)

// IPMap is the full JSON response from the IP Map API.
type IPMap struct {
Status string `json:"status"`
ReportURL string `json:"reportUrl"`
}

// GetIPMap returns an IPMap result for a group of IPs.
//
// `len(ips)` must not exceed 500,000.
func GetIPMap(ips []net.IP) (*IPMap, error) {
return DefaultClient.GetIPMap(ips)
}

// GetIPMap returns an IPMap result for a group of IPs.
//
// `len(ips)` must not exceed 500,000.
func (c *Client) GetIPMap(ips []net.IP) (*IPMap, error) {
if len(ips) > 500000 {
return nil, errors.New("ip count must be <500,000")
}

jsonArrStr, err := json.Marshal(ips)
if err != nil {
return nil, err
}
jsonBuf := bytes.NewBuffer(jsonArrStr)

req, err := c.newRequest(nil, "POST", "map?cli=1", jsonBuf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")

result := new(IPMap)
if _, err := c.do(req, result); err != nil {
return nil, err
}

return result, nil
}

0 comments on commit be16a8e

Please sign in to comment.