forked from abclogin/dadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoip.go
56 lines (49 loc) · 2 KB
/
geoip.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
47
48
49
50
51
52
53
54
55
56
package dadata // import "gopkg.in/webdeskltd/dadata.v2"
import (
"context"
"fmt"
"net/http"
)
// GeoIP try to find address by IP
// see documentation on:
// https://dadata.ru/api/detect_address_by_ip/
// https://confluence.hflabs.ru/pages/viewpage.action?pageId=715096277
// ip string representation of ip-address (example 10.12.44.23)
// if ip=="" then dadata try to get ip-address from X-Forwarded-For header
func (daData *DaData) GeoIP(ip string) (*GeoIPResponse, error) {
return daData.GeoIPWithCtx(context.Background(), ip)
}
// GeoIPWithCtx try to find address by IP
// see documentation on:
// https://dadata.ru/api/detect_address_by_ip/
// https://confluence.hflabs.ru/pages/viewpage.action?pageId=715096277
// ip string representation of ip-address (example 10.12.44.23)
// if ip=="" then dadata try to get ip-address from X-Forwarded-For header
func (daData *DaData) GeoIPWithCtx(ctx context.Context, ip string) (result *GeoIPResponse, err error) {
result = &GeoIPResponse{}
if err = daData.sendRequestToURL(ctx, "GET", baseSuggestURL+"detectAddressByIp?ip="+ip, nil, &result); err != nil {
result = nil
} else if result.Location == nil {
result, err = nil, fmt.Errorf("dadata.GeoIP: cannot detect address by ip %s", ip)
}
return
}
// GeolocateAddress try to find address by coordinates
func (daData *DaData) GeolocateAddress(req GeolocateRequest) ([]ResponseAddress, error) {
return daData.GeolocateAddressWithCtx(context.Background(), req)
}
// GeolocateAddressWithCtx try to find address by coordinates
func (daData *DaData) GeolocateAddressWithCtx(ctx context.Context, req GeolocateRequest) (ret []ResponseAddress, err error) {
var result = &SuggestAddressResponse{}
err = daData.sendRequestToURL(ctx, http.MethodPost, baseSuggestURL+"geolocate/address", &req, result)
if err != nil {
ret = nil
return
}
if len(result.Suggestions) == 0 {
ret, err = nil, fmt.Errorf("dadata.GeolocateAddress: cannot detect addresses by coordinates %+v", req)
return
}
ret = result.Suggestions
return
}