-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
61 lines (52 loc) · 1.42 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
package ccv2
import (
"fmt"
"io"
"net/http"
"net/url"
)
// requestOptions contains all the options to create an HTTP request.
type requestOptions struct {
// URIParams are the list URI route parameters
URIParams map[string]string
// Query is a list of HTTP query parameters
Query url.Values
// RequestName is the name of the request (see routes)
RequestName string
// URI is the URI of the request.
URI string
// Method is the HTTP method of the request.
Method string
// Body is the request body
Body io.Reader
}
// newHTTPRequest returns a constructed HTTP.Request with some defaults.
// Defaults are applied when Request fields are not filled in.
func (client Client) newHTTPRequest(passedRequest requestOptions) (*http.Request, error) {
var request *http.Request
var err error
if passedRequest.URI != "" {
request, err = http.NewRequest(
passedRequest.Method,
fmt.Sprintf("%s%s", client.API(), passedRequest.URI),
passedRequest.Body,
)
} else {
request, err = client.router.CreateRequest(
passedRequest.RequestName,
passedRequest.URIParams,
passedRequest.Body,
)
if err == nil {
request.URL.RawQuery = passedRequest.Query.Encode()
}
}
if err != nil {
return nil, err
}
request.Header = http.Header{}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("User-Agent", client.userAgent)
return request, nil
}