-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ipinfo/uman/ip-summary
Add IP summary API integration.
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |