-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathclient_options.go
124 lines (94 loc) · 2.39 KB
/
client_options.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
package workwx
import (
"net/http"
)
// DefaultQYAPIHost 默认企业微信 API Host
const DefaultQYAPIHost = "https://qyapi.weixin.qq.com"
type options struct {
QYAPIHost string
HTTP *http.Client
AccessTokenProvider ITokenProvider
JSAPITicketProvider ITokenProvider
JSAPITicketAgentConfigProvider ITokenProvider
}
// CtorOption 客户端对象构造参数
type CtorOption interface {
applyTo(*options)
}
// impl Default for options
func defaultOptions() options {
return options{
QYAPIHost: DefaultQYAPIHost,
HTTP: &http.Client{},
AccessTokenProvider: nil,
JSAPITicketProvider: nil,
JSAPITicketAgentConfigProvider: nil,
}
}
//
//
//
type withQYAPIHost struct {
x string
}
// WithQYAPIHost 覆盖默认企业微信 API 域名
func WithQYAPIHost(host string) CtorOption {
return &withQYAPIHost{x: host}
}
var _ CtorOption = (*withQYAPIHost)(nil)
func (x *withQYAPIHost) applyTo(y *options) {
y.QYAPIHost = x.x
}
//
//
//
type withHTTPClient struct {
x *http.Client
}
// WithHTTPClient 使用给定的 http.Client 作为 HTTP 客户端
func WithHTTPClient(client *http.Client) CtorOption {
return &withHTTPClient{x: client}
}
var _ CtorOption = (*withHTTPClient)(nil)
func (x *withHTTPClient) applyTo(y *options) {
y.HTTP = x.x
}
//
//
//
type withAccessTokenProvider struct {
x ITokenProvider
}
func WithAccessTokenProvider(provider ITokenProvider) CtorOption {
return &withAccessTokenProvider{x: provider}
}
var _ CtorOption = (*withAccessTokenProvider)(nil)
func (x *withAccessTokenProvider) applyTo(y *options) {
y.AccessTokenProvider = x.x
}
//
//
//
type withJSAPITicketProvider struct {
x ITokenProvider
}
func WithJSAPITicketProvider(provider ITokenProvider) CtorOption {
return &withJSAPITicketProvider{x: provider}
}
var _ CtorOption = (*withJSAPITicketProvider)(nil)
func (x *withJSAPITicketProvider) applyTo(y *options) {
y.JSAPITicketProvider = x.x
}
//
//
//
type withJSAPITicketAgentConfigProvider struct {
x ITokenProvider
}
func WithJSAPITicketAgentConfigProvider(provider ITokenProvider) CtorOption {
return &withJSAPITicketAgentConfigProvider{x: provider}
}
var _ CtorOption = (*withJSAPITicketAgentConfigProvider)(nil)
func (x *withJSAPITicketAgentConfigProvider) applyTo(y *options) {
y.JSAPITicketAgentConfigProvider = x.x
}