-
Notifications
You must be signed in to change notification settings - Fork 12
/
request.go
147 lines (117 loc) · 2.99 KB
/
request.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
// +build !mock
package synapse
import (
"github.com/parnurzeal/gorequest"
)
/********** TYPES **********/
type (
// Request represents the http request client
Request struct {
authKey, clientID, clientSecret, fingerprint, ipAddress string
}
)
/********** GLOBAL VARIABLES **********/
var goreq = gorequest.New()
/********** METHODS **********/
func (req *Request) updateRequest(clientID, clientSecret, fingerprint, ipAddress string, authKey ...string) Request {
var aKey string
if len(authKey) > 0 {
aKey = authKey[0]
}
return Request{
authKey: aKey,
clientID: clientID,
clientSecret: clientSecret,
fingerprint: fingerprint,
ipAddress: ipAddress,
}
}
/********** REQUEST **********/
// Get performs a GET request
func (req *Request) Get(url string, params []string) ([]byte, error) {
var p string
if len(params) > 0 {
p = params[0]
}
res, body, errs := goreq.
Get(url).
Set("x-sp-gateway", req.clientID+"|"+req.clientSecret).
Set("x-sp-user-ip", req.ipAddress).
Set("x-sp-user", req.authKey+"|"+req.fingerprint).
Query(p).
EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if res.StatusCode != 200 {
log.info("GET", res.Status, url)
return body, handleHTTPError(body)
}
log.info("GET", res.Status, url)
return body, nil
}
// Post performs a POST request
func (req *Request) Post(url, data string, params []string) ([]byte, error) {
newRequest := goreq.
Post(url).
Set("x-sp-gateway", req.clientID+"|"+req.clientSecret).
Set("x-sp-user-ip", req.ipAddress).
Set("x-sp-user", req.authKey+"|"+req.fingerprint)
if len(params) > 0 {
newRequest.Set("x-sp-idempotency-key", params[0])
}
res, body, errs := newRequest.
Send(data).
EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if res.StatusCode != 200 {
log.info("POST", res.Status, url)
return body, handleHTTPError(body)
}
log.info("POST", res.Status, url)
return body, nil
}
// Patch performs a PATCH request
func (req *Request) Patch(url, data string, params []string) ([]byte, error) {
var p string
if len(params) > 0 {
p = params[0]
}
res, body, errs := goreq.
Patch(url).
Set("x-sp-gateway", req.clientID+"|"+req.clientSecret).
Set("x-sp-user-ip", req.ipAddress).
Set("x-sp-user", req.authKey+"|"+req.fingerprint).
Query(p).
Send(data).
EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if res.StatusCode != 200 {
log.info("PATCH", res.Status, url)
return body, handleHTTPError(body)
}
log.info("PATCH", res.Status, url)
return body, nil
}
// Delete performs a DELETE request
func (req *Request) Delete(url string) ([]byte, error) {
res, body, errs := goreq.
Delete(url).
Set("x-sp-gateway", req.clientID+"|"+req.clientSecret).
Set("x-sp-user-ip", req.ipAddress).
Set("x-sp-user", req.authKey+"|"+req.fingerprint).
EndBytes()
if len(errs) > 0 {
return nil, errs[0]
}
if res.StatusCode != 200 {
log.info("DELETE", res.Status, url)
return body, handleHTTPError(body)
}
log.info("DELETE", res.Status, url)
return body, nil
}