-
Notifications
You must be signed in to change notification settings - Fork 18
/
request.go
138 lines (119 loc) · 3.4 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cute
import (
"net/url"
)
// RequestBuilder is a function for set options in request
type RequestBuilder func(o *requestOptions)
// File is struct for upload file in form field
// If you set Path, file will read from file system
// If you set Name and Body, file will set from this fields
type File struct {
Path string
Name string
Body []byte
}
type requestOptions struct {
method string
url *url.URL
uri string
headers map[string][]string
query map[string][]string
body []byte
bodyMarshal interface{}
fileForms map[string]*File
forms map[string][]byte
}
func newRequestOptions() *requestOptions {
return &requestOptions{
headers: make(map[string][]string),
query: make(map[string][]string),
fileForms: make(map[string]*File),
forms: make(map[string][]byte),
}
}
// WithMethod is a function for set method (GET, POST ...) in request
func WithMethod(method string) func(o *requestOptions) {
return func(o *requestOptions) {
o.method = method
}
}
// WithURL is a function for set url in request
func WithURL(url *url.URL) func(o *requestOptions) {
return func(o *requestOptions) {
o.url = url
}
}
// WithURI is a function for set url in request
func WithURI(uri string) func(o *requestOptions) {
return func(o *requestOptions) {
o.uri = uri
}
}
// WithHeaders is a function for set or merge headers in request
func WithHeaders(headers map[string][]string) func(o *requestOptions) {
return func(o *requestOptions) {
for key, values := range headers {
o.headers[key] = append(o.headers[key], values...)
}
}
}
// WithHeadersKV is a function for set headers in request
func WithHeadersKV(name string, value string) func(o *requestOptions) {
return func(o *requestOptions) {
o.headers[name] = []string{value}
}
}
// WithQueryKV is a function for set query in request
func WithQueryKV(name string, value string) func(o *requestOptions) {
return func(o *requestOptions) {
o.query[name] = []string{value}
}
}
// WithQuery is a function for set or merge query parameters in request
func WithQuery(queries map[string][]string) func(o *requestOptions) {
return func(o *requestOptions) {
for key, values := range queries {
o.query[key] = values
}
}
}
// WithBody is a function for set body in request
func WithBody(body []byte) func(o *requestOptions) {
return func(o *requestOptions) {
o.body = body
}
}
// WithMarshalBody is a function for marshal body and set body in request
func WithMarshalBody(body interface{}) func(o *requestOptions) {
return func(o *requestOptions) {
o.bodyMarshal = body
}
}
// WithFileFormKV is a function for set file form in request
func WithFileFormKV(name string, file *File) func(o *requestOptions) {
return func(o *requestOptions) {
o.fileForms[name] = file
}
}
// WithFileForm is a function for set file form in request
func WithFileForm(fileForms map[string]*File) func(o *requestOptions) {
return func(o *requestOptions) {
for name, file := range fileForms {
o.fileForms[name] = file
}
}
}
// WithFormKV is a function for set body in form request
func WithFormKV(name string, body []byte) func(o *requestOptions) {
return func(o *requestOptions) {
o.forms[name] = body
}
}
// WithForm is a function for set body in form request
func WithForm(forms map[string][]byte) func(o *requestOptions) {
return func(o *requestOptions) {
for name, body := range forms {
o.forms[name] = body
}
}
}