-
Notifications
You must be signed in to change notification settings - Fork 30
/
request.go
119 lines (101 loc) · 2.64 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
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
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"github.com/spotinst/spotinst-sdk-go/spotinst"
)
type Request struct {
Obj interface{}
Params url.Values
url *url.URL
method string
body io.Reader
header http.Header
}
// toHTTP converts the request to an HTTP request.
func (r *Request) toHTTP(ctx context.Context, cfg *spotinst.Config) (*http.Request, error) {
// Set the user credentials.
creds, err := cfg.Credentials.Get()
if err != nil {
return nil, err
}
if creds.Token != "" {
r.header.Set("Authorization", "Bearer "+creds.Token)
}
if creds.Account != "" {
r.Params.Set("accountId", creds.Account)
}
// Encode the query parameters.
r.url.RawQuery = r.Params.Encode()
// Check if we should encode the body.
if r.body == nil && r.Obj != nil {
if b, err := EncodeBody(r.Obj); err != nil {
return nil, err
} else {
r.body = b
}
}
// Create the HTTP request.
req, err := http.NewRequest(r.method, r.url.RequestURI(), r.body)
if err != nil {
return nil, err
}
// Set request base URL.
req.URL.Host = cfg.BaseURL.Host
req.URL.Scheme = cfg.BaseURL.Scheme
// Set request headers.
req.Host = cfg.BaseURL.Host
req.Header = r.header
req.Header.Set("Content-Type", cfg.ContentType)
req.Header.Add("Accept", cfg.ContentType)
req.Header.Add("User-Agent", cfg.UserAgent)
return req.WithContext(ctx), nil
}
// EncodeBody is used to encode a request body
func EncodeBody(obj interface{}) (io.Reader, error) {
buf := bytes.NewBuffer(nil)
if err := json.NewEncoder(buf).Encode(obj); err != nil {
return nil, err
}
return buf, nil
}
// toHTTP converts the request to an HTTP request.
func (r *Request) toHTTPOrg(ctx context.Context, cfg *spotinst.Config) (*http.Request, error) {
// Set the user credentials.
creds, err := cfg.Credentials.Get()
if err != nil {
return nil, err
}
if creds.Token != "" {
r.header.Set("Authorization", "Bearer "+creds.Token)
}
// Encode the query parameters.
r.url.RawQuery = r.Params.Encode()
// Check if we should encode the body.
if r.body == nil && r.Obj != nil {
if b, err := EncodeBody(r.Obj); err != nil {
return nil, err
} else {
r.body = b
}
}
// Create the HTTP request.
req, err := http.NewRequest(r.method, r.url.RequestURI(), r.body)
if err != nil {
return nil, err
}
// Set request base URL.
req.URL.Host = cfg.BaseURL.Host
req.URL.Scheme = cfg.BaseURL.Scheme
// Set request headers.
req.Host = cfg.BaseURL.Host
req.Header = r.header
req.Header.Set("Content-Type", cfg.ContentType)
req.Header.Add("Accept", cfg.ContentType)
req.Header.Add("User-Agent", cfg.UserAgent)
return req.WithContext(ctx), nil
}