Skip to content

Commit 557151e

Browse files
author
Slavek Kabrda
authored
Use keys in headers for endpoints that allow this (#284)
1 parent 43491ff commit 557151e

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func (client *Client) Validate() (bool, error) {
8282
if err != nil {
8383
return false, err
8484
}
85+
req.Header.Set("DD-API-KEY", client.apiKey)
86+
req.Header.Set("DD-APPLICATION-KEY", client.appKey)
8587

8688
resp, err = client.doRequestWithRetries(req, client.RetryTimeout)
8789
if err != nil {

integration/series_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package integration
2+
3+
import (
4+
"testing"
5+
6+
dd "github.com/zorkian/go-datadog-api"
7+
)
8+
9+
func TestSeriesSubmit(t *testing.T) {
10+
metrics := []dd.Metric{{
11+
Metric: dd.String("test.metric"),
12+
Points: []dd.DataPoint{{dd.Float64(1.0), dd.Float64(2.0)}},
13+
Type: dd.String("gauge"),
14+
Host: dd.String("myhost"),
15+
Tags: []string{"some:tag"},
16+
Unit: dd.String("unit"),
17+
}}
18+
19+
err := client.PostMetrics(metrics)
20+
if err != nil {
21+
t.Fatalf("Posting metrics failed when it shouldn't. (%s)", err)
22+
}
23+
}

request.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ type Response struct {
2828
Error string `json:"error"`
2929
}
3030

31+
func (client *Client) apiAcceptsKeysInHeaders(api string) bool {
32+
for _, prefix := range []string{"/v1/series", "/v1/check_run", "/v1/events", "/v1/screen"} {
33+
if strings.HasPrefix(api, prefix) {
34+
return false
35+
}
36+
}
37+
return true
38+
}
39+
3140
// uriForAPI is to be called with either an API resource like "/v1/events"
3241
// or a full URL like the IP Ranges one
3342
// and it will give the proper request URI to be posted to.
@@ -40,8 +49,10 @@ func (client *Client) uriForAPI(api string) (string, error) {
4049
return "", err
4150
}
4251
q := apiBase.Query()
43-
q.Add("api_key", client.apiKey)
44-
q.Add("application_key", client.appKey)
52+
if !client.apiAcceptsKeysInHeaders(api) {
53+
q.Add("api_key", client.apiKey)
54+
q.Add("application_key", client.appKey)
55+
}
4556
apiBase.RawQuery = q.Encode()
4657
return apiBase.String(), nil
4758
}
@@ -233,6 +244,10 @@ func (client *Client) createRequest(method, api string, reqbody interface{}) (*h
233244
if err != nil {
234245
return nil, err
235246
}
247+
if client.apiAcceptsKeysInHeaders(api) {
248+
req.Header.Set("DD-API-KEY", client.apiKey)
249+
req.Header.Set("DD-APPLICATION-KEY", client.appKey)
250+
}
236251
if bodyReader != nil {
237252
req.Header.Add("Content-Type", "application/json")
238253
}

request_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"fmt"
55
"net/http"
66
"net/http/httptest"
7+
"net/url"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
1011
)
1112

13+
var needKeysInQueryParams = []string{"/v1/series", "/v1/check_run", "/v1/events", "/v1/screen"}
14+
1215
func TestUriForApi(t *testing.T) {
1316
c := Client{
1417
apiKey: "sample_api_key",
@@ -28,6 +31,47 @@ func TestUriForApi(t *testing.T) {
2831
assert.Nil(t, err)
2932
assert.Equal(t, "https://base.datadoghq.com/api/v1/events?api_key=sample_api_key&application_key=sample_app_key", uri)
3033
})
34+
t.Run("Test all endpoints that need keys in query params", func(t *testing.T) {
35+
for _, api := range needKeysInQueryParams {
36+
uri, err := c.uriForAPI(api)
37+
assert.Nil(t, err)
38+
parsed, err := url.Parse(uri)
39+
assert.Nil(t, err)
40+
assert.Equal(t, parsed.Query().Get("api_key"), "sample_api_key")
41+
assert.Equal(t, parsed.Query().Get("application_key"), "sample_app_key")
42+
}
43+
})
44+
t.Run("Test an endpoint that doesn't need keys in query params", func(t *testing.T) {
45+
uri, err := c.uriForAPI("/v1/dashboard")
46+
assert.Nil(t, err)
47+
assert.Equal(t, "https://base.datadoghq.com/api/v1/dashboard", uri)
48+
})
49+
}
50+
51+
func TestCreateRequest(t *testing.T) {
52+
c := Client{
53+
apiKey: "sample_api_key",
54+
appKey: "sample_app_key",
55+
baseUrl: "https://base.datadoghq.com",
56+
HttpClient: &http.Client{},
57+
RetryTimeout: 1000,
58+
}
59+
t.Run("Test an endpoint that doesn't need keys in query params", func(t *testing.T) {
60+
req, err := c.createRequest("GET", "/v1/dashboard", nil)
61+
assert.Nil(t, err)
62+
assert.Equal(t, "sample_api_key", req.Header.Get("DD-API-KEY"))
63+
assert.Equal(t, "sample_app_key", req.Header.Get("DD-APPLICATION-KEY"))
64+
})
65+
t.Run("Test endpoints that need keys in query params", func(t *testing.T) {
66+
for _, api := range needKeysInQueryParams {
67+
req, err := c.createRequest("GET", api, nil)
68+
assert.Nil(t, err)
69+
// we make sure that we *don't* have keys in query params, because some endpoints
70+
// fail if we send keys both in headers and query params
71+
assert.Equal(t, "", req.Header.Get("DD-API-KEY"))
72+
assert.Equal(t, "", req.Header.Get("DD-APPLICATION-KEY"))
73+
}
74+
})
3175
}
3276

3377
func TestRedactError(t *testing.T) {

0 commit comments

Comments
 (0)