forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
77 lines (65 loc) · 2.03 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
package ccv3
import (
"io"
"net/http"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// requestOptions contains all the options to create an HTTP request.
type requestOptions struct {
// URIParams are the list URI route parameters
URIParams internal.Params
// Query is a list of HTTP query parameters. Query will overwrite any
// existing query string in the URI. If you want to preserve the query
// string in URI make sure Query is nil.
Query []Query
// RequestName is the name of the request (see routes)
RequestName string
// Header contains custom headers to pass to the request.
Header http.Header
// Method is the HTTP method.
Method string
// URL is the request path.
URL string
// Body is the content of the request.
Body io.ReadSeeker
}
// newHTTPRequest returns a constructed HTTP.Request with some defaults.
// Defaults are applied when Request options are not filled in.
func (requester *RealRequester) newHTTPRequest(passedRequest requestOptions) (*cloudcontroller.Request, error) {
var request *http.Request
var err error
if passedRequest.URL != "" {
request, err = http.NewRequest(
passedRequest.Method,
passedRequest.URL,
passedRequest.Body,
)
} else {
request, err = requester.router.CreateRequest(
passedRequest.RequestName,
map[string]string(passedRequest.URIParams),
passedRequest.Body,
)
}
if err != nil {
return nil, err
}
if passedRequest.Query != nil {
request.URL.RawQuery = FormatQueryParameters(passedRequest.Query).Encode()
}
request.Header = http.Header{}
if passedRequest.Header != nil {
request.Header = passedRequest.Header
}
if request.Header.Get("User-Agent") == "" {
request.Header.Set("User-Agent", requester.userAgent)
}
if request.Header.Get("Accept") == "" {
request.Header.Set("Accept", "application/json")
}
if request.Header.Get("Content-Type") == "" {
request.Header.Set("Content-Type", "application/json")
}
return cloudcontroller.NewRequest(request, passedRequest.Body), nil
}