-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoaxios.go
executable file
·241 lines (211 loc) · 4.88 KB
/
goaxios.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
package goaxios
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"
)
// RunRest is a tiny wrapper around Go's *http.Request object to make it quicker to run REST http requests.
// It returns the *http.Response object, the response body as byte, the unmarshalled response body and an error object (if any or nil)
func (ga *GoAxios) RunRest() Response {
if ga.Interceptor.Request != nil {
ga.Interceptor.Request(ga)
}
// TODO: improve validate before request
err := ga.validateBeforeRequest()
if err != nil {
return Response{
Response: nil,
Bytes: nil,
Body: nil,
Error: err,
}
}
// replace path params
for k, v := range ga.Params {
ga.Url = strings.Replace(ga.Url, ":"+k, v, -1)
}
// parse query params
for k, v := range ga.Query {
if strings.HasSuffix(ga.Url, "?") {
ga.Url += k + "=" + v + "&"
} else if strings.HasSuffix(ga.Url, "&") {
ga.Url += k + "=" + v + "&"
} else {
ga.Url += "?" + k + "=" + v + "&"
}
}
ga.Url = strings.TrimSuffix(ga.Url, "&")
// fake http response
var fail *http.Response
// fake response body
var body []byte
// response body
var response interface{}
if ga.ResponseStruct != nil {
response = ga.ResponseStruct
}
// parse body
// reqBody := strings.NewReader(ga.Body)
reqBody, err := json.Marshal(ga.Body)
if err != nil {
return Response{
Response: fail,
Bytes: body,
Body: response,
Error: err,
}
}
client := &http.Client{
Timeout: ga.Timeout,
}
req, err := http.NewRequest(strings.ToUpper(ga.Method), ga.Url, nil)
if err != nil {
return Response{
Response: fail,
Bytes: body,
Body: response,
Error: err,
}
}
// add headers
for k, v := range ga.Headers {
req.Header.Add(k, v)
}
// add body
if ga.IsMultiPart || ga.Form != nil {
r, w := io.Pipe()
writer := multipart.NewWriter(w)
go func() {
defer w.Close()
defer writer.Close()
for _, pf := range ga.Form.Files {
var file io.ReadCloser
var err error
// open file
if pf.Path != "" && pf.Handle == nil {
file, err = os.Open(pf.Path)
if err != nil {
return
}
} else {
file = pf.Handle
}
// close file
defer file.Close()
part, err := writer.CreateFormFile(pf.Key, pf.Name)
if err != nil {
return
}
_, err = io.Copy(part, file)
if err != nil {
return
}
}
for _, pd := range ga.Form.Data {
_ = writer.WriteField(pd.Key, pd.Value)
}
}()
req.Body = r
req.Header.Add("Content-Type", writer.FormDataContentType())
} else {
if ga.Body != nil {
closerBody := io.NopCloser(bytes.NewReader(reqBody))
req.Body = closerBody
}
if ga.Headers == nil {
req.Header.Add("Content-Type", "application/json")
}
}
// add bearer token
if ga.BearerToken != "" {
req.Header.Add("Authorization", "Bearer "+ga.BearerToken)
}
res, err := client.Do(req)
if err != nil {
return Response{
Response: res,
Bytes: body,
Body: response,
Error: err,
}
}
if ga.Interceptor.Response != nil {
ga.Interceptor.Response(res)
}
defer res.Body.Close()
// handle download
if ga.IsDownload {
if ga.DownloadDestination.Location != "" {
out, err := os.Create(ga.DownloadDestination.Location)
if err != nil {
return Response{
Response: res,
Bytes: body,
Body: response,
Error: err,
}
}
defer out.Close()
_, err = io.Copy(out, res.Body)
if err != nil {
return Response{
Response: res,
Bytes: body,
Body: response,
Error: err,
}
}
} else if ga.DownloadDestination.Writer != nil {
_, err = io.Copy(ga.DownloadDestination.Writer, res.Body)
if err != nil {
return Response{
Response: res,
Bytes: body,
Body: response,
Error: err,
}
}
} else {
return Response{
Response: res,
Bytes: body,
Body: response,
Error: errors.New("download destination not provided"),
}
}
return Response{
Response: res,
Bytes: body,
Body: response,
Error: nil,
}
} else {
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return Response{
Response: res,
Bytes: body,
Body: response,
Error: err,
}
}
// unmarshall
contentType := res.Header.Get("Content-Type")
return ga.performResponseMarshalling(contentType, response, data, body, err, res)
}
}
// RunGraphQL is a wrapper around Go's *http.Request object to make it faster to run GraphQL http requests.
// It returns the *http.Response object, the response body as byte, the unmarshalled response body and an error object (if any or nil)
func (ga *GoAxios) RunGraphQL() (*http.Response, []byte, interface{}, error) {
return new(http.Response), *new([]uint8), new(interface{}), nil
}
// URL is a getter for the URL property
func (ga *GoAxios) URL() string {
return ga.Url
}