/
requestbuilder.go
215 lines (183 loc) 路 4.8 KB
/
requestbuilder.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
package core
import (
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"path/filepath"
"strings"
)
type RequestBuilder struct {
Query url.Values
Form url.Values
Attachment map[string][]FileMarshaler
Header http.Header
Cookie []*http.Cookie
Path map[string]string // placeholder: value
BodyType string // json, xml, etc.
Body io.ReadCloser
ctx context.Context
}
func NewRequestBuilder(ctx context.Context) *RequestBuilder {
return &RequestBuilder{
Query: make(url.Values),
Form: make(url.Values),
Attachment: make(map[string][]FileMarshaler),
Header: make(http.Header),
Cookie: make([]*http.Cookie, 0),
Path: make(map[string]string),
ctx: ctx,
}
}
func (rb *RequestBuilder) Populate(req *http.Request) error {
if err := rb.validate(); err != nil {
return err
}
// Populate the querystring.
req.URL.RawQuery = rb.Query.Encode()
// Populate forms.
if rb.hasForm() {
if rb.hasAttachment() { // multipart form
if err := rb.populateMultipartForm(req); err != nil {
return err
}
} else { // urlencoded form
rb.populateForm(req)
}
}
// Populate body.
if rb.hasBody() {
req.Body = rb.Body
rb.Header.Set("Content-Type", rb.bodyContentType())
}
// Populate path.
if rb.hasPath() {
newPath := req.URL.Path
for key, value := range rb.Path {
newPath = strings.Replace(newPath, "{"+key+"}", value, -1)
}
req.URL.Path = newPath
req.URL.RawPath = ""
}
// Populate the headers.
if rb.Header != nil {
req.Header = rb.Header
}
// Populate the cookies.
for _, cookie := range rb.Cookie {
req.AddCookie(cookie)
}
return nil
}
func (rb *RequestBuilder) SetQuery(key string, value []string) {
rb.Query[key] = value
}
func (rb *RequestBuilder) SetForm(key string, value []string) {
rb.Form[key] = value
}
func (rb *RequestBuilder) SetHeader(key string, value []string) {
rb.Header[http.CanonicalHeaderKey(key)] = value
}
func (rb *RequestBuilder) SetPath(key string, value []string) {
if len(value) > 0 {
rb.Path[key] = value[0]
}
}
func (rb *RequestBuilder) SetBody(bodyType string, bodyReader io.ReadCloser) {
rb.BodyType = bodyType
rb.Body = bodyReader
}
func (rb *RequestBuilder) SetAttachment(key string, files []FileMarshaler) {
rb.Attachment[key] = files
}
func (rb *RequestBuilder) bodyContentType() string {
switch rb.BodyType {
case "json":
return "application/json"
case "xml":
return "application/xml"
}
return ""
}
func (rb *RequestBuilder) validate() error {
if rb.hasForm() && rb.hasBody() {
return errors.New("cannot use both form and body directive at the same time")
}
return nil
}
func (rb *RequestBuilder) hasPath() bool {
return len(rb.Path) > 0
}
func (rb *RequestBuilder) hasForm() bool {
return len(rb.Form) > 0 || rb.hasAttachment()
}
func (rb *RequestBuilder) hasAttachment() bool {
return len(rb.Attachment) > 0
}
func (rb *RequestBuilder) hasBody() bool {
return rb.Body != nil && rb.BodyType != ""
}
func (rb *RequestBuilder) populateForm(req *http.Request) {
rb.Header.Set("Content-Type", "application/x-www-form-urlencoded")
formData := rb.Form.Encode()
req.Body = io.NopCloser(strings.NewReader(formData))
}
func (rb *RequestBuilder) populateMultipartForm(req *http.Request) error {
// Create a pipe and a multipart writer.
pr, pw := io.Pipe()
writer := multipart.NewWriter(pw)
// Write the multipart form data to the pipe in a separate goroutine.
go func() {
defer pw.Close()
defer writer.Close()
// Populate the form fields.
for k, v := range rb.Form {
for _, sv := range v {
select {
case <-rb.ctx.Done():
pw.CloseWithError(rb.ctx.Err())
return
default:
fieldWriter, _ := writer.CreateFormField(k)
fieldWriter.Write([]byte(sv))
}
}
}
// Populate the attachments.
for key, files := range rb.Attachment {
for i, file := range files {
select {
case <-rb.ctx.Done():
pw.CloseWithError(rb.ctx.Err())
return
default:
filename := file.Filename()
contentReader, err := file.MarshalFile()
filename = normalizeUploadFilename(key, filename, i)
if err != nil {
pw.CloseWithError(fmt.Errorf("upload %s %q failed: %w", key, filename, err))
return
}
fileWriter, _ := writer.CreateFormFile(key, filename)
if _, err = io.Copy(fileWriter, contentReader); err != nil {
pw.CloseWithError(fmt.Errorf("upload %s %q failed: %w", key, filename, err))
return
}
}
}
}
}()
// Set the body to the read end of the pipe and the content type.
req.Body = io.NopCloser(pr)
rb.Header.Set("Content-Type", writer.FormDataContentType())
return nil
}
func normalizeUploadFilename(key, filename string, index int) string {
if filename == "" {
return fmt.Sprintf("%s_%d", key, index)
}
return filepath.Base(filename)
}