-
Notifications
You must be signed in to change notification settings - Fork 0
/
dondominio.go
333 lines (278 loc) · 9.29 KB
/
dondominio.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"sync/atomic"
"time"
"github.com/gorilla/schema"
)
// DefaultTimeout api requests after 180s
const DefaultTimeout = 180 * time.Second
// Endpoints
const Endpoint = "https://simple-api.dondominio.net"
var encoder = schema.NewEncoder()
// Client represents a client to call the DD API
type Client struct {
// AppKey holds the Application key
AppKey string
// AppSecret holds the Application secret key
AppSecret string
// API endpoint
endpoint string
// Client is the underlying HTTP client used to run the requests. It may be overloaded but a default one is instanciated in ``NewClient`` by default.
Client *http.Client
// Logger is used to log HTTP requests and responses.
Logger Logger
// Ensures that the timeDelta function is only ran once
// sync.Once would consider init done, even in case of error
// hence a good old flag
timeDelta atomic.Value
// Timeout configures the maximum duration to wait for an API requests to complete
Timeout time.Duration
// UserAgent configures the user-agent indication that will be sent in the requests to DDcloud API
UserAgent string
}
// NewClient represents a new client to call the API
func NewClient(endpoint, appKey, appSecret string) (*Client, error) {
var httpClient http.Client
if proxyUrl, err := url.Parse(os.Getenv("PROXY")); err == nil {
//fmt.Printf("PROXY: %s\n", proxyUrl)
httpClient = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
},
}
} else {
httpClient = http.Client{}
}
client := Client{
AppKey: appKey,
AppSecret: appSecret,
Client: &httpClient,
Timeout: time.Duration(DefaultTimeout),
}
// Get and check the configuration
if err := client.loadConfig(endpoint); err != nil {
return nil, err
}
return &client, nil
}
// NewEndpointClient will create an API client for specified
// endpoint and load all credentials from environment or
// configuration files
func NewEndpointClient(endpoint string) (*Client, error) {
return NewClient(endpoint, "", "")
}
// NewDefaultClient will load all it's parameter from environment
// or configuration files
func NewDefaultClient() (*Client, error) {
return NewClient("", "", "")
}
//
// High level helpers
//
// Ping performs a ping to DD API.
// In fact, ping is just a /auth/time call, in order to check if API is up.
func (c *Client) Ping() error {
_, err := c.getTime()
return err
}
// TimeDelta represents the delay between the machine that runs the code and the
// DD API. The delay shouldn't change, let's do it only once.
func (c *Client) TimeDelta() (time.Duration, error) {
return c.getTimeDelta()
}
// Time returns time from the DD API, by asking GET /auth/time.
func (c *Client) Time() (*time.Time, error) {
return c.getTime()
}
//
// Common request wrappers
//
// Get is a wrapper for the GET method
func (c *Client) Get(url string, resType interface{}) error {
return c.CallAPI("GET", url, nil, resType)
}
// Post is a wrapper for the POST method
func (c *Client) Post(url string, reqBody, resType interface{}) error {
return c.CallAPI("POST", url, reqBody, resType)
}
// Put is a wrapper for the PUT method
func (c *Client) Put(url string, reqBody, resType interface{}) error {
return c.CallAPI("PUT", url, reqBody, resType)
}
// Delete is a wrapper for the DELETE method
func (c *Client) Delete(url string, resType interface{}) error {
return c.CallAPI("DELETE", url, nil, resType)
}
// GetWithContext is a wrapper for the GET method
func (c *Client) GetWithContext(ctx context.Context, url string, resType interface{}) error {
return c.CallAPIWithContext(ctx, "GET", url, nil, resType)
}
// PostWithContext is a wrapper for the POST method
func (c *Client) PostWithContext(ctx context.Context, url string, reqBody, resType interface{}) error {
return c.CallAPIWithContext(ctx, "POST", url, reqBody, resType)
}
// PutWithContext is a wrapper for the PUT method
func (c *Client) PutWithContext(ctx context.Context, url string, reqBody, resType interface{}) error {
return c.CallAPIWithContext(ctx, "PUT", url, reqBody, resType)
}
// DeleteWithContext is a wrapper for the DELETE method
func (c *Client) DeleteWithContext(ctx context.Context, url string, resType interface{}) error {
return c.CallAPIWithContext(ctx, "DELETE", url, nil, resType)
}
// timeDelta returns the time delta between the host and the remote API
func (c *Client) getTimeDelta() (time.Duration, error) {
d, ok := c.timeDelta.Load().(time.Duration)
if ok {
return d, nil
}
ddTime, err := c.getTime()
if err != nil {
return 0, err
}
d = time.Since(*ddTime)
c.timeDelta.Store(d)
return d, nil
}
// getTime t returns time from for a given api client endpoint
func (c *Client) getTime() (*time.Time, error) {
var timestamp int64
err := c.Get("/auth/time", ×tamp)
if err != nil {
return nil, err
}
serverTime := time.Unix(timestamp, 0)
return &serverTime, nil
}
// NewRequest returns a new HTTP request
func (c *Client) NewRequest(method, path string, reqBody interface{}) (*http.Request, error) {
var err error
var body = url.Values{}
if reqBody != nil {
err = encoder.Encode(reqBody, body)
if err != nil {
return nil, err
}
}
body.Set("apiuser", c.AppKey)
body.Set("apipasswd", c.AppSecret)
fmt.Fprintf(os.Stdout, "url: %s, body %s\n", path, body.Encode())
target := fmt.Sprintf("%s%s", c.endpoint, path)
req, err := http.NewRequest(method, target, strings.NewReader(body.Encode()))
if err != nil {
return nil, err
}
// Inject headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
req.Header.Add("Accept", "application/json")
// Send the request with requested timeout
c.Client.Timeout = c.Timeout
if c.UserAgent != "" {
req.Header.Set("User-Agent", "github.com/galgus/go-dd ("+c.UserAgent+")")
} else {
req.Header.Set("User-Agent", "github.com/galgus/go-dd")
}
return req, nil
}
// Do sends an HTTP request and returns an HTTP response
func (c *Client) Do(req *http.Request) (*http.Response, error) {
if c.Logger != nil {
c.Logger.LogRequest(req)
}
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
if c.Logger != nil {
c.Logger.LogResponse(resp)
}
return resp, nil
}
// CallAPI is the lowest level call helper. If needAuth is true,
// inject authentication headers and sign the request.
//
// Request signature is a sha1 hash on following fields, joined by '+':
// - applicationSecret (from Client instance)
// - consumerKey (from Client instance)
// - capitalized method (from arguments)
// - full request url, including any query string argument
// - full serialized request body
// - server current time (takes time delta into account)
//
// Call will automatically assemble the target url from the endpoint
// configured in the client instance and the path argument. If the reqBody
// argument is not nil, it will also serialize it as json and inject
// the required Content-Type header.
//
// If everything went fine, unmarshall response into resType and return nil
// otherwise, return the error
func (c *Client) CallAPI(method, path string, reqBody, resType interface{}) error {
return c.CallAPIWithContext(context.Background(), method, path, reqBody, resType)
}
// CallAPIWithContext is the lowest level call helper. If needAuth is true,
// inject authentication headers and sign the request.
//
// Request signature is a sha1 hash on following fields, joined by '+':
// - applicationSecret (from Client instance)
// - consumerKey (from Client instance)
// - capitalized method (from arguments)
// - full request url, including any query string argument
// - full serialized request body
// - server current time (takes time delta into account)
//
// # Context is used by http.Client to handle context cancelation
//
// Call will automatically assemble the target url from the endpoint
// configured in the client instance and the path argument. If the reqBody
// argument is not nil, it will also serialize it as json and inject
// the required Content-Type header.
//
// If everything went fine, unmarshall response into resType and return nil
// otherwise, return the error
func (c *Client) CallAPIWithContext(ctx context.Context, method, path string, reqBody, resType interface{}) error {
req, err := c.NewRequest(method, path, reqBody)
if err != nil {
return err
}
req = req.WithContext(ctx)
response, err := c.Do(req)
if err != nil {
return err
}
return c.UnmarshalResponse(response, resType)
}
// UnmarshalResponse checks the response and unmarshals it into the response
// type if needed Helper function, called from CallAPI
func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) error {
// Read all the response body
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
// < 200 && >= 300 : API error
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
apiError := &APIError{Code: response.StatusCode}
if err = json.Unmarshal(body, apiError); err != nil {
apiError.Message = string(body)
}
apiError.QueryID = response.Header.Get("X-Dd-QueryID")
return apiError
}
// Nothing to unmarshal
if len(body) == 0 || resType == nil {
return nil
}
fmt.Printf("body: %s\n", string(body))
d := json.NewDecoder(bytes.NewReader(body))
d.UseNumber()
return d.Decode(&resType)
}