-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip_location.go
46 lines (43 loc) · 989 Bytes
/
ip_location.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package txlbs
import (
"context"
"net/url"
)
// IPLocationResponse IP定位回复
type IPLocationResponse struct {
Meta
Result struct {
IP string `json:"ip"`
Location struct {
Lng float64 `json:"lng"`
Lat float64 `json:"lat"`
} `json:"location"`
AdInfo struct {
Nation string `json:"nation"`
Province string `json:"province"`
City string `json:"city"`
Adcode int `json:"adcode"`
} `json:"ad_info"`
} `json:"result"`
}
// IPLocation 定位查询
func (c *Client) IPLocation(ctx context.Context, ip string) (*IPLocationResponse, error) {
v := url.Values{}
if ip != "" {
v.Set("ip", ip)
}
v, err := c.signatureQueryParameter(v, string(ipLocation))
if err != nil {
return nil, err
}
r := c.ca.NewRequest().WithPath(ipLocation.Full(v.Encode()))
resp, err := c.ca.Do(ctx, r)
if err != nil {
return nil, err
}
var dr IPLocationResponse
if err := resp.DecodeFromJSON(&dr); err != nil {
return nil, err
}
return &dr, nil
}