Skip to content

Commit

Permalink
Merge pull request #27 from ipinfo/uman/ip-summary
Browse files Browse the repository at this point in the history
Add IP summary API integration.
  • Loading branch information
UmanShahzad authored Feb 9, 2021
2 parents 03b0834 + f5d1a35 commit 40b3f22
Show file tree
Hide file tree
Showing 4 changed files with 111 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.3.0

- Added support for IP summary API.

# 2.2.3

- Added CSV tags for `Core` data for easier CSV marshaling.
Expand Down
42 changes: 42 additions & 0 deletions example/ip-summary/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"log"
"net"
"os"

"github.com/ipinfo/go/v2/ipinfo"
)

func main() {
client := ipinfo.NewClient(nil, nil, os.Getenv("TOKEN"))
result, err := client.GetIPSummary(
[]net.IP{
net.ParseIP("3.3.3.0"),
net.ParseIP("3.3.3.1"),
net.ParseIP("3.3.3.2"),
net.ParseIP("3.3.3.3"),
net.ParseIP("4.4.4.0"),
net.ParseIP("4.4.4.1"),
net.ParseIP("4.4.4.2"),
net.ParseIP("4.4.4.3"),
net.ParseIP("8.8.8.0"),
net.ParseIP("8.8.8.1"),
net.ParseIP("8.8.8.2"),
net.ParseIP("8.8.8.3"),
net.ParseIP("1.1.1.0"),
net.ParseIP("1.1.1.1"),
net.ParseIP("1.1.1.2"),
net.ParseIP("1.1.1.3"),
net.ParseIP("2.2.2.0"),
net.ParseIP("2.2.2.1"),
net.ParseIP("2.2.2.2"),
net.ParseIP("2.2.2.3"),
},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("result=%v\n", result)
}
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.2.3"
defaultUserAgent = "IPinfoClient/Go/2.3.0"
)

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

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

// IPSummary is the full JSON response from the IP summary API.
type IPSummary struct {
Total uint64 `json:"total"`
Unique uint64 `json:"unique"`
Countries map[string]uint64 `json:"countries"`
Cities map[string]uint64 `json:"cities"`
Regions map[string]uint64 `json:"regions"`
ASNs map[string]uint64 `json:"asns"`
Companies map[string]uint64 `json:"companies"`
IPTypes map[string]uint64 `json:"ipTypes"`
Routes map[string]uint64 `json:"routes"`
Privacy struct {
VPN uint64 `json:"vpn"`
Proxy uint64 `json:"proxy"`
Hosting uint64 `json:"hosting"`
Tor uint64 `json:"tor"`
} `json:"privacy"`
Anycast uint64 `json:"anycast"`
Bogon uint64 `json:"bogon"`
}

// GetIPSummary returns summarized results for a group of IPs.
//
// `ips` must contain at least 10 unique IPs, and a total maximum of 1000.
func GetIPSummary(ips []net.IP) (*IPSummary, error) {
return DefaultClient.GetIPSummary(ips)
}

// GetIPSummary returns summarized results for a group of IPs.
//
// `ips` must contain at least 10 unique IPs, and a total maximum of 1000.
func (c *Client) GetIPSummary(ips []net.IP) (*IPSummary, error) {
if len(ips) < 10 || len(ips) > 1000 {
return nil, errors.New("unique ip count must be >10 && <1000")
}

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

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

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

return result, nil
}

0 comments on commit 40b3f22

Please sign in to comment.