Skip to content

Commit

Permalink
Merge pull request #22 from ipinfo/uman/batch-ops
Browse files Browse the repository at this point in the history
Batch ops
  • Loading branch information
UmanShahzad authored Jan 15, 2021
2 parents 2381479 + 940b8f0 commit e7d6ce2
Show file tree
Hide file tree
Showing 11 changed files with 572 additions and 46 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# 2.2.0

- The following functions are now private:
- `Client.Do`
- `Client.NewRequest`
- `CheckResponse`
- The following **new** functions now exist, which operate on the IPinfo
`/batch` endpoint:
- `Client.GetBatch`
- `Client.GetIPInfoBatch`
- `Client.GetIPStrInfoBatch`
- `Client.GetASNDetailsBatch`
- `ipinfo.GetBatch`
- `ipinfo.GetIPInfoBatch`
- `ipinfo.GetIPStrInfoBatch`
- `ipinfo.GetASNDetailsBatch`

# 2.1.1

- Fixed go module path to have "v2" at the end as necessary.
Expand Down
39 changes: 39 additions & 0 deletions example/batch-asn/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"log"
"os"
"time"

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

func main() {
client := ipinfo.NewClient(
nil,
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
os.Getenv("TOKEN"),
)
for i := 0; i < 3; i++ {
fmt.Printf("doing lookup #%v\n", i)
batchResult, err := client.GetASNDetailsBatch(
[]string{
"AS321",
"AS999",
},
ipinfo.BatchReqOpts{
TimeoutPerBatch: 0,
TimeoutTotal: 5,
},
)
if err != nil {
log.Fatal(err)
}
for k, v := range batchResult {
fmt.Printf("k=%v v=%v\n", k, v)
}
fmt.Println()
}
}
40 changes: 40 additions & 0 deletions example/batch-core-netip/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

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

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

func main() {
client := ipinfo.NewClient(
nil,
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
os.Getenv("TOKEN"),
)
for i := 0; i < 3; i++ {
fmt.Printf("doing lookup #%v\n", i)
batchResult, err := client.GetIPInfoBatch(
[]net.IP{
net.ParseIP("1.1.1.1"),
net.ParseIP("8.8.8.8"),
},
ipinfo.BatchReqOpts{
TimeoutPerBatch: 0,
TimeoutTotal: 5,
},
)
if err != nil {
log.Fatal(err)
}
for k, v := range batchResult {
fmt.Printf("k=%v v=%v\n", k, v)
}
fmt.Println()
}
}
39 changes: 39 additions & 0 deletions example/batch-core-str/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"log"
"os"
"time"

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

func main() {
client := ipinfo.NewClient(
nil,
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
os.Getenv("TOKEN"),
)
for i := 0; i < 3; i++ {
fmt.Printf("doing lookup #%v\n", i)
batchResult, err := client.GetIPStrInfoBatch(
[]string{
"1.1.1.1",
"8.8.8.8",
},
ipinfo.BatchReqOpts{
TimeoutPerBatch: 0,
TimeoutTotal: 5,
},
)
if err != nil {
log.Fatal(err)
}
for k, v := range batchResult {
fmt.Printf("k=%v v=%v\n", k, v)
}
fmt.Println()
}
}
43 changes: 43 additions & 0 deletions example/batch-generic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"log"
"os"
"time"

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

func main() {
client := ipinfo.NewClient(
nil,
ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
os.Getenv("TOKEN"),
)
for i := 0; i < 3; i++ {
fmt.Printf("doing lookup #%v\n", i)
batchResult, err := client.GetBatch(
[]string{
"1.1.1.1",
"1.1.1.1/country",
"8.8.8.8",
"8.8.8.8/country",
"AS321",
},
ipinfo.BatchReqOpts{
BatchSize: 2,
TimeoutPerBatch: 0,
TimeoutTotal: 5,
},
)
if err != nil {
log.Fatal(err)
}
for k, v := range batchResult {
fmt.Printf("k=%v v=%v\n", k, v)
}
fmt.Println()
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/ipinfo/go/v2

go 1.15

require github.com/patrickmn/go-cache v2.1.0+incompatible
require (
github.com/patrickmn/go-cache v2.1.0+incompatible
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
24 changes: 14 additions & 10 deletions ipinfo/asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func (err *InvalidASNError) Error() string {
return "invalid ASN: " + err.ASN
}

// Set `v.CountryName` properly by mapping country abbreviation to full country
// name.
func (v *ASNDetails) setCountryName() {
if v.Country != "" {
v.CountryName = countriesMap[v.Country]
}
}

// GetASNDetails returns the details for the specified ASN.
func GetASNDetails(asn string) (*ASNDetails, error) {
return DefaultClient.GetASNDetails(asn)
Expand All @@ -53,35 +61,31 @@ func (c *Client) GetASNDetails(asn string) (*ASNDetails, error) {
return nil, &InvalidASNError{ASN: asn}
}

cacheKey := "asn:" + asn

// perform cache lookup.
if c.Cache != nil {
if res, err := c.Cache.Get(cacheKey); err == nil {
if res, err := c.Cache.Get(asn); err == nil {
return res.(*ASNDetails), nil
}
}

// prepare req
req, err := c.NewRequest(asn + "/json")
req, err := c.newRequest(nil, "GET", asn, nil)
if err != nil {
return nil, err
}

// do req
v := new(ASNDetails)
if _, err := c.Do(req, v); err != nil {
if _, err := c.do(req, v); err != nil {
return nil, err
}

// map country to full country name
if v.Country != "" {
v.CountryName = countriesMap[v.Country]
}
// format
v.setCountryName()

// cache req result
if c.Cache != nil {
if err := c.Cache.Set(cacheKey, v); err != nil {
if err := c.Cache.Set(asn, v); err != nil {
return v, err
}
}
Expand Down
Loading

0 comments on commit e7d6ce2

Please sign in to comment.