-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
49 lines (39 loc) · 939 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package datadog
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
const metricEndpointFormat = "https://api.datadoghq.com/api/v1/series?api_key=%s"
type Client struct {
APIKey string
HTTPClient *http.Client
}
// UploadTimeSeries .
func (c *Client) UploadTimeSeries(tsReq TimeSeriesRequest) error {
if len(tsReq.Series) == 0 {
return nil
}
body, err := json.Marshal(tsReq)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf(metricEndpointFormat, c.APIKey), bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK && resp.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("invalid API response: %d", resp.StatusCode)
}
io.Copy(ioutil.Discard, resp.Body)
return nil
}