Skip to content

Commit e7d6ce2

Browse files
authored
Merge pull request #22 from ipinfo/uman/batch-ops
Batch ops
2 parents 2381479 + 940b8f0 commit e7d6ce2

File tree

11 files changed

+572
-46
lines changed

11 files changed

+572
-46
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# 2.2.0
2+
3+
- The following functions are now private:
4+
- `Client.Do`
5+
- `Client.NewRequest`
6+
- `CheckResponse`
7+
- The following **new** functions now exist, which operate on the IPinfo
8+
`/batch` endpoint:
9+
- `Client.GetBatch`
10+
- `Client.GetIPInfoBatch`
11+
- `Client.GetIPStrInfoBatch`
12+
- `Client.GetASNDetailsBatch`
13+
- `ipinfo.GetBatch`
14+
- `ipinfo.GetIPInfoBatch`
15+
- `ipinfo.GetIPStrInfoBatch`
16+
- `ipinfo.GetASNDetailsBatch`
17+
118
# 2.1.1
219

320
- Fixed go module path to have "v2" at the end as necessary.

example/batch-asn/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"time"
8+
9+
"github.com/ipinfo/go/v2/ipinfo"
10+
"github.com/ipinfo/go/v2/ipinfo/cache"
11+
)
12+
13+
func main() {
14+
client := ipinfo.NewClient(
15+
nil,
16+
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
17+
os.Getenv("TOKEN"),
18+
)
19+
for i := 0; i < 3; i++ {
20+
fmt.Printf("doing lookup #%v\n", i)
21+
batchResult, err := client.GetASNDetailsBatch(
22+
[]string{
23+
"AS321",
24+
"AS999",
25+
},
26+
ipinfo.BatchReqOpts{
27+
TimeoutPerBatch: 0,
28+
TimeoutTotal: 5,
29+
},
30+
)
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
for k, v := range batchResult {
35+
fmt.Printf("k=%v v=%v\n", k, v)
36+
}
37+
fmt.Println()
38+
}
39+
}

example/batch-core-netip/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net"
7+
"os"
8+
"time"
9+
10+
"github.com/ipinfo/go/v2/ipinfo"
11+
"github.com/ipinfo/go/v2/ipinfo/cache"
12+
)
13+
14+
func main() {
15+
client := ipinfo.NewClient(
16+
nil,
17+
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
18+
os.Getenv("TOKEN"),
19+
)
20+
for i := 0; i < 3; i++ {
21+
fmt.Printf("doing lookup #%v\n", i)
22+
batchResult, err := client.GetIPInfoBatch(
23+
[]net.IP{
24+
net.ParseIP("1.1.1.1"),
25+
net.ParseIP("8.8.8.8"),
26+
},
27+
ipinfo.BatchReqOpts{
28+
TimeoutPerBatch: 0,
29+
TimeoutTotal: 5,
30+
},
31+
)
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
for k, v := range batchResult {
36+
fmt.Printf("k=%v v=%v\n", k, v)
37+
}
38+
fmt.Println()
39+
}
40+
}

example/batch-core-str/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"time"
8+
9+
"github.com/ipinfo/go/v2/ipinfo"
10+
"github.com/ipinfo/go/v2/ipinfo/cache"
11+
)
12+
13+
func main() {
14+
client := ipinfo.NewClient(
15+
nil,
16+
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
17+
os.Getenv("TOKEN"),
18+
)
19+
for i := 0; i < 3; i++ {
20+
fmt.Printf("doing lookup #%v\n", i)
21+
batchResult, err := client.GetIPStrInfoBatch(
22+
[]string{
23+
"1.1.1.1",
24+
"8.8.8.8",
25+
},
26+
ipinfo.BatchReqOpts{
27+
TimeoutPerBatch: 0,
28+
TimeoutTotal: 5,
29+
},
30+
)
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
for k, v := range batchResult {
35+
fmt.Printf("k=%v v=%v\n", k, v)
36+
}
37+
fmt.Println()
38+
}
39+
}

example/batch-generic/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"time"
8+
9+
"github.com/ipinfo/go/v2/ipinfo"
10+
"github.com/ipinfo/go/v2/ipinfo/cache"
11+
)
12+
13+
func main() {
14+
client := ipinfo.NewClient(
15+
nil,
16+
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
17+
os.Getenv("TOKEN"),
18+
)
19+
for i := 0; i < 3; i++ {
20+
fmt.Printf("doing lookup #%v\n", i)
21+
batchResult, err := client.GetBatch(
22+
[]string{
23+
"1.1.1.1",
24+
"1.1.1.1/country",
25+
"8.8.8.8",
26+
"8.8.8.8/country",
27+
"AS321",
28+
},
29+
ipinfo.BatchReqOpts{
30+
BatchSize: 2,
31+
TimeoutPerBatch: 0,
32+
TimeoutTotal: 5,
33+
},
34+
)
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
for k, v := range batchResult {
39+
fmt.Printf("k=%v v=%v\n", k, v)
40+
}
41+
fmt.Println()
42+
}
43+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/ipinfo/go/v2
22

33
go 1.15
44

5-
require github.com/patrickmn/go-cache v2.1.0+incompatible
5+
require (
6+
github.com/patrickmn/go-cache v2.1.0+incompatible
7+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
22
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
3+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
4+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

ipinfo/asn.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func (err *InvalidASNError) Error() string {
4242
return "invalid ASN: " + err.ASN
4343
}
4444

45+
// Set `v.CountryName` properly by mapping country abbreviation to full country
46+
// name.
47+
func (v *ASNDetails) setCountryName() {
48+
if v.Country != "" {
49+
v.CountryName = countriesMap[v.Country]
50+
}
51+
}
52+
4553
// GetASNDetails returns the details for the specified ASN.
4654
func GetASNDetails(asn string) (*ASNDetails, error) {
4755
return DefaultClient.GetASNDetails(asn)
@@ -53,35 +61,31 @@ func (c *Client) GetASNDetails(asn string) (*ASNDetails, error) {
5361
return nil, &InvalidASNError{ASN: asn}
5462
}
5563

56-
cacheKey := "asn:" + asn
57-
5864
// perform cache lookup.
5965
if c.Cache != nil {
60-
if res, err := c.Cache.Get(cacheKey); err == nil {
66+
if res, err := c.Cache.Get(asn); err == nil {
6167
return res.(*ASNDetails), nil
6268
}
6369
}
6470

6571
// prepare req
66-
req, err := c.NewRequest(asn + "/json")
72+
req, err := c.newRequest(nil, "GET", asn, nil)
6773
if err != nil {
6874
return nil, err
6975
}
7076

7177
// do req
7278
v := new(ASNDetails)
73-
if _, err := c.Do(req, v); err != nil {
79+
if _, err := c.do(req, v); err != nil {
7480
return nil, err
7581
}
7682

77-
// map country to full country name
78-
if v.Country != "" {
79-
v.CountryName = countriesMap[v.Country]
80-
}
83+
// format
84+
v.setCountryName()
8185

8286
// cache req result
8387
if c.Cache != nil {
84-
if err := c.Cache.Set(cacheKey, v); err != nil {
88+
if err := c.Cache.Set(asn, v); err != nil {
8589
return v, err
8690
}
8791
}

0 commit comments

Comments
 (0)