Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit a8f55d1

Browse files
committed
Add support for providing additional HTTP headers
1 parent 8a5c1ba commit a8f55d1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Config struct {
2929
APIKey string
3030
// BasicAuth is optional basic auth credentials.
3131
BasicAuth *url.Userinfo
32+
// HTTPHeaders are optional HTTP headers.
33+
HTTPHeaders map[string]string
3234
// Client provides an optional HTTP client, otherwise a default will be used.
3335
Client *http.Client
3436
// OrgID provides an optional organization ID, ignored when using APIKey, BasicAuth defaults to last used org
@@ -109,6 +111,11 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i
109111
} else if c.config.OrgID != 0 {
110112
req.Header.Add("X-Grafana-Org-Id", strconv.FormatInt(c.config.OrgID, 10))
111113
}
114+
if c.config.HTTPHeaders != nil {
115+
for k, v := range c.config.HTTPHeaders {
116+
req.Header.Add(k, v)
117+
}
118+
}
112119

113120
if os.Getenv("GF_LOG") != "" {
114121
if body == nil {

client_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ func TestNew_orgID(t *testing.T) {
5353
}
5454
}
5555

56+
func TestNew_HTTPHeaders(t *testing.T) {
57+
const key = "foo"
58+
headers := map[string]string {key: "bar"}
59+
c, err := New("http://my-grafana.com", Config{HTTPHeaders: headers})
60+
if err != nil {
61+
t.Fatalf("expected error to be nil; got: %s", err.Error())
62+
}
63+
64+
value, ok := c.config.HTTPHeaders[key]
65+
if !ok {
66+
t.Errorf("expected error: %v; got: %v", headers, c.config.HTTPHeaders)
67+
}
68+
if value != headers[key] {
69+
t.Errorf("expected error: %s; got: %s", headers[key], value)
70+
}
71+
}
72+
5673
func TestNew_invalidURL(t *testing.T) {
5774
_, err := New("://my-grafana.com", Config{APIKey: "123"})
5875

0 commit comments

Comments
 (0)