-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
255 lines (222 loc) · 7.36 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
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
package go_mojoauth
import (
"errors"
"github.com/mojoauth/go-sdk/httprutils"
"github.com/mojoauth/go-sdk/mojoerror"
)
var URLEncodedHeader = map[string]string{"content-Type": "application/x-www-form-urlencoded"}
var JSONHeader = map[string]string{"content-Type": "application/json"}
// NewGetRequest takes a uri and query parameters, then constructs a GET request for MojoAuth API endpoints requiring access tokens
// being passed in Authorization Bearer header
func (mojo Mojoauth) NewGetReqWithToken(path string, queries ...map[string]string) (*httprutils.Request, error) {
if mojo.Context.Token == "" {
errMsg := "Must initialize MojoAuth with access token for this API call."
err := mojoerror.New("MissingTokenErr", errMsg, errors.New(errMsg))
return nil, err
}
request := &httprutils.Request{
Method: httprutils.Get,
URL: mojo.Domain + path,
Headers: map[string]string{
"content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + mojo.Context.Token,
},
QueryParams: map[string]string{
"X-API-Key": mojo.Context.ApiKey,
},
}
for _, q := range queries {
for k, v := range q {
request.QueryParams[k] = v
}
}
return request, nil
}
// NewGetRequest takes a uri and query parameters, then constructs a GET request for a MojoAuth API endpoint
func (mojo Mojoauth) NewGetReq(path string, queries ...map[string]string) *httprutils.Request {
request := &httprutils.Request{
Method: httprutils.Get,
URL: mojo.Domain + path,
Headers: map[string]string{
"content-Type": "application/x-www-form-urlencoded",
"X-API-Key": mojo.Context.ApiKey,
},
QueryParams: map[string]string{},
}
for _, q := range queries {
for k, v := range q {
request.QueryParams[k] = v
}
}
return request
}
// NewPostReqWithToken takes a uri, body, and query parameters, then constructs the request for MojoAuth PUT API end points requiring access tokens being passed in Authorization Bearer header
func (mojo Mojoauth) NewPostReqWithToken(path string, body interface{}, queries ...map[string]string) (*httprutils.Request, error) {
if mojo.Context.Token == "" {
errMsg := "Must initialize MojoAuth with access token for this API call."
err := mojoerror.New("MissingTokenErr", errMsg, errors.New(errMsg))
return nil, err
}
encodedBody, error := httprutils.EncodeBody(body)
if error != nil {
return nil, error
}
request := &httprutils.Request{
Method: httprutils.Post,
URL: mojo.Domain + path,
Headers: map[string]string{
"content-Type": "application/json",
"Authorization": "Bearer " + mojo.Context.Token,
"X-API-Key": mojo.Context.ApiKey,
},
Body: encodedBody,
}
for _, q := range queries {
for k, v := range q {
request.QueryParams[k] = v
}
}
return request, nil
}
// NewPostReq takes a uri, body, and optional queries to construct a POST request for a MojoAuth POST API endpoint
func (mojo Mojoauth) NewPostReq(path string, body interface{}, queries ...map[string]string) (*httprutils.Request, error) {
encodedBody, error := httprutils.EncodeBody(body)
if error != nil {
return nil, error
}
request := &httprutils.Request{
Method: httprutils.Post,
URL: mojo.Domain + path,
Headers: map[string]string{
"X-API-Key": mojo.Context.ApiKey,
"content-Type": "application/json",
},
QueryParams: map[string]string{},
Body: encodedBody,
}
for _, q := range queries {
for k, v := range q {
request.QueryParams[k] = v
}
}
return request, nil
}
// NewPutReq takes a uri, body, and optional queries to construct a PUT request for a MojoAuth API endpoint
func (mojo Mojoauth) NewPutReq(path string, body interface{}, queries ...map[string]string) (*httprutils.Request, error) {
encodedBody, error := httprutils.EncodeBody(body)
if error != nil {
return nil, error
}
request := &httprutils.Request{
Method: httprutils.Put,
URL: mojo.Domain + path,
Headers: map[string]string{
"content-Type": "application/json",
},
QueryParams: map[string]string{
"X-API-Key": mojo.Context.ApiKey,
},
Body: encodedBody,
}
for _, q := range queries {
for k, v := range q {
request.QueryParams[k] = v
}
}
return request, nil
}
// NewPutReqWithToken takes a uri and query parameters, then constructs a PUT request for MojoAuth API endpoints requiring access tokens
// being passed in Authorization Bearer header
func (mojo Mojoauth) NewPutReqWithToken(path string, body interface{}, queries ...map[string]string) (*httprutils.Request, error) {
if mojo.Context.Token == "" {
errMsg := "Must initialize MojoAuth with access token for this API call."
err := mojoerror.New("MissingTokenErr", errMsg, errors.New(errMsg))
return nil, err
}
encodedBody, error := httprutils.EncodeBody(body)
if error != nil {
return nil, error
}
request := &httprutils.Request{
Method: httprutils.Put,
URL: mojo.Domain + path,
Headers: map[string]string{
"content-Type": "application/json",
"Authorization": "Bearer " + mojo.Context.Token,
},
QueryParams: map[string]string{
"X-API-Key": mojo.Context.ApiKey,
},
Body: encodedBody,
}
for _, q := range queries {
for k, v := range q {
request.QueryParams[k] = v
}
}
return request, nil
}
// NewDeleteReq takes a uri, body, and optional queries to construct a DELETE request for a MojoAuth POST API endpoint
func (mojo Mojoauth) NewDeleteReq(path string, body ...interface{}) *httprutils.Request {
if len(body) != 0 {
encoded, err := httprutils.EncodeBody(body[0])
if err != nil {
return nil
}
return &httprutils.Request{
Method: httprutils.Delete,
URL: mojo.Domain + path,
Headers: URLEncodedHeader,
Body: encoded,
}
} else {
return &httprutils.Request{
Method: httprutils.Delete,
URL: mojo.Domain + path,
Headers: URLEncodedHeader,
}
}
}
// NewDeleteReqWithToken takes a uri and query parameters, then constructs a PUT request for MojoAuth API endpoints requiring access tokens
// being passed in Authorization Bearer header
func (mojo Mojoauth) NewDeleteReqWithToken(path string, body interface{}, queries ...map[string]string) (*httprutils.Request, error) {
if mojo.Context.Token == "" {
errMsg := "Must initialize MojoAuth with access token for this API call."
err := mojoerror.New("MissingTokenErr", errMsg, errors.New(errMsg))
return nil, err
}
encodedBody, error := httprutils.EncodeBody(body)
if error != nil {
return nil, error
}
request := &httprutils.Request{
Method: httprutils.Delete,
URL: mojo.Domain + path,
Headers: map[string]string{
"content-Type": "application/json",
"Authorization": "Bearer " + mojo.Context.Token,
},
QueryParams: map[string]string{
"X-API-Key": mojo.Context.ApiKey,
},
Body: encodedBody,
}
for _, q := range queries {
for k, v := range q {
request.QueryParams[k] = v
}
}
return request, nil
}
// AddApiCredentialsToReqHeader removes the X-API-Key query parameter from a constructed request
// and add MojoAuth app credentials in the request headers
func (mojo Mojoauth) AddApiCredentialsToReqHeader(req *httprutils.Request) {
delete(req.QueryParams, "X-API-Key")
req.Headers["X-API-Key"] = mojo.Context.ApiKey
}
// NormalizeApiKey normalizes the apikey parameter in queries for requests to be sent to
// MojoAuth endpoints that only accept "apikey"
func (mojo Mojoauth) NormalizeApiKey(req *httprutils.Request) {
delete(req.QueryParams, "X-API-Key")
req.QueryParams["apikey"] = mojo.Context.ApiKey
}