refactor: Refactored geolocate API integration after review

This commit is contained in:
MrShtein 2024-05-12 09:07:57 +03:00
parent 9d7e5244da
commit 6dd9ffdb08
5 changed files with 51 additions and 89 deletions

View File

@ -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
}

View File

@ -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"`
}
)

View File

@ -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
}

View File

@ -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{

View File

@ -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(), &params)
res, err := api.GeoLocate(context.Background(), &params)
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(), &params)
res, err := api.GeoLocate(context.Background(), &params)
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(), &params)
res, err := api.GeoLocate(context.Background(), &params)
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(), &params)
res, err := api.GeoLocate(context.Background(), &params)
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(), &params)
res, err := api.GeoLocate(context.Background(), &params)
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(), &params)
res, err := api.GeoLocate(context.Background(), &params)
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(), &params)
res, err := api.GeoLocate(context.Background(), &params)
s.NoError(err)
s.NotEmpty(res)