forked from signalfx/signalfx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
104 lines (89 loc) · 2.81 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
package signalfx
import (
"context"
"io"
"net/http"
"net/url"
stdpath "path"
"time"
)
// DefaultAPIURL is the default URL for making API requests
const DefaultAPIURL = "https://api.signalfx.com"
// AuthHeaderKey is the HTTP header used to pass along the auth token
// Note that while HTTP headers are case insensitive this header is case
// sensitive on the tests for convenience.
const AuthHeaderKey = "X-Sf-Token"
// Client is a SignalFx API client.
type Client struct {
baseURL string
httpClient *http.Client
authToken string
userAgent string
}
// ClientParam is an option for NewClient. Its implementation borrows
// from Dave Cheney's functional options API
// (https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis).
type ClientParam func(*Client) error
// NewClient creates a new SignalFx client using the specified token.
func NewClient(token string, options ...ClientParam) (*Client, error) {
client := &Client{
baseURL: DefaultAPIURL,
httpClient: &http.Client{
Timeout: time.Second * 30,
},
authToken: token,
userAgent: "signalfx-go",
}
for _, option := range options {
option(client)
}
return client, nil
}
// APIUrl sets the URL that our client will communicate with, allowing
// it to be adjusted to another URL for testing or communication with other
// SignalFx clusters. Example `"https://api.signalfx.com"`.
func APIUrl(apiURL string) ClientParam {
return func(client *Client) error {
client.baseURL = apiURL
return nil
}
}
// UserAgent sets the UserAgent string to include with the request.
func UserAgent(userAgent string) ClientParam {
return func(client *Client) error {
client.userAgent = userAgent
return nil
}
}
// HTTPClient sets the `http.Client` that this API client will use to
// to communicate. This allows you to replace the client or tune it to your
// needs.
func HTTPClient(httpClient *http.Client) ClientParam {
return func(client *Client) error {
client.httpClient = httpClient
return nil
}
}
func (c *Client) doRequest(ctx context.Context, method string, path string, params url.Values, body io.Reader) (*http.Response, error) {
return c.doRequestWithToken(ctx, method, path, params, body, c.authToken)
}
func (c *Client) doRequestWithToken(ctx context.Context, method string, path string, params url.Values, body io.Reader, token string) (*http.Response, error) {
destURL, err := url.Parse(c.baseURL)
if err != nil {
return nil, err
}
destURL.Path = stdpath.Join(destURL.Path, path)
if params != nil {
destURL.RawQuery = params.Encode()
}
req, err := http.NewRequestWithContext(ctx, method, destURL.String(), body)
if token != "" {
req.Header.Set(AuthHeaderKey, token)
}
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Content-Type", "application/json")
if err != nil {
return nil, err
}
return c.httpClient.Do(req)
}