-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocations.go
107 lines (101 loc) · 3.16 KB
/
locations.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package openaq
import (
"context"
"fmt"
"net/url"
"strconv"
)
type LocationArgs struct {
//
BaseArgs *BaseArgs
// Coordinates represents latitude,longitude (y,x) values to center the search
// must be paired with Radius
Coordinates *CoordinatesArgs
// Radius represents the distance in meters from the Coordinates position
// to search, must be paired with Coordinates
Radius int64
// Bbox is a bounding box to search within and is represented as a slice of four coordinates
// in the form [ymin xmin ymax xmax]
Bbox []float64
// IsoCode is the ISO 3166-1 alpha-2 country code, unlike countries ID ISO code searching can only accept
// a single value and not a slice of values
IsoCode string
// Monitor allows filtering for reference grade/regulatory monitors vs air sensors (AKA low-cost sensors)
Monitor *bool
// Mobile allowing filtering for mobile locations vs stationary locations
Mobile bool
// A slice of owners IDs
OwnersIDs []int64
//
Countries *Countries
//
Providers *Providers
}
func (locationArgs *LocationArgs) Values(q url.Values) (url.Values, error) {
if locationArgs.Coordinates != nil {
lat := strconv.FormatFloat(locationArgs.Coordinates.Lat, 'f', -1, 64)
lon := strconv.FormatFloat(locationArgs.Coordinates.Lon, 'f', -1, 64)
coords := fmt.Sprintf("%s,%s", lat, lon)
q.Add("coordinates", coords)
}
if locationArgs.Countries != nil {
q = locationArgs.Countries.Values(q)
}
if locationArgs.Providers != nil {
q = locationArgs.Providers.Values(q)
}
if locationArgs.Radius != 0 {
q.Add("radius", strconv.Itoa(int(locationArgs.Radius)))
}
if locationArgs.IsoCode != "" {
q.Add("iso", locationArgs.IsoCode)
}
if locationArgs.Monitor != nil {
q.Add("monitor", strconv.FormatBool(*locationArgs.Monitor))
}
if locationArgs.Bbox != nil {
minX := strconv.FormatFloat(locationArgs.Bbox[0], 'f', -1, 64)
minY := strconv.FormatFloat(locationArgs.Bbox[1], 'f', -1, 64)
maxX := strconv.FormatFloat(locationArgs.Bbox[2], 'f', -1, 64)
maxY := strconv.FormatFloat(locationArgs.Bbox[3], 'f', -1, 64)
bboxValue := fmt.Sprintf("%s,%s,%s,%s", minX, minY, maxX, maxY)
q.Add("bbox", bboxValue)
}
return q, nil
}
// QueryParams translates LocationArgs struct into url.Values
func (locationArgs LocationArgs) QueryParams() (url.Values, error) {
q := make(url.Values)
q, err := locationArgs.BaseArgs.Values(q)
if err != nil {
return nil, err
}
q, err = locationArgs.Values(q)
if err != nil {
return nil, err
}
return q, nil
}
// GetLocations fetches all locations filtered by any params passed.
func (c *Client) GetLocations(ctx context.Context, args LocationArgs) (*LocationsResponse, error) {
resp := &LocationsResponse{}
queryParams, err := args.QueryParams()
if err != nil {
return nil, err
}
err = c.request(ctx, "GET", "/locations", queryParams, resp)
if err != nil {
return nil, err
}
return resp, nil
}
// GetLocation fetches a single location by ID.
func (c *Client) GetLocation(ctx context.Context, locationsID int64) (*LocationsResponse, error) {
path := fmt.Sprintf("/locations/%d", locationsID)
resp := &LocationsResponse{}
err := c.request(ctx, "GET", path, nil, resp)
if err != nil {
return nil, err
}
return resp, nil
}