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

Commit af5d3ac

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

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
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 {

client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestNew_basicAuth(t *testing.T) {
11-
c, err := New("user:pass", "http://my-grafana.com")
11+
c, err := New("user:pass", "http://my-grafana.com", nil)
1212
if err != nil {
1313
t.Errorf("expected error to be nil; got: %s", err.Error())
1414
}
@@ -20,7 +20,7 @@ func TestNew_basicAuth(t *testing.T) {
2020
}
2121

2222
func TestNew_tokenAuth(t *testing.T) {
23-
c, err := New("123", "http://my-grafana.com")
23+
c, err := New("123", "http://my-grafana.com", nil)
2424
if err != nil {
2525
t.Errorf("expected error to be nil; got: %s", err.Error())
2626
}
@@ -37,7 +37,7 @@ func TestNew_tokenAuth(t *testing.T) {
3737
}
3838

3939
func TestNew_invalidURL(t *testing.T) {
40-
_, err := New("123", "://my-grafana.com")
40+
_, err := New("123", "://my-grafana.com", nil)
4141

4242
expected := "parse \"://my-grafana.com\": missing protocol scheme"
4343
if err.Error() != expected {

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)