5
5
"encoding/json"
6
6
"errors"
7
7
"fmt"
8
+ "io"
8
9
"io/ioutil"
9
10
"mime/multipart"
10
11
"net/http"
@@ -64,10 +65,6 @@ func NewAPI(t authorization.TokenGetter, client *http.Client, clientID string, r
64
65
65
66
// Call sends request to API with given action
66
67
func (a * api ) Call (action string , reqPayload interface {}, respPayload interface {}) error {
67
- rawBody , err := json .Marshal (reqPayload )
68
- if err != nil {
69
- return err
70
- }
71
68
token , err := a .getToken ()
72
69
if err != nil {
73
70
return err
@@ -77,7 +74,15 @@ func (a *api) Call(action string, reqPayload interface{}, respPayload interface{
77
74
if err != nil {
78
75
return fmt .Errorf ("couldn't create new http request: %v" , err )
79
76
}
80
- req .Body = ioutil .NopCloser (bytes .NewReader (rawBody ))
77
+
78
+ rawBody , err := json .Marshal (reqPayload )
79
+ if err != nil {
80
+ return err
81
+ }
82
+ req .GetBody = func () (io.ReadCloser , error ) {
83
+ return ioutil .NopCloser (bytes .NewReader (rawBody )), nil
84
+ }
85
+ req .Body , _ = req .GetBody ()
81
86
82
87
req .Header .Set ("Content-Type" , "application/json" )
83
88
req .Header .Set ("Authorization" , fmt .Sprintf ("%s %s" , token .Type , token .AccessToken ))
@@ -119,6 +124,17 @@ func NewAPIWithFileUpload(t authorization.TokenGetter, client *http.Client, clie
119
124
// Returned URL shall be used in call to SendFile or SendEvent or it'll become invalid
120
125
// in about 24 hours.
121
126
func (a * fileUploadAPI ) UploadFile (filename string , file []byte ) (string , error ) {
127
+ token := a .tokenGetter ()
128
+ if token == nil {
129
+ return "" , fmt .Errorf ("couldn't get token" )
130
+ }
131
+
132
+ req , err := a .httpRequestGenerator (token , a .host , "upload_file" )
133
+ if err != nil {
134
+ return "" , fmt .Errorf ("couldn't create new http request: %v" , err )
135
+ }
136
+ req .Method = "POST"
137
+
122
138
body := & bytes.Buffer {}
123
139
writer := multipart .NewWriter (body )
124
140
w , err := writer .CreateFormFile ("file" , filename )
@@ -131,17 +147,11 @@ func (a *fileUploadAPI) UploadFile(filename string, file []byte) (string, error)
131
147
if err := writer .Close (); err != nil {
132
148
return "" , fmt .Errorf ("couldn't close multipart writer: %v" , err )
133
149
}
134
- token := a .tokenGetter ()
135
- if token == nil {
136
- return "" , fmt .Errorf ("couldn't get token" )
137
- }
138
150
139
- req , err := a .httpRequestGenerator (token , a .host , "upload_file" )
140
- if err != nil {
141
- return "" , fmt .Errorf ("couldn't create new http request: %v" , err )
151
+ req .GetBody = func () (io.ReadCloser , error ) {
152
+ return ioutil .NopCloser (bytes .NewReader (body .Bytes ())), nil
142
153
}
143
- req .Method = "POST"
144
- req .Body = ioutil .NopCloser (body )
154
+ req .Body , _ = req .GetBody ()
145
155
146
156
req .Header .Set ("Content-Type" , writer .FormDataContentType ())
147
157
req .Header .Set ("Authorization" , fmt .Sprintf ("%s %s" , token .Type , token .AccessToken ))
@@ -185,6 +195,14 @@ func (a *api) send(req *http.Request, respPayload interface{}) error {
185
195
}
186
196
187
197
req .Header .Set ("Authorization" , fmt .Sprintf ("%s %s" , token .Type , token .AccessToken ))
198
+ if req .Body != nil {
199
+ reqBody , err := req .GetBody ()
200
+ if err != nil {
201
+ return fmt .Errorf ("couldn't get request body: %v" , err )
202
+ }
203
+ req .Body = reqBody
204
+ }
205
+
188
206
attempts ++
189
207
return do ()
190
208
}
0 commit comments