-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttprequest.go
More file actions
195 lines (161 loc) · 4.28 KB
/
httprequest.go
File metadata and controls
195 lines (161 loc) · 4.28 KB
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
package httprequest
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
)
const (
MIMEApplicationJson = "application/json"
MIMEApplicationXml = "application/xml"
MIMETextXml = "text/xml"
HeaderAuthorization = "Authorization"
HeaderContentType = "Content-Type"
)
func New(httpMethod, url string, body interface{}) *RequestBuilder {
return &RequestBuilder{
body: body,
url: url,
httpMethod: httpMethod,
expectedStatusCodes: []int{http.StatusOK},
contentType: MIMEApplicationJson,
}
}
type RequestBuilder struct {
body interface{}
url string
httpMethod string
contentType string
expectedStatusCodes []int
header http.Header
}
func (b *RequestBuilder) Do(ctx context.Context, doer Doer, out interface{}) (*http.Response, error) {
req, err := b.Build(ctx)
if err != nil {
return nil, err
}
resp, err := doer.Do(req)
if err != nil {
return nil, err
}
err = b.validateStatusCode(resp)
if err != nil {
return nil, err
}
err = b.unmarshalResponse(resp, out)
if err != nil {
return nil, err
}
return resp, nil
}
func (b *RequestBuilder) Build(ctx context.Context) (*http.Request, error) {
var body io.Reader
var err error
body, err = b.resolveContentType()
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, b.httpMethod, b.url, body)
if err != nil {
return nil, fmt.Errorf("unable to create request")
}
req.Header = b.header
return req, nil
}
func (b *RequestBuilder) ContentType(contentType string) *RequestBuilder {
b.contentType = contentType
return b
}
func (b *RequestBuilder) StatusIs(status int) *RequestBuilder {
b.expectedStatusCodes = []int{status}
return b
}
func (b *RequestBuilder) StatusIn(statuses []int) *RequestBuilder {
b.expectedStatusCodes = statuses
return b
}
func (b *RequestBuilder) AddHeader(key, value string) *RequestBuilder {
if b.header == nil {
b.header = http.Header{}
}
b.header.Add(key, value)
return b
}
func (b *RequestBuilder) SetHeader(key, value string) *RequestBuilder {
if b.header == nil {
b.header = http.Header{}
}
b.header.Set(key, value)
return b
}
func (b *RequestBuilder) resolveContentType() (body io.Reader, err error) {
if b.body == nil {
return http.NoBody, nil
}
b.SetHeader(HeaderContentType, b.contentType)
// Parse the content type using mime parsing and save the mediatype as the content type
// parameters are currently unsupported and are discarded
b.contentType, _, err = mime.ParseMediaType(b.contentType)
if err != nil {
return nil, fmt.Errorf("unable to parse media type: %v", err)
}
var bodyBytes []byte
switch b.contentType {
case MIMEApplicationJson:
bodyBytes, err = json.Marshal(b.body)
if err != nil {
return nil, fmt.Errorf("unable to marshal body to json: %v", err)
}
body = bytes.NewReader(bodyBytes)
case MIMEApplicationXml, MIMETextXml:
bodyBytes, err = xml.Marshal(b.body)
if err != nil {
return nil, fmt.Errorf("unable to marshal body to xml: %v", err)
}
body = bytes.NewReader(bodyBytes)
default:
return nil, fmt.Errorf("unsupported content type: %s", b.contentType)
}
return body, nil
}
func (b *RequestBuilder) unmarshalResponse(resp *http.Response, out interface{}) error {
respBytes, err := ioutil.ReadAll(resp.Body)
switch b.contentType {
case MIMEApplicationJson:
err = json.Unmarshal(respBytes, &out)
if err != nil {
return fmt.Errorf("unable to unmarshal json body: %v", err)
}
case MIMEApplicationXml, MIMETextXml:
err = xml.Unmarshal(respBytes, &out)
if err != nil {
return fmt.Errorf("unable to unmarshal xml body: %v", err)
}
default:
return fmt.Errorf("unsupported content type: %s", b.contentType)
}
return nil
}
func (b *RequestBuilder) validateStatusCode(resp *http.Response) error {
if len(b.expectedStatusCodes) == 0 {
b.expectedStatusCodes = []int{http.StatusOK}
}
var isExpectedStatus bool
for _, code := range b.expectedStatusCodes {
if isExpectedStatus = resp.StatusCode == code; isExpectedStatus {
break
}
}
if !isExpectedStatus {
return fmt.Errorf("received unexpected status code: %v", resp.StatusCode)
}
return nil
}
type Doer interface {
Do(*http.Request) (*http.Response, error)
}