Skip to content

Commit

Permalink
Merge pull request #24 from grafana/matiasb/client-passing-grafana-url
Browse files Browse the repository at this point in the history
Add option to instantiate client passing grafana URL
  • Loading branch information
matiasb authored Nov 28, 2024
2 parents 2aeb52c + 60304e7 commit 63aded6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
50 changes: 48 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -67,15 +84,15 @@ 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
}
client.token = token
return client, nil
}

func newClient(url string) (*Client, error) {
func newClient(url, grafana_url string) (*Client, error) {
c := &Client{}

// Configure the HTTP client.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
49 changes: 49 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var testIntegrationBody = `{
"mobile_app":{
"title":null,
"message":null
},
}
}
}`

Expand Down
4 changes: 3 additions & 1 deletion on_call_shift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 63aded6

Please sign in to comment.