lot of new functionalities
This commit is contained in:
50
core/utils/geo/geo.go
Normal file
50
core/utils/geo/geo.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user