2026-02-14 15:40:12 +00:00
|
|
|
package stadia
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-02-25 02:44:35 +00:00
|
|
|
"io"
|
2026-02-14 15:40:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type BulkGeocodeQuery interface {
|
|
|
|
|
endpoint() string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BulkGeocodeRequestItem represents a single request in a bulk geocoding operation
|
|
|
|
|
type BulkGeocodeRequestItem struct {
|
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
|
Query BulkGeocodeQuery `json:"query"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BulkGeocodeResponseItem represents a single response in a bulk geocoding operation
|
|
|
|
|
type BulkGeocodeResponseItem struct {
|
|
|
|
|
Response *GeocodeResponse `json:"response,omitempty"`
|
|
|
|
|
Status int `json:"status"`
|
|
|
|
|
Message string `json:"msg,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StadiaMaps) BulkGeocode(requests []BulkGeocodeQuery) ([]BulkGeocodeResponseItem, error) {
|
|
|
|
|
// https://docs.stadiamaps.com/geocoding-search-autocomplete/bulk-geocoding-search/
|
|
|
|
|
// POST 'https://api.stadiamaps.com/geocoding/v1/search/bulk?api_key=YOUR-API-KEY'
|
|
|
|
|
body := make([]BulkGeocodeRequestItem, 0)
|
|
|
|
|
for _, r := range requests {
|
|
|
|
|
body = append(body, BulkGeocodeRequestItem{
|
|
|
|
|
Endpoint: r.endpoint(),
|
|
|
|
|
Query: r,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
var results []BulkGeocodeResponseItem
|
2026-02-25 02:44:35 +00:00
|
|
|
var api_error Error
|
2026-02-14 15:40:12 +00:00
|
|
|
resp, err := s.client.R().
|
|
|
|
|
SetBody(body).
|
|
|
|
|
SetContentType("application/json").
|
2026-04-16 20:37:49 +00:00
|
|
|
SetPathParam("urlBase", s.urlBaseApi).
|
2026-02-14 15:40:12 +00:00
|
|
|
SetQueryParam("api_key", s.APIKey).
|
2026-02-25 02:44:35 +00:00
|
|
|
SetError(&api_error).
|
2026-02-14 15:40:12 +00:00
|
|
|
SetResult(&results).
|
|
|
|
|
Post("https://{urlBase}/geocoding/v1/search/bulk")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("bulk geocode request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !resp.IsSuccess() {
|
2026-02-25 02:44:35 +00:00
|
|
|
if api_error.Error() != "" {
|
|
|
|
|
return nil, &api_error
|
|
|
|
|
}
|
|
|
|
|
content, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("read all failure: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("bulk geocoding request failed with status code: %d: %s", resp.StatusCode(), content)
|
2026-02-14 15:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|