Skip to content

Commit c7e140a

Browse files
authored
Merge pull request #71 from ipinfo/silvano/eng-647-add-resproxy-support-in-ipinfogo-library
Add Residential Proxy API support
2 parents 8f11dda + 795c56b commit c7e140a

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

example/resproxy/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
8+
"github.com/ipinfo/go/v2/ipinfo"
9+
)
10+
11+
func main() {
12+
// Get access token by signing up a free account at
13+
// https://ipinfo.io/signup.
14+
// Provide token as an environment variable `TOKEN`,
15+
// e.g. TOKEN="XXXXXXXXXXXXXX" go run main.go
16+
client := ipinfo.NewClient(nil, nil, os.Getenv("TOKEN"))
17+
resproxy, err := client.GetResproxy("175.107.211.204")
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
fmt.Printf("%v\n", resproxy)
22+
}

ipinfo/resproxy.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ipinfo
2+
3+
// ResproxyDetails represents residential proxy detection details for an IP.
4+
type ResproxyDetails struct {
5+
IP string `json:"ip"`
6+
LastSeen string `json:"last_seen"`
7+
PercentDaysSeen float64 `json:"percent_days_seen"`
8+
Service string `json:"service"`
9+
}
10+
11+
// GetResproxy returns the residential proxy details for the specified IP.
12+
func GetResproxy(ip string) (*ResproxyDetails, error) {
13+
return DefaultClient.GetResproxy(ip)
14+
}
15+
16+
// GetResproxy returns the residential proxy details for the specified IP.
17+
func (c *Client) GetResproxy(ip string) (*ResproxyDetails, error) {
18+
// perform cache lookup.
19+
cacheKey := cacheKey("resproxy:" + ip)
20+
if c.Cache != nil {
21+
if res, err := c.Cache.Get(cacheKey); err == nil {
22+
return res.(*ResproxyDetails), nil
23+
}
24+
}
25+
26+
// prepare req
27+
req, err := c.newRequest(nil, "GET", "resproxy/"+ip, nil)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
// do req
33+
v := new(ResproxyDetails)
34+
if _, err := c.do(req, v); err != nil {
35+
return nil, err
36+
}
37+
38+
// cache req result
39+
if c.Cache != nil {
40+
if err := c.Cache.Set(cacheKey, v); err != nil {
41+
return v, err
42+
}
43+
}
44+
45+
return v, nil
46+
}

0 commit comments

Comments
 (0)