-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
51 lines (39 loc) · 1.29 KB
/
request.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
package ecobee
import (
"context"
"io"
"net/http"
"net/url"
"golang.org/x/net/context/ctxhttp"
)
func (c *Client) get(ctx context.Context, endpoint string, queryParameters url.Values, headers map[string][]string) (*http.Response, error) {
return c.sendRequest(ctx, http.MethodGet, endpoint, queryParameters, headers, nil)
}
func (c *Client) post(ctx context.Context, endpoint string, queryParameters url.Values, headers map[string][]string, body io.Reader) (*http.Response, error) {
return c.sendRequest(ctx, http.MethodPost, endpoint, queryParameters, headers, body)
}
func (c *Client) sendRequest(ctx context.Context, method string, endpoint string, queryParameters url.Values, headers map[string][]string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, endpoint, body)
if err != nil {
return nil, err
}
req.URL.RawQuery = queryParameters.Encode()
for k, v := range c.customHTTPHeaders {
req.Header.Set(k, v)
}
for k, v := range headers {
req.Header[k] = v
}
resp, err := c.doRequest(ctx, req)
if err != nil {
return resp, err
}
return resp, nil
}
func (c *Client) doRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
resp, err := ctxhttp.Do(ctx, c.httpClient, req)
if err != nil {
return resp, err
}
return resp, nil
}