-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmaxcdn.go
293 lines (244 loc) · 7.43 KB
/
maxcdn.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
package maxcdn
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"github.com/garyburd/go-oauth/oauth"
)
const (
userAgent = "Go MaxCDN API Client"
contentType = "application/x-www-form-urlencoded"
logsPath = "/v3/reporting/logs.json"
)
// APIHost is the hostname, including protocol, to MaxCDN's API.
var APIHost = "https://rws.maxcdn.com"
// MaxCDN is the core struct for interacting with MaxCDN.
//
// HTTPClient can be overridden as needed, but will be set to
// http.DefaultClient by default.
type MaxCDN struct {
// MaxCDN Consumer Alias
Alias string
// Display raw http Request and Response for each http Transport
Verbose bool
client oauth.Client
HTTPClient *http.Client
}
// NewMaxCDN sets up a new MaxCDN instance.
func NewMaxCDN(alias, token, secret string) *MaxCDN {
return &MaxCDN{
HTTPClient: http.DefaultClient,
Alias: alias,
client: oauth.Client{
Credentials: oauth.Credentials{
Token: token,
Secret: secret,
},
TemporaryCredentialRequestURI: APIHost + "oauth/request_token",
TokenRequestURI: APIHost + "oauth/access_token",
},
}
}
// Get does an OAuth signed http.Get
func (max *MaxCDN) Get(endpointType interface{}, endpoint string, form url.Values) (*Response, error) {
return max.DoParse(endpointType, "GET", endpoint, form)
}
// GetLogs is a seperate getter for MaxCDN's logs.json endpoint, as it currently doesn't follow
// the json format of other endpoints.
func (max *MaxCDN) GetLogs(form url.Values) (Logs, error) {
var logs Logs
rsp, err := max.Request("GET", logsPath, form)
if err != nil {
return logs, err
}
err = json.NewDecoder(rsp.Body).Decode(&logs)
_ = rsp.Body.Close()
if err != nil {
return logs, err
}
return logs, nil
}
// Post does an OAuth signed http.Post
func (max *MaxCDN) Post(endpointType interface{}, endpoint string, form url.Values) (*Response, error) {
return max.DoParse(endpointType, "POST", endpoint, form)
}
// Put does an OAuth signed http.Put
func (max *MaxCDN) Put(endpointType interface{}, endpoint string, form url.Values) (*Response, error) {
return max.DoParse(endpointType, "PUT", endpoint, form)
}
// Delete does an OAuth signed http.Delete
//
// Delete does not take an endpointType because delete only returns a status code.
func (max *MaxCDN) Delete(endpoint string, form url.Values) (*Response, error) {
if form != nil {
endpoint = fmt.Sprintf("%s?%s", endpoint, form.Encode())
}
return max.Do("DELETE", endpoint, nil)
}
// PurgeZone purges a specified zones cache.
func (max *MaxCDN) PurgeZone(zone int) (*Response, error) {
return max.Delete(fmt.Sprintf("/zones/pull.json/%d/cache", zone), nil)
}
// PurgeZoneString purges a specified zones cache.
func (max *MaxCDN) PurgeZoneString(zone string) (*Response, error) {
return max.Delete(fmt.Sprintf("/zones/pull.json/%s/cache", zone), nil)
}
// PurgeZonesString purges multiple zones caches.
func (max *MaxCDN) PurgeZonesString(zones []string) ([]*Response, error) {
var (
resChannel = make(chan *Response)
errChannel = make(chan error)
resps = []*Response{}
last error
)
for _, zone := range zones {
go func(zone string) {
res, err := max.PurgeZoneString(zone)
resChannel <- res
errChannel <- err
}(zone)
}
// Wait for all responses to come back.
// TODO: Consider adding some method of timing out.
for range zones {
res := <-resChannel
err := <-errChannel
resps = append(resps, res)
last = err
}
close(resChannel)
close(errChannel)
return resps, last
}
// PurgeZones purges multiple zones caches.
func (max *MaxCDN) PurgeZones(zones []int) ([]*Response, error) {
zoneStrings := make([]string, 0, len(zones))
for _, zone := range zones {
zoneStrings = append(zoneStrings, strconv.FormatInt(int64(zone), 10))
}
return max.PurgeZonesString(zoneStrings)
}
// PurgeFile purges a specified file by zone from cache.
func (max *MaxCDN) PurgeFile(zone int, file string) (*Response, error) {
return max.PurgeFileString(strconv.FormatInt(int64(zone), 10), file)
}
// PurgeFileString purges a specified file by zone from cache.
func (max *MaxCDN) PurgeFileString(zone string, file string) (*Response, error) {
form := url.Values{}
form.Set("files", file)
return max.Delete("/zones/pull.json/"+zone+"/cache", form)
}
// PurgeFiles purges multiple files from a zone.
func (max *MaxCDN) PurgeFiles(zone int, files []string) ([]*Response, error) {
var (
resChannel = make(chan *Response)
errChannel = make(chan error)
resps = []*Response{}
last error
)
for _, file := range files {
go func(file string) {
res, err := max.PurgeFile(zone, file)
resChannel <- res
errChannel <- err
}(file)
}
// Wait for all responses to come back.
// TODO: Consider adding some method of timing out.
for range files {
res := <-resChannel
err := <-errChannel
resps = append(resps, res)
last = err
}
close(resChannel)
close(errChannel)
return resps, last
}
// DoParse execute the http query and unmarshal the data into `endpointType`.
func (max *MaxCDN) DoParse(endpointType interface{}, method, endpoint string, form url.Values) (*Response, error) {
rsp, err := max.Do(method, endpoint, form)
if err != nil {
return nil, err
}
if err := json.Unmarshal(rsp.Data, &endpointType); err != nil {
return nil, err
}
return rsp, nil
}
// Do is a low level method to interact with MaxCDN's RESTful API via Request
// and return a parsed Response. It's used by all other methods.
//
// This method closes the raw http.Response body.
func (max *MaxCDN) Do(method, endpoint string, form url.Values) (*Response, error) {
rsp := &Response{}
res, err := max.Request(method, endpoint, form)
if err != nil {
return nil, err
}
rsp.Headers = res.Header
err = json.NewDecoder(res.Body).Decode(&rsp)
_ = res.Body.Close()
if err != nil {
return nil, err
}
if rsp.Code > 299 {
return nil, fmt.Errorf("%s: %s", rsp.Error.Type, rsp.Error.Message)
}
return rsp, nil
}
// Request is a low level method to interact with MaxCDN's RESTful API. It's
// used by all other methods.
//
// If using this method, you must manually close the res.Body or bad things
// may happen.
func (max *MaxCDN) Request(method, endpoint string, form url.Values) (*http.Response, error) {
req, err := http.NewRequest(method, max.url(endpoint), nil)
if err != nil {
return nil, err
}
if method == "GET" && req.URL.RawQuery != "" {
return nil, errors.New("oauth: url must not contain a query string")
}
if form != nil {
if method == "GET" {
req.URL.RawQuery = form.Encode()
} else {
req.Body = ioutil.NopCloser(strings.NewReader(form.Encode()))
}
// Only post needs a signed form.
if method != "POST" {
form = nil
}
}
req.Header.Set("Authorization", max.client.AuthorizationHeader(nil, method, req.URL, form))
req.Header.Set("Content-Type", contentType)
req.Header.Set("User-Agent", userAgent)
if max.Verbose {
if buf, err := httputil.DumpRequest(req, true); err == nil {
fmt.Printf("Request: %s\n---\n\n", buf)
}
}
res, err := max.HTTPClient.Do(req)
if err != nil {
fmt.Printf("Response Error: %s\n---\n\n", err)
}
if max.Verbose && res != nil {
if buf, err := httputil.DumpResponse(res, true); err == nil {
fmt.Printf("Response: %s\n---\n\n", buf)
}
}
return res, err
}
func (max *MaxCDN) url(endpoint string) string {
if len(endpoint) > 0 && endpoint[0] == '/' {
endpoint = endpoint[1:]
}
return fmt.Sprintf("%s/%s/%s", APIHost, max.Alias, endpoint)
}