-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
103 lines (88 loc) · 3.45 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
package httputil
import (
"net/http"
"strings"
"time"
)
// ClientOptions http client options
type ClientOptions struct {
userAgent string
timeout time.Duration
proxy string
dialTimeout time.Duration
keepAliveTimeout time.Duration
tlsHandshakeTimeout time.Duration
}
// ClientOption http client option
type ClientOption func(*ClientOptions)
// WithUserAgent sed to replace the default UserAgent when making a request.
func WithUserAgent(userAgent string) func(*ClientOptions) {
return func(options *ClientOptions) {
options.userAgent = userAgent
}
}
// WithTimeout If a timeout is set, each HTTP request will use this time as the maximum limit for completing the operation.
// If not set, the default timeout will be 3 minutes.
// If you want to use a different time limit for specific requests, you can override the default timeout by using WithRequestTimeout during the request.
func WithTimeout(timeout time.Duration) func(*ClientOptions) {
return func(options *ClientOptions) {
options.timeout = timeout
}
}
// WithProxy If a proxy is set, each HTTP request will use this proxy to make the request.
func WithProxy(proxy string) func(*ClientOptions) {
return func(options *ClientOptions) {
options.proxy = strings.TrimSpace(proxy)
}
}
// WithDialTimeout If a dial timeout is set, each HTTP request will use this time as the maximum limit for establishing a connection.
func WithDialTimeout(dialTimeout time.Duration) func(*ClientOptions) {
return func(options *ClientOptions) {
options.dialTimeout = dialTimeout
}
}
// WithKeepAliveTimeout If a keep-alive timeout is set, each HTTP request will use this time as the maximum limit for keeping the connection alive.
func WithKeepAliveTimeout(keepAliveTimeout time.Duration) func(*ClientOptions) {
return func(options *ClientOptions) {
options.keepAliveTimeout = keepAliveTimeout
}
}
// WithTLSHandshakeTimeout If a TLS handshake timeout is set, each HTTP request will use this time as the maximum limit for completing the TLS handshake.
func WithTLSHandshakeTimeout(tlsHandshakeTimeout time.Duration) func(*ClientOptions) {
return func(options *ClientOptions) {
options.tlsHandshakeTimeout = tlsHandshakeTimeout
}
}
// RequestOptions http request options
type RequestOptions struct {
headers http.Header
referer string
contentType string
timeout time.Duration
}
// RequestOption http request option
type RequestOption func(*RequestOptions)
// WithHeaders If headers are set, each HTTP request will use these headers to make the request.
func WithHeaders(headers http.Header) func(*RequestOptions) {
return func(options *RequestOptions) {
options.headers = headers
}
}
// WithReferer If a referer is set, each HTTP request will use this referer to make the request.
func WithReferer(referer string) func(*RequestOptions) {
return func(options *RequestOptions) {
options.referer = strings.TrimSpace(referer)
}
}
// WithContentType If a content type is set, each HTTP request will use this content type to make the request.
func WithContentType(contentType string) func(*RequestOptions) {
return func(options *RequestOptions) {
options.contentType = strings.TrimSpace(contentType)
}
}
// WithRequestTimeout If a timeout is set, each HTTP request will use this time as the maximum limit for completing the operation.
func WithRequestTimeout(timeout time.Duration) func(*RequestOptions) {
return func(options *RequestOptions) {
options.timeout = timeout
}
}