Skip to content

Commit 63aded6

Browse files
authored
Merge pull request #24 from grafana/matiasb/client-passing-grafana-url
Add option to instantiate client passing grafana URL
2 parents 2aeb52c + 60304e7 commit 63aded6

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

client.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Client struct {
4040
client *retryablehttp.Client
4141
token string
4242
baseURL *url.URL
43+
grafanaURL *url.URL
4344
disableRetries bool
4445
limiter *rate.Limiter
4546
UserAgent string
@@ -59,6 +60,22 @@ type Client struct {
5960
UserNotificationRules *UserNotificationRuleService
6061
}
6162

63+
func NewWithGrafanaURL(base_url, token, grafana_url string) (*Client, error) {
64+
if token == "" {
65+
return nil, fmt.Errorf("Token required")
66+
}
67+
68+
if base_url == "" {
69+
return nil, fmt.Errorf("BaseUrl required")
70+
}
71+
client, err := newClient(base_url, grafana_url)
72+
if err != nil {
73+
return nil, err
74+
}
75+
client.token = token
76+
return client, nil
77+
}
78+
6279
func New(base_url, token string) (*Client, error) {
6380
if token == "" {
6481
return nil, fmt.Errorf("Token required")
@@ -67,15 +84,15 @@ func New(base_url, token string) (*Client, error) {
6784
if base_url == "" {
6885
return nil, fmt.Errorf("BaseUrl required")
6986
}
70-
client, err := newClient(base_url)
87+
client, err := newClient(base_url, "")
7188
if err != nil {
7289
return nil, err
7390
}
7491
client.token = token
7592
return client, nil
7693
}
7794

78-
func newClient(url string) (*Client, error) {
95+
func newClient(url, grafana_url string) (*Client, error) {
7996
c := &Client{}
8097

8198
// Configure the HTTP client.
@@ -96,6 +113,12 @@ func newClient(url string) (*Client, error) {
96113
if err != nil {
97114
return nil, err
98115
}
116+
117+
err = c.setGrafanaURL(grafana_url)
118+
if err != nil {
119+
return nil, err
120+
}
121+
99122
c.UserAgent = defaultUserAgent
100123

101124
// Create services. Keep in sync with Client struct
@@ -135,6 +158,18 @@ func (c *Client) setBaseURL(urlStr string) error {
135158
return nil
136159
}
137160

161+
func (c *Client) setGrafanaURL(urlStr string) error {
162+
if urlStr != "" {
163+
grafanaUrl, err := url.Parse(urlStr)
164+
if err != nil {
165+
return err
166+
}
167+
c.grafanaURL = grafanaUrl
168+
}
169+
170+
return nil
171+
}
172+
138173
func (c *Client) NewRequest(method, path string, opt interface{}) (*retryablehttp.Request, error) {
139174
u := *c.baseURL
140175
unescaped, err := url.PathUnescape(path)
@@ -147,6 +182,9 @@ func (c *Client) NewRequest(method, path string, opt interface{}) (*retryablehtt
147182
reqHeaders := make(http.Header)
148183
reqHeaders.Set("Accept", "application/json")
149184
reqHeaders.Set("Authorization", c.token)
185+
if c.grafanaURL != nil {
186+
reqHeaders.Set("X-Grafana-URL", c.grafanaURL.String())
187+
}
150188
if c.UserAgent != "" {
151189
reqHeaders.Set("User-Agent", c.UserAgent)
152190
}
@@ -318,3 +356,11 @@ func (c *Client) BaseURL() *url.URL {
318356
u := *c.baseURL
319357
return &u
320358
}
359+
360+
func (c *Client) GrafanaURL() *url.URL {
361+
if c.grafanaURL == nil {
362+
return nil
363+
}
364+
u := *c.grafanaURL
365+
return &u
366+
}

client_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,55 @@ func TestNewClient(t *testing.T) {
4747
}
4848
}
4949

50+
func TestNewClientWithGrafanaURL(t *testing.T) {
51+
c, err := NewWithGrafanaURL("base_url", "token", "grafana_url")
52+
if err != nil {
53+
t.Fatalf("Failed to create client: %v", err)
54+
}
55+
56+
expectedBaseURL := "base_url/" + apiVersionPath
57+
58+
if c.BaseURL().String() != expectedBaseURL {
59+
t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), expectedBaseURL)
60+
}
61+
62+
if c.GrafanaURL().String() != "grafana_url" {
63+
t.Errorf("NewClient GrafanaURL is %s, want grafana_url", c.GrafanaURL().String())
64+
}
65+
}
66+
67+
func TestCheckRequest(t *testing.T) {
68+
c, err := New("base_url", "token")
69+
if err != nil {
70+
t.Fatalf("Failed to create client: %v", err)
71+
}
72+
73+
req, err := c.NewRequest("GET", "test", nil)
74+
if err != nil {
75+
t.Fatalf("Failed to create request: %v", err)
76+
}
77+
78+
if req.Header.Get("X-Grafana-URL") != "" {
79+
t.Errorf("X-Grafana-URL should not be set: %s", req.Header.Get("X-Grafana-URL"))
80+
}
81+
}
82+
83+
func TestCheckRequestSettingGrafanaURL(t *testing.T) {
84+
c, err := NewWithGrafanaURL("base_url", "token", "grafana_url")
85+
if err != nil {
86+
t.Fatalf("Failed to create client: %v", err)
87+
}
88+
89+
req, err := c.NewRequest("GET", "test", nil)
90+
if err != nil {
91+
t.Fatalf("Failed to create request: %v", err)
92+
}
93+
94+
if req.Header.Get("X-Grafana-URL") != "grafana_url" {
95+
t.Errorf("X-Grafana-URL is not set correctly: %s", req.Header.Get("X-Grafana-URL"))
96+
}
97+
}
98+
5099
func TestCheckResponse(t *testing.T) {
51100
c, err := New("base_url", "token")
52101
if err != nil {

integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var testIntegrationBody = `{
110110
"mobile_app":{
111111
"title":null,
112112
"message":null
113-
},
113+
}
114114
}
115115
}`
116116

on_call_shift_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ var byDay = []string{"MO", "FR"}
1313
var interval = 2
1414
var users = []string{"U4DNY931HHJS5", "U6RV9WPSL6DFW"}
1515

16+
var until = "2020-09-05T13:00:00"
17+
1618
var testOnCallShift = &OnCallShift{
1719
ID: "OH3V5FYQEYJ6M",
1820
TeamId: "T3HRAP3K3IKOP",
1921
Name: "Test On-Call Shift",
2022
Type: "recurrent_event",
2123
Start: "2020-09-04T13:00:00",
22-
Until: "2020-09-05T13:00:00",
24+
Until: &until,
2325
Level: 0,
2426
Duration: 7200,
2527
Frequency: &frequency,

0 commit comments

Comments
 (0)