diff --git a/client.go b/client.go index b6af886..054e045 100644 --- a/client.go +++ b/client.go @@ -40,6 +40,7 @@ type Client struct { client *retryablehttp.Client token string baseURL *url.URL + grafanaURL *url.URL disableRetries bool limiter *rate.Limiter UserAgent string @@ -59,6 +60,22 @@ type Client struct { UserNotificationRules *UserNotificationRuleService } +func NewWithGrafanaURL(base_url, token, grafana_url string) (*Client, error) { + if token == "" { + return nil, fmt.Errorf("Token required") + } + + if base_url == "" { + return nil, fmt.Errorf("BaseUrl required") + } + client, err := newClient(base_url, grafana_url) + if err != nil { + return nil, err + } + client.token = token + return client, nil +} + func New(base_url, token string) (*Client, error) { if token == "" { return nil, fmt.Errorf("Token required") @@ -67,7 +84,7 @@ func New(base_url, token string) (*Client, error) { if base_url == "" { return nil, fmt.Errorf("BaseUrl required") } - client, err := newClient(base_url) + client, err := newClient(base_url, "") if err != nil { return nil, err } @@ -75,7 +92,7 @@ func New(base_url, token string) (*Client, error) { return client, nil } -func newClient(url string) (*Client, error) { +func newClient(url, grafana_url string) (*Client, error) { c := &Client{} // Configure the HTTP client. @@ -96,6 +113,12 @@ func newClient(url string) (*Client, error) { if err != nil { return nil, err } + + err = c.setGrafanaURL(grafana_url) + if err != nil { + return nil, err + } + c.UserAgent = defaultUserAgent // Create services. Keep in sync with Client struct @@ -135,6 +158,18 @@ func (c *Client) setBaseURL(urlStr string) error { return nil } +func (c *Client) setGrafanaURL(urlStr string) error { + if urlStr != "" { + grafanaUrl, err := url.Parse(urlStr) + if err != nil { + return err + } + c.grafanaURL = grafanaUrl + } + + return nil +} + func (c *Client) NewRequest(method, path string, opt interface{}) (*retryablehttp.Request, error) { u := *c.baseURL unescaped, err := url.PathUnescape(path) @@ -147,6 +182,9 @@ func (c *Client) NewRequest(method, path string, opt interface{}) (*retryablehtt reqHeaders := make(http.Header) reqHeaders.Set("Accept", "application/json") reqHeaders.Set("Authorization", c.token) + if c.grafanaURL != nil { + reqHeaders.Set("X-Grafana-URL", c.grafanaURL.String()) + } if c.UserAgent != "" { reqHeaders.Set("User-Agent", c.UserAgent) } @@ -318,3 +356,11 @@ func (c *Client) BaseURL() *url.URL { u := *c.baseURL return &u } + +func (c *Client) GrafanaURL() *url.URL { + if c.grafanaURL == nil { + return nil + } + u := *c.grafanaURL + return &u +} diff --git a/client_test.go b/client_test.go index b6b5c96..5efd0c6 100644 --- a/client_test.go +++ b/client_test.go @@ -47,6 +47,55 @@ func TestNewClient(t *testing.T) { } } +func TestNewClientWithGrafanaURL(t *testing.T) { + c, err := NewWithGrafanaURL("base_url", "token", "grafana_url") + if err != nil { + t.Fatalf("Failed to create client: %v", err) + } + + expectedBaseURL := "base_url/" + apiVersionPath + + if c.BaseURL().String() != expectedBaseURL { + t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), expectedBaseURL) + } + + if c.GrafanaURL().String() != "grafana_url" { + t.Errorf("NewClient GrafanaURL is %s, want grafana_url", c.GrafanaURL().String()) + } +} + +func TestCheckRequest(t *testing.T) { + c, err := New("base_url", "token") + if err != nil { + t.Fatalf("Failed to create client: %v", err) + } + + req, err := c.NewRequest("GET", "test", nil) + if err != nil { + t.Fatalf("Failed to create request: %v", err) + } + + if req.Header.Get("X-Grafana-URL") != "" { + t.Errorf("X-Grafana-URL should not be set: %s", req.Header.Get("X-Grafana-URL")) + } +} + +func TestCheckRequestSettingGrafanaURL(t *testing.T) { + c, err := NewWithGrafanaURL("base_url", "token", "grafana_url") + if err != nil { + t.Fatalf("Failed to create client: %v", err) + } + + req, err := c.NewRequest("GET", "test", nil) + if err != nil { + t.Fatalf("Failed to create request: %v", err) + } + + if req.Header.Get("X-Grafana-URL") != "grafana_url" { + t.Errorf("X-Grafana-URL is not set correctly: %s", req.Header.Get("X-Grafana-URL")) + } +} + func TestCheckResponse(t *testing.T) { c, err := New("base_url", "token") if err != nil { diff --git a/integration_test.go b/integration_test.go index 92b16d4..95b0a67 100644 --- a/integration_test.go +++ b/integration_test.go @@ -110,7 +110,7 @@ var testIntegrationBody = `{ "mobile_app":{ "title":null, "message":null - }, + } } }` diff --git a/on_call_shift_test.go b/on_call_shift_test.go index a74cedd..9e0f755 100644 --- a/on_call_shift_test.go +++ b/on_call_shift_test.go @@ -13,13 +13,15 @@ var byDay = []string{"MO", "FR"} var interval = 2 var users = []string{"U4DNY931HHJS5", "U6RV9WPSL6DFW"} +var until = "2020-09-05T13:00:00" + var testOnCallShift = &OnCallShift{ ID: "OH3V5FYQEYJ6M", TeamId: "T3HRAP3K3IKOP", Name: "Test On-Call Shift", Type: "recurrent_event", Start: "2020-09-04T13:00:00", - Until: "2020-09-05T13:00:00", + Until: &until, Level: 0, Duration: 7200, Frequency: &frequency,