-
Notifications
You must be signed in to change notification settings - Fork 156
/
client.go
127 lines (105 loc) · 2.86 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2013 by authors and contributors.
*/
package datadog
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"sync"
"time"
)
// Client is the object that handles talking to the Datadog API. This maintains
// state information for a particular application connection.
type Client struct {
apiKey, appKey, baseUrl string
//The Http Client that is used to make requests
HttpClient *http.Client
RetryTimeout time.Duration
//Option to specify extra headers like User-Agent
ExtraHeader map[string]string
// rateLimiting is used to store the rate limitting stats.
// More information in the official documentation: https://docs.datadoghq.com/api/?lang=bash#rate-limiting
rateLimitingStats map[string]RateLimit
// Mutex to protect the rate limiting map.
m sync.Mutex
}
type RateLimit struct {
Limit string
Period string
Reset string
Remaining string
}
// valid is the struct to unmarshal validation endpoint responses into.
type valid struct {
Errors []string `json:"errors"`
IsValid bool `json:"valid"`
}
// NewClient returns a new datadog.Client which can be used to access the API
// methods. The expected argument is the API key.
func NewClient(apiKey, appKey string) *Client {
baseUrl := os.Getenv("DATADOG_HOST")
if baseUrl == "" {
baseUrl = "https://api.datadoghq.com"
}
return &Client{
apiKey: apiKey,
appKey: appKey,
baseUrl: baseUrl,
HttpClient: http.DefaultClient,
RetryTimeout: time.Duration(60 * time.Second),
rateLimitingStats: make(map[string]RateLimit),
ExtraHeader: make(map[string]string),
}
}
// SetKeys changes the value of apiKey and appKey.
func (c *Client) SetKeys(apiKey, appKey string) {
c.apiKey = apiKey
c.appKey = appKey
}
// SetBaseUrl changes the value of baseUrl.
func (c *Client) SetBaseUrl(baseUrl string) {
c.baseUrl = baseUrl
}
// GetBaseUrl returns the baseUrl.
func (c *Client) GetBaseUrl() string {
return c.baseUrl
}
// Validate checks if the API key (not the APP key) is valid.
func (client *Client) Validate() (bool, error) {
var out valid
var resp *http.Response
uri, err := client.uriForAPI("/v1/validate")
if err != nil {
return false, err
}
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return false, err
}
req.Header.Set("DD-API-KEY", client.apiKey)
if (client.appKey != "") {
req.Header.Set("DD-APPLICATION-KEY", client.appKey)
}
resp, err = client.doRequestWithRetries(req, client.RetryTimeout)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden {
return false, nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
if err = json.Unmarshal(body, &out); err != nil {
return false, err
}
return out.IsValid, nil
}