Merge pull request #26 from MrShtein/Add_geolocate_api
Add geolocate api
This commit is contained in:
commit
9cc185f545
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
# Created by .ignore support plugin (hsz.mobi)
|
||||
|
||||
/.idea
|
||||
.env
|
||||
|
|
|
@ -92,7 +92,7 @@ type (
|
|||
// full documentation https://confluence.hflabs.ru/pages/viewpage.action?pageId=222888017
|
||||
BoundValue string
|
||||
|
||||
// Address base struct for datdata.Address
|
||||
// Address base struct for dadata.Address
|
||||
Address struct {
|
||||
Source string `json:"source"` // Исходный адрес одной строкой
|
||||
Result string `json:"result"` // Стандартизованный адрес одной строкой
|
||||
|
|
29
api/suggest/geolocate.go
Normal file
29
api/suggest/geolocate.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package suggest
|
||||
|
||||
import (
|
||||
"github.com/ekomobile/dadata/v2/api/model"
|
||||
)
|
||||
|
||||
type (
|
||||
// GeolocateParams Request struct
|
||||
// full documentation https://confluence.hflabs.ru/pages/viewpage.action?pageId=808583277
|
||||
GeolocateParams struct {
|
||||
Lat string `json:"lat"` // geographic latitude
|
||||
Lon string `json:"lon"` // geographic longitude
|
||||
Count int `json:"count,omitempty"` // number of results (max 20)
|
||||
RadiusMeters int `json:"radius_meters,omitempty"` // search radius in metres (max. 1000)
|
||||
Language string `json:"language,omitempty"` // in which language to return the result (ru / en)
|
||||
}
|
||||
|
||||
// GeolocateSuggestion api response for address
|
||||
GeolocateSuggestion struct {
|
||||
Value string `json:"value"`
|
||||
UnrestrictedValue string `json:"unrestricted_value"`
|
||||
Data *model.Address `json:"data"`
|
||||
}
|
||||
|
||||
// GeolocateResponse result slice for address suggestions
|
||||
GeolocateResponse struct {
|
||||
Suggestions []*GeolocateSuggestion `json:"suggestions"`
|
||||
}
|
||||
)
|
|
@ -117,3 +117,15 @@ func (a *Api) GeoIP(ctx context.Context, ip string) (result *GeoIPResponse, err
|
|||
err = a.Client.Get(ctx, "iplocate/address", params, result)
|
||||
return
|
||||
}
|
||||
|
||||
// GeoLocate try to return suggest addresses by params
|
||||
// see documentation on:https://dadata.ru/api/geolocate/
|
||||
func (a *Api) GeoLocate(ctx context.Context, params *GeolocateParams) (ret []*GeolocateSuggestion, err error) {
|
||||
var result = &GeolocateResponse{}
|
||||
err = a.Client.Post(ctx, "geolocate/address", params, result)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ret = result.Suggestions
|
||||
return
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ const (
|
|||
EndpointURL = "https://dadata.ru/api/v2/"
|
||||
// EndpointURLSuggest is a suggestion API endpoint.
|
||||
EndpointURLSuggest = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/"
|
||||
|
||||
// EndpointURLClean is a cleaner API endpoint.
|
||||
EndpointURLClean = "https://cleaner.dadata.ru/api/v1/"
|
||||
)
|
||||
|
|
100
dadata_test.go
100
dadata_test.go
|
@ -288,6 +288,106 @@ func (s *ApiCleanIntegrationTest) TestPassport() {
|
|||
s.Len(res, 1)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithoutLongitude() {
|
||||
api := NewSuggestApi()
|
||||
|
||||
params := suggest.GeolocateParams{
|
||||
Lat: "55.878",
|
||||
}
|
||||
|
||||
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||
|
||||
s.NoError(err)
|
||||
s.Empty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithoutLatitude() {
|
||||
api := NewSuggestApi()
|
||||
|
||||
params := suggest.GeolocateParams{
|
||||
Lon: "37.653",
|
||||
}
|
||||
|
||||
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||
|
||||
s.NoError(err)
|
||||
s.Empty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithEmptyBody() {
|
||||
api := NewSuggestApi()
|
||||
|
||||
params := suggest.GeolocateParams{}
|
||||
|
||||
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||
|
||||
s.NoError(err)
|
||||
s.Empty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithLanguageParamRU() {
|
||||
api := NewSuggestApi()
|
||||
|
||||
params := suggest.GeolocateParams{
|
||||
Lat: "55.878",
|
||||
Lon: "37.653",
|
||||
Language: "RU",
|
||||
}
|
||||
|
||||
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||
|
||||
s.NoError(err)
|
||||
s.NotEmpty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithLanguageParamEN() {
|
||||
api := NewSuggestApi()
|
||||
|
||||
params := suggest.GeolocateParams{
|
||||
Lat: "55.878",
|
||||
Lon: "37.653",
|
||||
Language: "EN",
|
||||
}
|
||||
|
||||
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||
|
||||
s.NoError(err)
|
||||
s.NotEmpty(res)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithCountTwo() {
|
||||
api := NewSuggestApi()
|
||||
reqElementCount := 2
|
||||
|
||||
params := suggest.GeolocateParams{
|
||||
Lat: "55.878",
|
||||
Lon: "37.653",
|
||||
Language: "EN",
|
||||
Count: reqElementCount,
|
||||
}
|
||||
|
||||
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||
|
||||
s.NoError(err)
|
||||
s.Len(res, reqElementCount)
|
||||
}
|
||||
|
||||
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithRadius() {
|
||||
api := NewSuggestApi()
|
||||
|
||||
params := suggest.GeolocateParams{
|
||||
Lat: "55.878",
|
||||
Lon: "37.653",
|
||||
Language: "EN",
|
||||
RadiusMeters: 100,
|
||||
}
|
||||
|
||||
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||
|
||||
s.NoError(err)
|
||||
s.NotEmpty(res)
|
||||
}
|
||||
|
||||
func ExampleNewSuggestApi() {
|
||||
api := NewSuggestApi()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user