-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
79 lines (72 loc) · 3.06 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package osm
import (
"context"
"fmt"
"net/url"
)
const (
OsmTypeRelation = "R"
OsmTypeNode = "N"
)
// Reverse performs a reverse geocoding request to convert latitude and longitude into a human-readable address.
// It takes a context, latitude, longitude, and optional functions for additional configurations.
// It returns a ReverseResult and an error if the request fails.
func Reverse(ctx context.Context, lat float64, long float64, opts ...optFunc) (*ReverseResult, error) {
urlVals := url.Values{}
urlVals.Set("format", "json")
urlVals.Set("lat", fmt.Sprintf("%f", lat))
urlVals.Set("lon", fmt.Sprintf("%f", long))
return runRequest[ReverseResult](ctx, apiUrl+"reverse?"+urlVals.Encode(), opts...)
}
// Details retrieves detailed information about a specific OpenStreetMap object.
// It takes a context, OSM type, OSM ID, and optional functions for additional configurations.
// It returns a DetailsResult and an error if the request fails.
func Details(ctx context.Context, osmType string, osmID int, opts ...optFunc) (*DetailsResult, error) {
urlVals := url.Values{}
urlVals.Set("format", "json")
urlVals.Set("osmtype", osmType)
urlVals.Set("osmid", fmt.Sprintf("%d", osmID))
return runRequest[DetailsResult](ctx, apiUrl+"details?"+urlVals.Encode(), opts...)
}
// DetailsWithPlaceID retrieves detailed information about a specific place using its place ID.
// It takes a context, place ID, and optional functions for additional configurations.
// It returns a DetailsResult and an error if the request fails.
func DetailsWithPlaceID(ctx context.Context, placeID int, opts ...optFunc) (*DetailsResult, error) {
urlVals := url.Values{}
urlVals.Set("format", "json")
urlVals.Set("place_id", fmt.Sprintf("%d", placeID))
return runRequest[DetailsResult](ctx, apiUrl+"details?"+urlVals.Encode(), opts...)
}
// Search performs a search query to find places matching the given query string.
// It takes a context, query string, and optional functions for additional configurations.
// It returns a slice of SearchResult and an error if the request fails.
func Search(ctx context.Context, q string, opts ...optFunc) ([]SearchResult, error) {
urlVals := url.Values{}
urlVals.Set("format", "json")
urlVals.Set("q", q)
res, err := runRequest[[]SearchResult](ctx, apiUrl+"search?"+urlVals.Encode(), opts...)
if err != nil {
return nil, err
}
if res == nil {
return []SearchResult{}, nil
}
return *res, nil
}
// Lookup retrieves information about a specific OpenStreetMap object using its type and ID.
// It takes a context, OSM type, OSM ID, and optional functions for additional configurations.
// It returns a LookupResult and an error if the request fails.
func Lookup(ctx context.Context, osmType string, osmID int, opts ...optFunc) ([]LookupResult, error) {
urlVals := url.Values{}
urlVals.Set("format", "json")
urlVals.Set("osm_type", osmType)
urlVals.Set("osm_id", fmt.Sprintf("%d", osmID))
res, err := runRequest[[]LookupResult](ctx, apiUrl+"lookup?"+urlVals.Encode(), opts...)
if err != nil {
return nil, err
}
if res == nil {
return []LookupResult{}, nil
}
return *res, nil
}