diff --git a/.gitignore b/.gitignore index 6a55730..7cb33d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # Created by .ignore support plugin (hsz.mobi) /.idea +.env diff --git a/api/model/model.go b/api/model/model.go index 3d5fb08..54d1314 100644 --- a/api/model/model.go +++ b/api/model/model.go @@ -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"` // Стандартизованный адрес одной строкой diff --git a/api/suggest/geolocate.go b/api/suggest/geolocate.go new file mode 100644 index 0000000..404c4bd --- /dev/null +++ b/api/suggest/geolocate.go @@ -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"` + } +) diff --git a/api/suggest/suggest.go b/api/suggest/suggest.go index 755fec7..3e26f73 100644 --- a/api/suggest/suggest.go +++ b/api/suggest/suggest.go @@ -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 +} diff --git a/dadata.go b/dadata.go index 266595e..df7505c 100644 --- a/dadata.go +++ b/dadata.go @@ -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/" ) diff --git a/dadata_test.go b/dadata_test.go index 886b0dc..5c08a94 100644 --- a/dadata_test.go +++ b/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()