refactor: Refactored geolocate API integration after review
This commit is contained in:
parent
9d7e5244da
commit
6dd9ffdb08
|
@ -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
|
|
||||||
}
|
|
|
@ -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 (
|
type (
|
||||||
// RequestParams Request struct
|
// GeolocateParams Request struct
|
||||||
// full documentation https://confluence.hflabs.ru/pages/viewpage.action?pageId=808583277
|
// full documentation https://confluence.hflabs.ru/pages/viewpage.action?pageId=808583277
|
||||||
RequestParams struct {
|
GeolocateParams struct {
|
||||||
Lat string `json:"lat"` // geographic latitude
|
Lat string `json:"lat"` // geographic latitude
|
||||||
Lon string `json:"lon"` // geographic longitude
|
Lon string `json:"lon"` // geographic longitude
|
||||||
Count string `json:"count,omitempty"` // number of results (max 20)
|
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)
|
Language string `json:"language,omitempty"` // in which language to return the result (ru / en)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddressSuggestion api response for address
|
// GeolocateSuggestion api response for address
|
||||||
AddressSuggestion struct {
|
GeolocateSuggestion struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
UnrestrictedValue string `json:"unrestricted_value"`
|
UnrestrictedValue string `json:"unrestricted_value"`
|
||||||
Data *model.Address `json:"data"`
|
Data *model.Address `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddressResponse result slice for address suggestions
|
// GeolocateResponse result slice for address suggestions
|
||||||
AddressResponse struct {
|
GeolocateResponse struct {
|
||||||
Suggestions []*AddressSuggestion `json:"suggestions"`
|
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)
|
err = a.Client.Get(ctx, "iplocate/address", params, result)
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/ekomobile/dadata/v2/api/clean"
|
"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/profile"
|
||||||
"github.com/ekomobile/dadata/v2/api/stat"
|
"github.com/ekomobile/dadata/v2/api/stat"
|
||||||
"github.com/ekomobile/dadata/v2/api/suggest"
|
"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.
|
// NewProfileApi provides profile related API.
|
||||||
func NewProfileApi(opts ...client.Option) *profile.Api {
|
func NewProfileApi(opts ...client.Option) *profile.Api {
|
||||||
return &profile.Api{
|
return &profile.Api{
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ekomobile/dadata/v2/api/geolocate"
|
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
"github.com/ekomobile/dadata/v2/api/suggest"
|
"github.com/ekomobile/dadata/v2/api/suggest"
|
||||||
|
@ -23,10 +22,6 @@ type (
|
||||||
ApiCleanIntegrationTest struct {
|
ApiCleanIntegrationTest struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiGeolocateIntegrationTest struct {
|
|
||||||
suite.Suite
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *ApiSuggestIntegrationTest) SetupSuite() {
|
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) {
|
func TestSuggestApiIntegration(t *testing.T) {
|
||||||
suite.Run(t, &ApiSuggestIntegrationTest{})
|
suite.Run(t, &ApiSuggestIntegrationTest{})
|
||||||
}
|
}
|
||||||
|
@ -55,10 +44,6 @@ func TestCleanApiIntegration(t *testing.T) {
|
||||||
suite.Run(t, &ApiCleanIntegrationTest{})
|
suite.Run(t, &ApiCleanIntegrationTest{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGeolocationApiIntegration(t *testing.T) {
|
|
||||||
suite.Run(t, &ApiGeolocateIntegrationTest{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *ApiSuggestIntegrationTest) TestAddress() {
|
func (s *ApiSuggestIntegrationTest) TestAddress() {
|
||||||
api := NewSuggestApi()
|
api := NewSuggestApi()
|
||||||
params := suggest.RequestParams{
|
params := suggest.RequestParams{
|
||||||
|
@ -304,101 +289,101 @@ func (s *ApiCleanIntegrationTest) TestPassport() {
|
||||||
s.Len(res, 1)
|
s.Len(res, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithoutLongitude() {
|
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithoutLongitude() {
|
||||||
api := NewGeolocateApi()
|
api := NewSuggestApi()
|
||||||
|
|
||||||
params := geolocate.RequestParams{
|
params := suggest.GeolocateParams{
|
||||||
Lat: "55.878",
|
Lat: "55.878",
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := api.AddressByCoordinates(context.Background(), ¶ms)
|
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||||
|
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
s.Empty(res)
|
s.Empty(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithoutLatitude() {
|
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithoutLatitude() {
|
||||||
api := NewGeolocateApi()
|
api := NewSuggestApi()
|
||||||
|
|
||||||
params := geolocate.RequestParams{
|
params := suggest.GeolocateParams{
|
||||||
Lon: "37.653",
|
Lon: "37.653",
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := api.AddressByCoordinates(context.Background(), ¶ms)
|
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||||
|
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
s.Empty(res)
|
s.Empty(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithEmptyBody() {
|
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithEmptyBody() {
|
||||||
api := NewGeolocateApi()
|
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.NoError(err)
|
||||||
s.Empty(res)
|
s.Empty(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithLanguageParamRU() {
|
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithLanguageParamRU() {
|
||||||
api := NewGeolocateApi()
|
api := NewSuggestApi()
|
||||||
|
|
||||||
params := geolocate.RequestParams{
|
params := suggest.GeolocateParams{
|
||||||
Lat: "55.878",
|
Lat: "55.878",
|
||||||
Lon: "37.653",
|
Lon: "37.653",
|
||||||
Language: "RU",
|
Language: "RU",
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := api.AddressByCoordinates(context.Background(), ¶ms)
|
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||||
|
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
s.NotEmpty(res)
|
s.NotEmpty(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithLanguageParamEN() {
|
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithLanguageParamEN() {
|
||||||
api := NewGeolocateApi()
|
api := NewSuggestApi()
|
||||||
|
|
||||||
params := geolocate.RequestParams{
|
params := suggest.GeolocateParams{
|
||||||
Lat: "55.878",
|
Lat: "55.878",
|
||||||
Lon: "37.653",
|
Lon: "37.653",
|
||||||
Language: "EN",
|
Language: "EN",
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := api.AddressByCoordinates(context.Background(), ¶ms)
|
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||||
|
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
s.NotEmpty(res)
|
s.NotEmpty(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithCountTwo() {
|
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithCountTwo() {
|
||||||
api := NewGeolocateApi()
|
api := NewSuggestApi()
|
||||||
reqElementCount := 2
|
reqElementCount := 2
|
||||||
|
|
||||||
params := geolocate.RequestParams{
|
params := suggest.GeolocateParams{
|
||||||
Lat: "55.878",
|
Lat: "55.878",
|
||||||
Lon: "37.653",
|
Lon: "37.653",
|
||||||
Language: "EN",
|
Language: "EN",
|
||||||
Count: strconv.Itoa(reqElementCount),
|
Count: strconv.Itoa(reqElementCount),
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := api.AddressByCoordinates(context.Background(), ¶ms)
|
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||||
|
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
s.Len(res, 2)
|
s.Len(res, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiGeolocateIntegrationTest) TestAddressByCoordinatesWithRadius() {
|
func (s *ApiSuggestIntegrationTest) TestGeoLocateWithRadius() {
|
||||||
api := NewGeolocateApi()
|
api := NewSuggestApi()
|
||||||
|
|
||||||
params := geolocate.RequestParams{
|
params := suggest.GeolocateParams{
|
||||||
Lat: "55.878",
|
Lat: "55.878",
|
||||||
Lon: "37.653",
|
Lon: "37.653",
|
||||||
Language: "EN",
|
Language: "EN",
|
||||||
RadiusMeters: "100",
|
RadiusMeters: "100",
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := api.AddressByCoordinates(context.Background(), ¶ms)
|
res, err := api.GeoLocate(context.Background(), ¶ms)
|
||||||
|
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
s.NotEmpty(res)
|
s.NotEmpty(res)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user