/
client_chain.go
177 lines (161 loc) · 4.42 KB
/
client_chain.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package client
import (
"context"
"time"
)
// Prefix is a chaining function,
// which sets the URL prefix for next request of this client.
func (c *Client) Prefix(prefix string) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetPrefix(prefix)
return newClient
}
// Header is a chaining function,
// which sets custom HTTP headers with map for next request.
func (c *Client) Header(m map[string]string) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetHeaderMap(m)
return newClient
}
// HeaderRaw is a chaining function,
// which sets custom HTTP header using raw string for next request.
func (c *Client) HeaderRaw(headers string) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetHeaderRaw(headers)
return newClient
}
// Cookie is a chaining function,
// which sets cookie items with map for next request.
func (c *Client) Cookie(m map[string]string) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetCookieMap(m)
return newClient
}
// ContentType is a chaining function,
// which sets HTTP content type for the next request.
func (c *Client) ContentType(contentType string) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetContentType(contentType)
return newClient
}
// ContentJson is a chaining function,
// which sets the HTTP content type as "application/json" for the next request.
//
// Note that it also checks and encodes the parameter to JSON format automatically.
func (c *Client) ContentJson() *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetContentType("application/json")
return newClient
}
// ContentXml is a chaining function,
// which sets the HTTP content type as "application/xml" for the next request.
//
// Note that it also checks and encodes the parameter to XML format automatically.
func (c *Client) ContentXml() *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetContentType("application/xml")
return newClient
}
// Timeout is a chaining function,
// which sets the timeout for next request.
func (c *Client) Timeout(t time.Duration) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetTimeout(t)
return newClient
}
// BasicAuth is a chaining function,
// which sets HTTP basic authentication information for next request.
func (c *Client) BasicAuth(user, pass string) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetBasicAuth(user, pass)
return newClient
}
// Ctx is a chaining function,
// which sets context for next request of this client.
func (c *Client) Ctx(ctx context.Context) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetCtx(ctx)
return newClient
}
// Retry is a chaining function,
// which sets retry count and interval when failure for next request.
func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetRetry(retryCount, retryInterval)
return newClient
}
// Dump is a chaining function,
// which enables/disables dump feature for this request.
func (c *Client) Dump(dump ...bool) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
if len(dump) > 0 {
newClient.SetDump(dump[0])
} else {
newClient.SetDump(true)
}
return newClient
}
// Proxy is a chaining function,
// which sets proxy for next request.
// Make sure you pass the correct `proxyURL`.
// The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.
// Only `http` and `socks5` proxies are supported currently.
func (c *Client) Proxy(proxyURL string) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetProxy(proxyURL)
return newClient
}
// RedirectLimit is a chaining function,
// which sets the redirect limit the number of jumps for the request.
func (c *Client) RedirectLimit(redirectLimit int) *Client {
newClient := c
if c.parent == nil {
newClient = c.Clone()
}
newClient.SetRedirectLimit(redirectLimit)
return newClient
}