-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurements.go
76 lines (70 loc) · 1.94 KB
/
measurements.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
package openaq
import (
"context"
"fmt"
"net/url"
"time"
)
type MeasurementsArgs struct {
BaseArgs BaseArgs
DatetimeFrom time.Time
DatetimeTo time.Time
Parameters *Parameters
PeriodName string
}
func (m *MeasurementsArgs) Values(q url.Values) (url.Values, error) {
if !m.DatetimeFrom.IsZero() {
q.Add("date_from", m.DatetimeFrom.UTC().Format("2006-01-02T15:04:05Z07:00"))
}
if !m.DatetimeTo.IsZero() {
q.Add("date_to", m.DatetimeTo.UTC().Format("2006-01-02T15:04:05Z07:00"))
}
if m.PeriodName != "" {
q.Add("period_name", m.PeriodName)
}
if m.Parameters != nil {
q = m.Parameters.Values(q)
}
return q, nil
}
// QueryParams translates MeasurementsArgs struct into url.Values
func (args MeasurementsArgs) QueryParams() (url.Values, error) {
q := make(url.Values)
q, err := args.BaseArgs.Values(q)
if err != nil {
return nil, err
}
q, err = args.Values(q)
if err != nil {
return nil, err
}
return q, nil
}
// GetLocationMeasurements fetches measurements for a single location by ID and any params passed
func (c *Client) GetLocationMeasurements(ctx context.Context, locationsID int64, args MeasurementsArgs) (*MeasurementsResponse, error) {
path := fmt.Sprintf("/locations/%d/measurements", locationsID)
queryParams, err := args.QueryParams()
if err != nil {
return nil, err
}
resp := &MeasurementsResponse{}
err = c.request(ctx, "GET", path, queryParams, resp)
if err != nil {
return nil, err
}
return resp, nil
}
// GetSensorMeasurements fetches measurements for a single sensor by ID and any params passed
func (c *Client) GetSensorMeasurements(ctx context.Context, sensorsID int64, args MeasurementsArgs) (*MeasurementsResponse, error) {
path := fmt.Sprintf("/sensors/%d/measurements", sensorsID)
queryParams, err := args.QueryParams()
if err != nil {
return nil, err
}
resp := &MeasurementsResponse{}
err = c.request(ctx, "GET", path, queryParams, resp)
if err != nil {
return nil, err
}
return resp, nil
}