Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Config struct {
APIKey string
// BasicAuth is optional basic auth credentials.
BasicAuth *url.Userinfo
// HTTPHeaders are optional HTTP headers.
HTTPHeaders map[string]string
// Client provides an optional HTTP client, otherwise a default will be used.
Client *http.Client
// OrgID provides an optional organization ID, ignored when using APIKey, BasicAuth defaults to last used org
Expand Down Expand Up @@ -109,6 +111,11 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i
} else if c.config.OrgID != 0 {
req.Header.Add("X-Grafana-Org-Id", strconv.FormatInt(c.config.OrgID, 10))
}
if c.config.HTTPHeaders != nil {
for k, v := range c.config.HTTPHeaders {
req.Header.Add(k, v)
}
}

if os.Getenv("GF_LOG") != "" {
if body == nil {
Expand Down
17 changes: 17 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ func TestNew_orgID(t *testing.T) {
}
}

func TestNew_HTTPHeaders(t *testing.T) {
const key = "foo"
headers := map[string]string{key: "bar"}
c, err := New("http://my-grafana.com", Config{HTTPHeaders: headers})
if err != nil {
t.Fatalf("expected error to be nil; got: %s", err.Error())
}

value, ok := c.config.HTTPHeaders[key]
if !ok {
t.Errorf("expected error: %v; got: %v", headers, c.config.HTTPHeaders)
}
if value != headers[key] {
t.Errorf("expected error: %s; got: %s", headers[key], value)
}
}

func TestNew_invalidURL(t *testing.T) {
_, err := New("://my-grafana.com", Config{APIKey: "123"})

Expand Down