-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
157 lines (133 loc) · 3.16 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package openaq
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"path"
"github.com/hashicorp/go-cleanhttp"
)
const (
defaultBaseURLScheme = "https"
defaultBaseURLHost = "api.openaq.org"
defaultBasePath = "/v3/"
defaultUserAgent = "openaq-go"
)
type Client struct {
baseURL *url.URL
apiKey string
userAgent string
httpHeaders map[string]string
client *http.Client
}
// Config contains client configuration.
type Config struct {
// BaseURLScheme is the url scheme to use defaults to https
BaseURLScheme string
// BaseURLHost is the base url to use defualts to api.openaq.org
BaseURLHost string
// APIKey is an optional API key.
APIKey string
// userAgent is an optional HTTP header.
UserAgent string
// HTTPHeaders are additional optional HTTP headers.
HttpHeaders map[string]string
// Client provides an optional HTTP client, otherwise a default will be used.
Client *http.Client
}
// New creates a new OpenAQ client.
func NewClient(config Config) (*Client, error) {
client := config.Client
if client == nil {
client = cleanhttp.DefaultClient()
}
var baseURLScheme string
if config.BaseURLScheme == "" {
baseURLScheme = defaultBaseURLScheme
} else {
baseURLScheme = config.BaseURLScheme
}
var baseURLHost string
if config.BaseURLHost == "" {
baseURLHost = defaultBaseURLHost
} else {
baseURLHost = config.BaseURLHost
}
var userAgent string
if config.UserAgent == "" {
userAgent = defaultUserAgent
} else {
userAgent = config.UserAgent
}
return &Client{
baseURL: &url.URL{
Scheme: baseURLScheme,
Host: baseURLHost,
Path: defaultBasePath,
},
apiKey: config.APIKey,
userAgent: userAgent,
httpHeaders: config.HttpHeaders,
client: client,
}, nil
}
func (c *Client) request(ctx context.Context, method, requestPath string, query url.Values, responseStruct interface{}) error {
var (
req *http.Request
resp *http.Response
err error
body []byte
)
req, err = c.newRequest(requestPath, query)
if err != nil {
return err
}
req = req.WithContext(ctx)
resp, err = c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
apiError := &APIError{Code: resp.StatusCode}
if err = json.Unmarshal(body, apiError); err != nil {
apiError.Message = string(body)
}
return apiError
}
if responseStruct == nil {
return nil
}
err = json.Unmarshal(body, responseStruct)
if err != nil {
return err
}
return nil
}
func (c *Client) newRequest(requestPath string, query url.Values) (*http.Request, error) {
url := c.baseURL
url.Path = path.Join(url.Path, requestPath)
url.RawQuery = query.Encode()
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return req, err
}
if c.apiKey != "" {
req.Header.Add("X-API-key", c.apiKey)
}
if c.userAgent != "" {
req.Header.Add("User-Agent", c.userAgent)
}
if c.httpHeaders != nil {
for k, v := range c.httpHeaders {
req.Header.Add(k, v)
}
}
req.Header.Add("Content-Type", "application/json")
return req, err
}