-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
174 lines (145 loc) · 3.42 KB
/
option.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
package requests
import (
"bytes"
"context"
"fmt"
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
)
type ClientOption func(client *Client)
func WithTimeout(timeout time.Duration) ClientOption {
return func(client *Client) { client.Timeout = timeout }
}
func WithTransport(transport http.RoundTripper) ClientOption {
return func(client *Client) { client.Transport = transport }
}
func WithCheckRedirect(checkRedirect func(req *http.Request, via []*http.Request) error) ClientOption {
return func(client *Client) { client.CheckRedirect = checkRedirect }
}
func WithJar(jar http.CookieJar) ClientOption {
return func(client *Client) { client.Jar = jar }
}
type ReqOption interface {
Do(req *Request) error
}
type Header map[string]string
func (h Header) Do(req *Request) error {
for key, value := range h {
req.Header.Set(key, value)
}
return nil
}
type Params map[string]string
func (p Params) Do(req *Request) error {
if len(p) == 0 {
return nil
}
if req.URL.RawQuery != "" {
req.URL.RawQuery += "&"
}
values := url.Values{}
for key, value := range p {
values.Set(key, value)
}
req.URL.RawQuery += values.Encode()
return nil
}
type Json map[string]interface{}
func (j Json) Do(req *Request) error {
if req.files != nil || req.form != nil {
return ErrInvalidBodyType
}
if req.json == nil {
req.json = make(Json)
}
for k, v := range j {
req.json[k] = v
}
return nil
}
type Jsons []Json
func (j Jsons) Do(req *Request) error {
if req.files != nil || req.form != nil {
return ErrInvalidBodyType
}
req.jsons = append(req.jsons, j...)
return nil
}
type Form map[string]string
func (f Form) Do(req *Request) error {
if req.json != nil || req.jsons != nil {
return ErrInvalidBodyType
}
if req.form == nil {
req.form = make(Form)
}
for k, v := range f {
req.form[k] = v
}
return nil
}
type Cookies map[string]string
func (c Cookies) Do(req *Request) error {
for name, value := range c {
req.AddCookie(&http.Cookie{Name: name, Value: value})
}
return nil
}
type file struct {
field string // file field
content io.ReadCloser
name string // file name
filePath string
}
func FileWithPath(field string, path string) *file {
return &file{filePath: path, field: field, name: filepath.Base(path)}
}
func FileWithContent(field, fileName string, content []byte) *file {
return &file{field: field, name: fileName, content: ioutil.NopCloser(bytes.NewReader(content))}
}
func (f file) Do(req *Request) error {
if f.field == "" {
return errors.Wrap(ErrInvalidFile, "field is nil")
} else if f.name == "" {
return errors.Wrap(ErrInvalidFile, "fileName is nil")
}
if f.content == nil && f.filePath == "" {
return errors.Wrap(ErrInvalidFile, "content is nil, path is nil")
} else if f.content == nil {
fc, err := os.Open(f.filePath)
if err != nil {
return errors.Wrap(ErrInvalidFile, fmt.Sprintf("open file err: %v", err))
}
f.content = fc
}
req.files = append(req.files, &f)
return nil
}
type Ctx struct {
context.Context
}
func (c Ctx) Do(req *Request) error {
req.Request = req.WithContext(c)
return nil
}
type Timeout time.Duration
func (t Timeout) Do(req *Request) error {
if time.Duration(t) == 0 {
return nil
}
ctx, _ := context.WithTimeout(req.Context(), time.Duration(t))
// todo call cancel
req.Request = req.WithContext(ctx)
return nil
}
type Gzip struct{}
func (Gzip) Do(req *Request) error {
req.gzip = true
return nil
}