Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit db79a60

Browse files
committed
Add support for providing additional HTTP headers
1 parent 249aba6 commit db79a60

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ type Client struct {
2121
key string
2222
baseURL url.URL
2323
*http.Client
24+
headers map[string]string
2425
}
2526

2627
//New creates a new grafana client
2728
//auth can be in user:pass format, or it can be an api key
28-
func New(auth, baseURL string) (*Client, error) {
29+
//headers specifies additional HTTP headers, or nil
30+
func New(auth string, baseURL string, headers map[string]string) (*Client, error) {
2931
u, err := url.Parse(baseURL)
3032
if err != nil {
3133
return nil, err
@@ -41,6 +43,7 @@ func New(auth, baseURL string) (*Client, error) {
4143
key,
4244
*u,
4345
cleanhttp.DefaultClient(),
46+
headers,
4447
}, nil
4548
}
4649

@@ -91,6 +94,11 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i
9194
if c.key != "" {
9295
req.Header.Add("Authorization", c.key)
9396
}
97+
if c.headers != nil {
98+
for k, v := range c.headers {
99+
req.Header.Add(k, v)
100+
}
101+
}
94102

95103
if os.Getenv("GF_LOG") != "" {
96104
if body == nil {

mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ func gapiTestTools(code int, body string) (*mockServer, *Client) {
4040
Host: "my-grafana.com",
4141
}
4242

43-
client := &Client{"my-key", url, httpClient}
43+
client := &Client{"my-key", url, httpClient, nil}
4444
return mock, client
4545
}

0 commit comments

Comments
 (0)