-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.go
59 lines (52 loc) · 1.38 KB
/
parameters.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
package openaq
import (
"context"
"fmt"
"net/url"
)
type ParametersArgs struct {
BaseArgs BaseArgs
ParameterType string
}
func (parametersArgs *ParametersArgs) Values(q url.Values) (url.Values, error) {
if parametersArgs.ParameterType != "" {
q.Add("parameter_type", parametersArgs.ParameterType)
}
return q, nil
}
// QueryParams translates ParametersArgs struct into url.Values
func (parametersArgs ParametersArgs) QueryParams() (url.Values, error) {
q := make(url.Values)
q, err := parametersArgs.BaseArgs.Values(q)
if err != nil {
return nil, err
}
q, err = parametersArgs.Values(q)
if err != nil {
return nil, err
}
return q, nil
}
// GetParameters fetches all parameters filtered by any params passed.
func (c *Client) GetParameters(ctx context.Context, args ParametersArgs) (*ParametersResponse, error) {
resp := &ParametersResponse{}
queryParams, err := args.QueryParams()
if err != nil {
return nil, err
}
err = c.request(ctx, "GET", "/parameters", queryParams, resp)
if err != nil {
return nil, err
}
return resp, nil
}
// GetParameter fetches a single parameter by ID.
func (c *Client) GetParameter(ctx context.Context, parametersID int64) (*ParametersResponse, error) {
path := fmt.Sprintf("/parameters/%d", parametersID)
resp := &ParametersResponse{}
err := c.request(ctx, "GET", path, nil, resp)
if err != nil {
return nil, err
}
return resp, nil
}