50 lines
898 B
Go
50 lines
898 B
Go
package geo
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type GeoService struct {
|
|
peliasURL string
|
|
}
|
|
|
|
func NewGeoService(peliasURL string) *GeoService {
|
|
return &GeoService{peliasURL: peliasURL}
|
|
}
|
|
|
|
type AutocompleteResult struct {
|
|
Features []any
|
|
}
|
|
|
|
func (s *GeoService) Autocomplete(text string) (*AutocompleteResult, error) {
|
|
resp, err := http.Get(fmt.Sprintf("%s/autocomplete?text=%s", s.peliasURL, text))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("Failed to read response body")
|
|
return nil, err
|
|
}
|
|
|
|
var response map[string]any
|
|
if err := json.Unmarshal(body, &response); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
features, ok := response["features"].([]any)
|
|
if !ok {
|
|
features = []any{}
|
|
}
|
|
|
|
return &AutocompleteResult{
|
|
Features: features,
|
|
}, nil
|
|
} |