diff --git a/api/geolocate/geolocate.go b/api/geolocate/geolocate.go new file mode 100644 index 0000000..26bef24 --- /dev/null +++ b/api/geolocate/geolocate.go @@ -0,0 +1,30 @@ +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 + } +) + +// AddressByGeolocate try to return suggest addresses by params +// see documentation on:https://dadata.ru/api/geolocate/ +func (a *Api) AddressByGeolocate(ctx context.Context, params *RequestParams) (ret []*AddressGeolocateSuggestion, err error) { + var result = &AddressGeolocateResponse{} + 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/geolocate/model.go new file mode 100644 index 0000000..09092f1 --- /dev/null +++ b/api/geolocate/model.go @@ -0,0 +1,27 @@ +package geolocate + +import "github.com/ekomobile/dadata/v2/api/model" + +type ( + // RequestParams Request struct + // full documentation https://confluence.hflabs.ru/pages/viewpage.action?pageId=808583277 + RequestParams struct { + Lat string `json:"lat"` // geographic latitude + Lon string `json:"lon"` // geographic longitude + Count string `json:"count,omitempty"` // number of results (max 20) + RadiusMeters string `json:"radius_meters,omitempty"` // search radius in metres (max. 1000) + Language string `json:"language,omitempty"` // in which language to return the result (ru / en) + } + + // AddressGeolocateSuggestion api response for address + AddressGeolocateSuggestion struct { + Value string `json:"value"` + UnrestrictedValue string `json:"unrestricted_value"` + Data *model.Address `json:"data"` + } + + // AddressGeolocateResponse result slice for address suggestions + AddressGeolocateResponse struct { + Suggestions []*AddressGeolocateSuggestion `json:"suggestions"` + } +) diff --git a/api/model/model.go b/api/model/model.go index 3d5fb08..d8ae48b 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 datdata.AddressByGeolocate Address struct { Source string `json:"source"` // Исходный адрес одной строкой Result string `json:"result"` // Стандартизованный адрес одной строкой diff --git a/dadata.go b/dadata.go index 266595e..a4818a8 100644 --- a/dadata.go +++ b/dadata.go @@ -4,6 +4,7 @@ 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" @@ -15,6 +16,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/" ) @@ -58,6 +60,12 @@ 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{