-
Notifications
You must be signed in to change notification settings - Fork 0
/
Requests.go
416 lines (352 loc) · 10.3 KB
/
Requests.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package libdatamanager
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"time"
)
// Method http request method
type Method string
// Requests
const (
GET Method = "GET"
POST Method = "POST"
DELETE Method = "DELETE"
PUT Method = "PUT"
)
// ContentType contenttype header of request
type ContentType string
// Content types
const (
JSONContentType ContentType = "application/json"
)
// PingRequest a ping request content
type PingRequest struct {
Payload string
}
// Endpoint a remote url-path
type Endpoint string
// Remote endpoints
const (
// Ping
EPPing Endpoint = "/ping"
// User
EPUser Endpoint = "/user"
EPLogin = EPUser + "/login"
EPRegister = EPUser + "/register"
EPUserStats = EPUser + "/stats"
// Files
EPFile Endpoint = "/file"
EPFileList = EPFile + "s"
EPFileUpdate = EPFile + "/update"
EPFileDelete = EPFile + "/delete"
EPFileGet = "/download/file"
EPFilePublish = EPFile + "/publish"
// Upload
EPFileUpload Endpoint = "/upload" + EPFile
// Attribute
EPAttribute Endpoint = "/attribute"
EPAttributes Endpoint = "/attributes"
// Tags
EPAttributeTag = EPAttribute + "/tag"
EPTagCreate = EPAttributeTag + "/create"
EPTagUpdate = EPAttributeTag + "/update"
EPTagDelete = EPAttributeTag + "/delete"
EPTags = EPAttributeTag + "/get"
// Group
EPAttributeGroup = EPAttribute + "/group"
EPGroupCreate = EPAttributeGroup + "/create"
EPGroupUpdate = EPAttributeGroup + "/update"
EPGroupDelete = EPAttributeGroup + "/delete"
EPGroups = EPAttributeGroup + "/get"
// Namespace
EPNamespace Endpoint = "/namespace"
EPNamespaceCreate = EPNamespace + "/create"
EPNamespaceUpdate = EPNamespace + "/update"
EPNamespaceDelete = EPNamespace + "/delete"
EPNamespaceList = EPNamespace + "s"
)
// RequestConfig configurations for requests
type RequestConfig struct {
IgnoreCert bool
URL string
MachineID string
Username string
SessionToken string
}
// GetBearerAuth returns bearer authorization from config
func (rc RequestConfig) GetBearerAuth() Authorization {
return Authorization{
Type: Bearer,
Palyoad: rc.SessionToken,
}
}
// Request a rest server request
type Request struct {
RequestType RequestType
Endpoint Endpoint
Payload interface{}
Config *RequestConfig
Method Method
ContentType ContentType
Authorization *Authorization
Headers map[string]string
BenchChan chan time.Time
Compressed bool
MaxConnectionsPerHost int
}
// FileListRequest contains file info (and a file)
type FileListRequest struct {
FileID uint `json:"fid"`
Name string `json:"name"`
AllNamespaces bool `json:"allns"`
OptionalParams OptionalRequetsParameter `json:"opt"`
Order string `json:"order,omitempty"`
Attributes FileAttributes `json:"attributes"`
}
// OptionalRequetsParameter optional request parameter
type OptionalRequetsParameter struct {
Verbose uint8 `json:"verb"`
}
// FileRequest contains data to update a file
type FileRequest struct {
FileID uint `json:"fid"`
Name string `json:"name,omitempty"`
PublicName string `json:"pubname,omitempty"`
Updates FileUpdateItem `json:"updates,omitempty"`
All bool `json:"all"`
Attributes FileAttributes `json:"attributes"`
}
// UpdateAttributeRequest contains data to update a tag
type UpdateAttributeRequest struct {
Name string `json:"name"`
NewName string `json:"newname"`
Namespace string `json:"namespace"`
}
// CredentialsRequest request containing credentials
type CredentialsRequest struct {
MachineID string `json:"mid,omitempty"`
Username string `json:"username"`
Password string `json:"pass"`
}
// NamespaceRequest namespace action request
type NamespaceRequest struct {
Namespace string `json:"ns"`
NewName string `json:"newName,omitempty"`
}
// UserAttributesRequest request for getting
// namespaces and groups
type UserAttributesRequest struct {
Mode uint `json:"m"`
}
// UploadRequestStruct contains file info (and a file)
type UploadRequestStruct struct {
// Required fields
UploadType UploadType `json:"type"`
Name string `json:"name"`
// Optional fields
URL string `json:"url,omitempty"`
Public bool `json:"pb,omitempty"`
PublicName string `json:"pbname,omitempty"`
Attributes FileAttributes `json:"attr,omitempty"`
Encryption int8 `json:"e,omitempty"`
Compressed bool `json:"compr,omitempty"`
Archived bool `json:"arved,omitempty"`
ReplaceFileByID uint `json:"r,omitempty"`
ReplaceEqualNames bool `json:"ren"`
All bool `json:"a"`
}
// StatsRequestStruct informations about a stat-request
type StatsRequestStruct struct {
Namespace string `json:"ns,omitempty"`
}
// UploadType type of upload
type UploadType uint8
// Available upload types
const (
FileUploadType UploadType = iota
URLUploadType
)
// RequestType type of request
type RequestType uint8
// Request types
const (
JSONRequestType RequestType = iota
RawRequestType
)
// NewRequest creates a new post request
func (limdm *LibDM) NewRequest(endpoint Endpoint, payload interface{}) *Request {
return &Request{
RequestType: JSONRequestType,
Endpoint: endpoint,
Payload: payload,
Config: limdm.Config,
Method: POST,
ContentType: JSONContentType,
MaxConnectionsPerHost: 1,
}
}
// WithConnectionLimit set limit of max connectionts per host
func (request *Request) WithConnectionLimit(maxConnections int) *Request {
request.MaxConnectionsPerHost = maxConnections
return request
}
// WithCompression use a different method
func (request *Request) WithCompression(compression bool) *Request {
request.Compressed = compression
return request
}
// WithMethod use a different method
func (request *Request) WithMethod(m Method) *Request {
request.Method = m
return request
}
// WithRequestType use different request type
func (request *Request) WithRequestType(rType RequestType) *Request {
request.RequestType = rType
return request
}
// WithAuth with authorization
func (request *Request) WithAuth(a Authorization) *Request {
request.Authorization = &a
return request
}
// WithAuthFromConfig with authorization
func (request *Request) WithAuthFromConfig() *Request {
auth := request.Config.GetBearerAuth()
request.Authorization = &auth
return request
}
// WithBenchCallback with bench
func (request *Request) WithBenchCallback(c chan time.Time) *Request {
request.BenchChan = c
return request
}
// WithContentType with contenttype
func (request *Request) WithContentType(ct ContentType) *Request {
request.ContentType = ct
return request
}
// WithHeader add header to request
func (request *Request) WithHeader(name string, value string) *Request {
if request.Headers == nil {
request.Headers = make(map[string]string)
}
request.Headers[name] = value
return request
}
// BuildClient return client
func (request *Request) BuildClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: request.MaxConnectionsPerHost,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: request.Config.IgnoreCert,
},
MaxIdleConns: 5,
MaxIdleConnsPerHost: 5,
DisableKeepAlives: true,
},
Timeout: 0,
}
}
// DoHTTPRequest do plain http request
func (request *Request) DoHTTPRequest() (*http.Response, error) {
client := request.BuildClient()
// Build url
u, err := url.Parse(request.Config.URL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, string(request.Endpoint))
var reader io.Reader
// Use correct payload
if request.RequestType == JSONRequestType {
// Encode data
var err error
bytePayload, err := json.Marshal(request.Payload)
if err != nil {
return nil, err
}
reader = bytes.NewReader(bytePayload)
} else if request.RequestType == RawRequestType {
switch request.Payload.(type) {
case []byte:
reader = bytes.NewReader((request.Payload).([]byte))
case io.Reader:
reader = (request.Payload).(io.Reader)
case io.PipeReader:
reader = (request.Payload).(*io.PipeReader)
}
}
if reader == nil {
reader = bytes.NewBuffer([]byte(""))
}
// Bulid request
req, _ := http.NewRequest(string(request.Method), u.String(), reader)
// Set contenttype header
req.Header.Set("Content-Type", string(request.ContentType))
if request.Compressed {
req.Header.Set("Content-Encoding", string("gzip"))
}
for headerKey, headerValue := range request.Headers {
req.Header.Set(headerKey, headerValue)
}
// Set Authorization header
if request.Authorization != nil {
req.Header.Set("Authorization", fmt.Sprintf("%s %s", string(request.Authorization.Type), request.Authorization.Palyoad))
}
return client.Do(req)
}
// Do a better request method
func (request Request) Do(retVar interface{}) (*RestRequestResponse, error) {
resp, err := request.DoHTTPRequest()
if err != nil || resp == nil {
return nil, err
}
defer resp.Body.Close()
var response *RestRequestResponse
response = &RestRequestResponse{
HTTPCode: resp.StatusCode,
Headers: &resp.Header,
}
if resp.StatusCode == 200 {
response.Status = ResponseSuccess
} else {
response.Status = ResponseError
}
response.Message = ""
// Only fill retVar if response was successful
if response.Status == ResponseSuccess && retVar != nil {
// Read response
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Parse response into retVar
err = json.Unmarshal(d, &retVar)
if err != nil {
return nil, err
}
} else if response.Status == ResponseError {
var errRes ErrorResponse
// Read response
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Parse response into retVar
err = json.Unmarshal(d, &errRes)
if err != nil {
return nil, err
}
response.Message = fmt.Sprintf("%s", errRes.Message)
}
return response, nil
}