From 6dd9ffdb08dd0639b74c757e8ab223cc2d13c9fc Mon Sep 17 00:00:00 2001 From: MrShtein Date: Sun, 12 May 2024 09:07:57 +0300 Subject: [PATCH] refactor: Refactored geolocate API integration after review --- api/geolocate/geolocate.go | 30 -------- .../model.go => suggest/geolocate.go} | 20 +++--- api/suggest/suggest.go | 12 ++++ dadata.go | 7 -- dadata_test.go | 71 ++++++++----------- 5 files changed, 51 insertions(+), 89 deletions(-) delete mode 100644 api/geolocate/geolocate.go rename api/{geolocate/model.go => suggest/geolocate.go} (66%) diff --git a/api/geolocate/geolocate.go b/api/geolocate/geolocate.go deleted file mode 100644 index ac15e91..0000000 --- a/api/geolocate/geolocate.go +++ /dev/null @@ -1,30 +0,0 @@ -package geolocate - -import ( - "context" -) - -type ( - // Requester provides transport level API calls. - Requester interface { - // Post makes a POST API call. Assumes sending json-encoded params in a request body. - Post(ctx context.Context, apiMethod string, params, result interface{}) error - } - - // Api provides suggestion API. - Api struct { - Client Requester - } -) - -// AddressByCoordinates try to return suggest addresses by params -// see documentation on:https://dadata.ru/api/geolocate/ -func (a *Api) AddressByCoordinates(ctx context.Context, params *RequestParams) (ret []*AddressSuggestion, err error) { - var result = &AddressResponse{} - err = a.Client.Post(ctx, "geolocate/address", params, result) - if err != nil { - return - } - ret = result.Suggestions - return -} diff --git a/api/geolocate/model.go b/api/suggest/geolocate.go similarity index 66% rename from api/geolocate/model.go rename to api/suggest/geolocate.go index 4d06404..8fffa8b 100644 --- a/api/geolocate/model.go +++ b/api/suggest/geolocate.go @@ -1,11 +1,13 @@ -package geolocate +package suggest -import "github.com/ekomobile/dadata/v2/api/model" +import ( + "github.com/ekomobile/dadata/v2/api/model" +) type ( - // RequestParams Request struct + // GeolocateParams Request struct // full documentation https://confluence.hflabs.ru/pages/viewpage.action?pageId=808583277 - RequestParams struct { + GeolocateParams struct { Lat string `json:"lat"` // geographic latitude Lon string `json:"lon"` // geographic longitude Count string `json:"count,omitempty"` // number of results (max 20) @@ -13,15 +15,15 @@ type ( Language string `json:"language,omitempty"` // in which language to return the result (ru / en) } - // AddressSuggestion api response for address - AddressSuggestion struct { + // GeolocateSuggestion api response for address + GeolocateSuggestion struct { Value string `json:"value"` UnrestrictedValue string `json:"unrestricted_value"` Data *model.Address `json:"data"` } - // AddressResponse result slice for address suggestions - AddressResponse struct { - Suggestions []*AddressSuggestion `json:"suggestions"` + // 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 a4818a8..df7505c 100644 --- a/dadata.go +++ b/dadata.go @@ -4,7 +4,6 @@ import ( "net/url" "github.com/ekomobile/dadata/v2/api/clean" - "github.com/ekomobile/dadata/v2/api/geolocate" "github.com/ekomobile/dadata/v2/api/profile" "github.com/ekomobile/dadata/v2/api/stat" "github.com/ekomobile/dadata/v2/api/suggest" @@ -60,12 +59,6 @@ func NewSuggestApi(opts ...client.Option) *suggest.Api { } } -func NewGeolocateApi(opts ...client.Option) *geolocate.Api { - return &geolocate.Api{ - Client: client.NewClient(endpointURLSuggest, opts...), - } -} - // NewProfileApi provides profile related API. func NewProfileApi(opts ...client.Option) *profile.Api { return &profile.Api{ diff --git a/dadata_test.go b/dadata_test.go index 21ad5c9..4307cda 100644 --- a/dadata_test.go +++ b/dadata_test.go @@ -9,7 +9,6 @@ import ( "strconv" "testing" - "github.com/ekomobile/dadata/v2/api/geolocate" "github.com/stretchr/testify/suite" "github.com/ekomobile/dadata/v2/api/suggest" @@ -23,10 +22,6 @@ type ( ApiCleanIntegrationTest struct { suite.Suite } - - ApiGeolocateIntegrationTest struct { - suite.Suite - } ) func (s *ApiSuggestIntegrationTest) SetupSuite() { @@ -41,12 +36,6 @@ func (s *ApiCleanIntegrationTest) SetupSuite() { } } -func (s *ApiGeolocateIntegrationTest) SetupSuite() { - if _, ok := os.LookupEnv("DADATA_API_KEY"); !ok { - s.Suite.T().Skip("no api keys in env") - } -} - func TestSuggestApiIntegration(t *testing.T) { suite.Run(t, &ApiSuggestIntegrationTest{}) } @@ -55,10 +44,6 @@ func TestCleanApiIntegration(t *testing.T) { suite.Run(t, &ApiCleanIntegrationTest{}) } -func TestGeolocationApiIntegration(t *testing.T) { - suite.Run(t, &ApiGeolocateIntegrationTest{}) -} - func (s *ApiSuggestIntegrationTest) TestAddress() { api := NewSuggestApi() params := suggest.RequestParams{ @@ -304,101 +289,101 @@ func (s *ApiCleanIntegrationTest) TestPassport() { s.Len(res, 1) } -func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithoutLongitude() { - api := NewGeolocateApi() +func (s *ApiSuggestIntegrationTest) TestGeoLocateWithoutLongitude() { + api := NewSuggestApi() - params := geolocate.RequestParams{ + params := suggest.GeolocateParams{ Lat: "55.878", } - res, err := api.AddressByCoordinates(context.Background(), ¶ms) + res, err := api.GeoLocate(context.Background(), ¶ms) s.NoError(err) s.Empty(res) } -func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithoutLatitude() { - api := NewGeolocateApi() +func (s *ApiSuggestIntegrationTest) TestGeoLocateWithoutLatitude() { + api := NewSuggestApi() - params := geolocate.RequestParams{ + params := suggest.GeolocateParams{ Lon: "37.653", } - res, err := api.AddressByCoordinates(context.Background(), ¶ms) + res, err := api.GeoLocate(context.Background(), ¶ms) s.NoError(err) s.Empty(res) } -func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithEmptyBody() { - api := NewGeolocateApi() +func (s *ApiSuggestIntegrationTest) TestGeoLocateWithEmptyBody() { + api := NewSuggestApi() - params := geolocate.RequestParams{} + params := suggest.GeolocateParams{} - res, err := api.AddressByCoordinates(context.Background(), ¶ms) + res, err := api.GeoLocate(context.Background(), ¶ms) s.NoError(err) s.Empty(res) } -func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithLanguageParamRU() { - api := NewGeolocateApi() +func (s *ApiSuggestIntegrationTest) TestGeoLocateWithLanguageParamRU() { + api := NewSuggestApi() - params := geolocate.RequestParams{ + params := suggest.GeolocateParams{ Lat: "55.878", Lon: "37.653", Language: "RU", } - res, err := api.AddressByCoordinates(context.Background(), ¶ms) + res, err := api.GeoLocate(context.Background(), ¶ms) s.NoError(err) s.NotEmpty(res) } -func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithLanguageParamEN() { - api := NewGeolocateApi() +func (s *ApiSuggestIntegrationTest) TestGeoLocateWithLanguageParamEN() { + api := NewSuggestApi() - params := geolocate.RequestParams{ + params := suggest.GeolocateParams{ Lat: "55.878", Lon: "37.653", Language: "EN", } - res, err := api.AddressByCoordinates(context.Background(), ¶ms) + res, err := api.GeoLocate(context.Background(), ¶ms) s.NoError(err) s.NotEmpty(res) } -func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithCountTwo() { - api := NewGeolocateApi() +func (s *ApiSuggestIntegrationTest) TestGeoLocateWithCountTwo() { + api := NewSuggestApi() reqElementCount := 2 - params := geolocate.RequestParams{ + params := suggest.GeolocateParams{ Lat: "55.878", Lon: "37.653", Language: "EN", Count: strconv.Itoa(reqElementCount), } - res, err := api.AddressByCoordinates(context.Background(), ¶ms) + res, err := api.GeoLocate(context.Background(), ¶ms) s.NoError(err) s.Len(res, 2) } -func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithRadius() { - api := NewGeolocateApi() +func (s *ApiSuggestIntegrationTest) TestGeoLocateWithRadius() { + api := NewSuggestApi() - params := geolocate.RequestParams{ + params := suggest.GeolocateParams{ Lat: "55.878", Lon: "37.653", Language: "EN", RadiusMeters: "100", } - res, err := api.AddressByCoordinates(context.Background(), ¶ms) + res, err := api.GeoLocate(context.Background(), ¶ms) s.NoError(err) s.NotEmpty(res)