forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequester.go
304 lines (249 loc) · 7.29 KB
/
requester.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
package ccv3
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"runtime"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Requester
type RequestParams struct {
RequestName string
URIParams internal.Params
Query []Query
RequestBody interface{}
ResponseBody interface{}
URL string
AppendToList func(item interface{}) error
}
type Requester interface {
InitializeConnection(settings TargetSettings)
InitializeRouter(baseURL string)
MakeListRequest(requestParams RequestParams) (IncludedResources, Warnings, error)
MakeRequest(requestParams RequestParams) (JobURL, Warnings, error)
MakeRequestReceiveRaw(
requestName string,
uriParams internal.Params,
responseBodyMimeType string,
) ([]byte, Warnings, error)
MakeRequestSendRaw(
requestName string,
uriParams internal.Params,
requestBody []byte,
requestBodyMimeType string,
responseBody interface{},
) (string, Warnings, error)
MakeRequestSendReceiveRaw(
Method string,
URL string,
headers http.Header,
requestBody []byte,
) ([]byte, *http.Response, error)
MakeRequestUploadAsync(
requestName string,
uriParams internal.Params,
requestBodyMimeType string,
requestBody io.ReadSeeker,
dataLength int64,
responseBody interface{},
writeErrors <-chan error,
) (string, Warnings, error)
WrapConnection(wrapper ConnectionWrapper)
}
type RealRequester struct {
connection cloudcontroller.Connection
router *internal.Router
userAgent string
wrappers []ConnectionWrapper
}
func (requester *RealRequester) InitializeConnection(settings TargetSettings) {
requester.connection = cloudcontroller.NewConnection(cloudcontroller.Config{
DialTimeout: settings.DialTimeout,
SkipSSLValidation: settings.SkipSSLValidation,
})
for _, wrapper := range requester.wrappers {
requester.connection = wrapper.Wrap(requester.connection)
}
}
func (requester *RealRequester) InitializeRouter(baseURL string) {
requester.router = internal.NewRouter(internal.APIRoutes, baseURL)
}
func (requester *RealRequester) MakeListRequest(requestParams RequestParams) (IncludedResources, Warnings, error) {
request, err := requester.buildRequest(requestParams)
if err != nil {
return IncludedResources{}, nil, err
}
return requester.paginate(request, requestParams.ResponseBody, requestParams.AppendToList)
}
func (requester *RealRequester) MakeRequest(requestParams RequestParams) (JobURL, Warnings, error) {
request, err := requester.buildRequest(requestParams)
if err != nil {
return "", nil, err
}
response := cloudcontroller.Response{}
if requestParams.ResponseBody != nil {
response.DecodeJSONResponseInto = requestParams.ResponseBody
}
err = requester.connection.Make(request, &response)
return JobURL(response.ResourceLocationURL), response.Warnings, err
}
func (requester *RealRequester) MakeRequestReceiveRaw(
requestName string,
uriParams internal.Params,
responseBodyMimeType string,
) ([]byte, Warnings, error) {
request, err := requester.newHTTPRequest(requestOptions{
RequestName: requestName,
URIParams: uriParams,
})
if err != nil {
return nil, nil, err
}
response := cloudcontroller.Response{}
request.Header.Set("Accept", responseBodyMimeType)
err = requester.connection.Make(request, &response)
return response.RawResponse, response.Warnings, err
}
func (requester *RealRequester) MakeRequestSendReceiveRaw(
Method string,
URL string,
headers http.Header,
requestBody []byte,
) ([]byte, *http.Response, error) {
request, err := requester.newHTTPRequest(requestOptions{
URL: URL,
Method: Method,
Body: bytes.NewReader(requestBody),
Header: headers,
})
if err != nil {
return nil, nil, err
}
response := cloudcontroller.Response{}
err = requester.connection.Make(request, &response)
return response.RawResponse, response.HTTPResponse, err
}
func (requester *RealRequester) MakeRequestSendRaw(
requestName string,
uriParams internal.Params,
requestBody []byte,
requestBodyMimeType string,
responseBody interface{},
) (string, Warnings, error) {
request, err := requester.newHTTPRequest(requestOptions{
RequestName: requestName,
URIParams: uriParams,
Body: bytes.NewReader(requestBody),
})
if err != nil {
return "", nil, err
}
request.Header.Set("Content-type", requestBodyMimeType)
response := cloudcontroller.Response{
DecodeJSONResponseInto: responseBody,
}
err = requester.connection.Make(request, &response)
return response.ResourceLocationURL, response.Warnings, err
}
func (requester *RealRequester) MakeRequestUploadAsync(
requestName string,
uriParams internal.Params,
requestBodyMimeType string,
requestBody io.ReadSeeker,
dataLength int64,
responseBody interface{},
writeErrors <-chan error,
) (string, Warnings, error) {
request, err := requester.newHTTPRequest(requestOptions{
RequestName: requestName,
URIParams: uriParams,
Body: requestBody,
})
if err != nil {
return "", nil, err
}
request.Header.Set("Content-Type", requestBodyMimeType)
request.ContentLength = dataLength
return requester.uploadAsynchronously(request, responseBody, writeErrors)
}
func NewRequester(config Config) *RealRequester {
userAgent := fmt.Sprintf(
"%s/%s (%s; %s %s)",
config.AppName,
config.AppVersion,
runtime.Version(),
runtime.GOARCH,
runtime.GOOS,
)
return &RealRequester{
userAgent: userAgent,
wrappers: append([]ConnectionWrapper{newErrorWrapper()}, config.Wrappers...),
}
}
func (requester *RealRequester) buildRequest(requestParams RequestParams) (*cloudcontroller.Request, error) {
options := requestOptions{
RequestName: requestParams.RequestName,
URIParams: requestParams.URIParams,
Query: requestParams.Query,
URL: requestParams.URL,
}
if requestParams.RequestBody != nil {
body, err := json.Marshal(requestParams.RequestBody)
if err != nil {
return nil, err
}
options.Body = bytes.NewReader(body)
}
request, err := requester.newHTTPRequest(options)
if err != nil {
return nil, err
}
return request, err
}
func (requester *RealRequester) uploadAsynchronously(request *cloudcontroller.Request, responseBody interface{}, writeErrors <-chan error) (string, Warnings, error) {
response := cloudcontroller.Response{
DecodeJSONResponseInto: responseBody,
}
httpErrors := make(chan error)
go func() {
defer close(httpErrors)
err := requester.connection.Make(request, &response)
if err != nil {
httpErrors <- err
}
}()
// The following section makes the following assumptions:
// 1) If an error occurs during file reading, an EOF is sent to the request
// object. Thus ending the request transfer.
// 2) If an error occurs during request transfer, an EOF is sent to the pipe.
// Thus ending the writing routine.
var firstError error
var writeClosed, httpClosed bool
for {
select {
case writeErr, ok := <-writeErrors:
if !ok {
writeClosed = true
break // for select
}
if firstError == nil {
firstError = writeErr
}
case httpErr, ok := <-httpErrors:
if !ok {
httpClosed = true
break // for select
}
if firstError == nil {
firstError = httpErr
}
}
if writeClosed && httpClosed {
break // for for
}
}
return response.ResourceLocationURL, response.Warnings, firstError
}