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

Add organization ID to client connection using header. #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
type Client struct {
key string
baseURL url.URL
orgID string
*http.Client
}

//New creates a new grafana client
//auth can be in user:pass format, or it can be an api key
func New(auth, baseURL string) (*Client, error) {
func New(auth, baseURL string, orgID int) (*Client, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
Expand All @@ -40,6 +41,7 @@ func New(auth, baseURL string) (*Client, error) {
return &Client{
key,
*u,
fmt.Sprintf("%d", orgID),
cleanhttp.DefaultClient(),
}, nil
}
Expand Down Expand Up @@ -88,6 +90,9 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i
if err != nil {
return req, err
}

req.Header.Add("X-Grafana-Org-Id", c.orgID)

if c.key != "" {
req.Header.Add("Authorization", c.key)
}
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

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

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

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

expected := "parse \"://my-grafana.com\": missing protocol scheme"
if err.Error() != expected {
Expand Down
2 changes: 1 addition & 1 deletion mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func gapiTestTools(code int, body string) (*mockServer, *Client) {
Host: "my-grafana.com",
}

client := &Client{"my-key", url, httpClient}
client := &Client{"my-key", url, "1", httpClient}
return mock, client
}