-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
394 lines (337 loc) · 9.13 KB
/
config.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* Copyright Bytedance Author(https://houseme.github.io/bytedance/). All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You can obtain one at https://github.com/houseme/bytedance.
*
*/
// Package config Configuration information of the DouYin Open Platform
package config
import (
"context"
"github.com/houseme/bytedance/utility/cache"
"github.com/houseme/bytedance/utility/logger"
"github.com/houseme/bytedance/utility/request"
)
const (
// CacheKeyPrefix 抖音 open cache key 前缀
CacheKeyPrefix = "bytedance_douyin_lite"
)
const (
// AccessTokenKey AccessToken Key
AccessTokenKey = "accessTokenKey"
)
// Secret defines the private key type
type Secret uint
const (
PKCS1 Secret = iota
PKCS8
)
// Config 抖音开放平台的配置信息
type Config struct {
version string
cacheKeyPrefix string
clientKey string
clientSecret string
redirectURL string
scopes string
token string
salt string // 支付密钥值
privateKey string // 私钥
publicKey string // 公钥
keyVersion int // 秘钥版本
keyType Secret
cache cache.Cache
request request.Request
logger logger.ILogger
}
type options struct {
CacheKeyPrefix string
ClientKey string
ClientSecret string
RedirectURL string
Scopes string
Token string
Salt string // 支付密钥值
PrivateKey string // 私钥
PublicKey string // 公钥
KeyVersion int // 秘钥版本
KeyType Secret
Cache cache.Cache
Logger logger.ILogger
Request request.Request
}
// Option micro app option
type Option func(*options)
// WithCacheKeyPrefix set cacheKeyPrefix
func WithCacheKeyPrefix(cacheKeyPrefix string) Option {
return func(o *options) {
o.CacheKeyPrefix = cacheKeyPrefix
}
}
// WithClientKey set clientKey
func WithClientKey(clientKey string) Option {
return func(o *options) {
o.ClientKey = clientKey
}
}
// WithClientSecret set clientSecret
func WithClientSecret(clientSecret string) Option {
return func(o *options) {
o.ClientSecret = clientSecret
}
}
// WithRedirectURL set redirectURL
func WithRedirectURL(redirectURL string) Option {
return func(o *options) {
o.RedirectURL = redirectURL
}
}
// WithScopes set scopes
func WithScopes(scopes string) Option {
return func(o *options) {
o.Scopes = scopes
}
}
// WithToken set token
func WithToken(token string) Option {
return func(o *options) {
o.Token = token
}
}
// WithSalt set salt
func WithSalt(salt string) Option {
return func(o *options) {
o.Salt = salt
}
}
// WithPrivateKey set privateKey
func WithPrivateKey(privateKey string) Option {
return func(o *options) {
o.PrivateKey = privateKey
}
}
// WithPublicKey set publicKey
func WithPublicKey(publicKey string) Option {
return func(o *options) {
o.PublicKey = publicKey
}
}
// WithKeyVersion set keyVersion
func WithKeyVersion(keyVersion int) Option {
return func(o *options) {
o.KeyVersion = keyVersion
}
}
// WithKeyType set keyType
func WithKeyType(keyType Secret) Option {
return func(o *options) {
o.KeyType = keyType
}
}
// WithLogger set logger
func WithLogger(logger logger.ILogger) Option {
return func(o *options) {
o.Logger = logger
}
}
// WithRequest set request
func WithRequest(request request.Request) Option {
return func(o *options) {
o.Request = request
}
}
// WithCache set cache
func WithCache(cache cache.Cache) Option {
return func(o *options) {
o.Cache = cache
}
}
// New create config
func New(ctx context.Context, opts ...Option) *Config {
op := options{
Logger: logger.NewDefaultLogger(),
Request: request.NewDefaultRequest(AccessTokenKey),
Cache: cache.NewRedis(ctx, cache.NewDefaultRedisOpts()),
CacheKeyPrefix: CacheKeyPrefix,
}
for _, option := range opts {
option(&op)
}
return &Config{
cacheKeyPrefix: op.CacheKeyPrefix,
clientKey: op.ClientKey,
clientSecret: op.ClientSecret,
redirectURL: op.RedirectURL,
scopes: op.Scopes,
salt: op.Salt,
token: op.Token,
privateKey: op.PrivateKey,
publicKey: op.PublicKey,
keyVersion: op.KeyVersion,
keyType: op.KeyType,
request: op.Request,
logger: op.Logger,
cache: op.Cache,
}
}
// SetVersion 设置 version
func (cfg *Config) SetVersion(version string) *Config {
cfg.version = version
return cfg
}
// SetCacheKeyPrefix 设置 cacheKeyPrefix
func (cfg *Config) SetCacheKeyPrefix(cacheKeyPrefix string) *Config {
cfg.cacheKeyPrefix = cacheKeyPrefix
return cfg
}
// SetClientKey 设置 clientKey
func (cfg *Config) SetClientKey(clientKey string) *Config {
cfg.clientKey = clientKey
return cfg
}
// SetClientSecret 设置 clientSecret
func (cfg *Config) SetClientSecret(clientSecret string) *Config {
cfg.clientSecret = clientSecret
return cfg
}
// SetRedirectURL 设置 redirectURL
func (cfg *Config) SetRedirectURL(redirectURL string) *Config {
cfg.redirectURL = redirectURL
return cfg
}
// SetScopes 设置 scopes
func (cfg *Config) SetScopes(scopes string) *Config {
cfg.scopes = scopes
return cfg
}
// SetSalt 设置 salt
func (cfg *Config) SetSalt(salt string) *Config {
cfg.salt = salt
return cfg
}
// SetToken 设置 token
func (cfg *Config) SetToken(token string) *Config {
cfg.token = token
return cfg
}
// SetPrivateKey 设置 privateKey
func (cfg *Config) SetPrivateKey(privateKey string) *Config {
cfg.privateKey = privateKey
return cfg
}
// SetPublicKey 设置 publicKey
func (cfg *Config) SetPublicKey(publicKey string) *Config {
cfg.publicKey = publicKey
return cfg
}
// SetKeyVersion 设置 keyVersion
func (cfg *Config) SetKeyVersion(keyVersion int) *Config {
cfg.keyVersion = keyVersion
return cfg
}
// SetKeyType 设置 keyType
func (cfg *Config) SetKeyType(keyType Secret) *Config {
cfg.keyType = keyType
return cfg
}
// NewConfig new config
func NewConfig(ctx context.Context, clientKey, clientSecret, redirectURL, scopes, salt, token string) *Config {
return &Config{
clientKey: clientKey,
clientSecret: clientSecret,
redirectURL: redirectURL,
scopes: scopes,
salt: salt,
token: token,
cache: cache.NewRedis(ctx, cache.NewDefaultRedisOpts()),
request: request.NewDefaultRequest(AccessTokenKey),
logger: logger.NewDefaultLogger(),
}
}
// SetCache 设置缓存
func (cfg *Config) SetCache(cache cache.Cache) *Config {
cfg.cache = cache
return cfg
}
// SetRequest 设置请求
func (cfg *Config) SetRequest(request request.Request) *Config {
cfg.request = request
return cfg
}
// SetLogger 设置日志
func (cfg *Config) SetLogger(logger logger.ILogger) *Config {
cfg.logger = logger
return cfg
}
// Version 获取 version
func (cfg *Config) Version() string {
return cfg.version
}
// CacheKeyPrefix 获取 cacheKeyPrefix
func (cfg *Config) CacheKeyPrefix() string {
return cfg.cacheKeyPrefix
}
// ClientKey 获取 clientKey
func (cfg *Config) ClientKey() string {
return cfg.clientKey
}
// ClientSecret 获取 clientSecret
func (cfg *Config) ClientSecret() string {
return cfg.clientSecret
}
// RedirectURL 获取 redirectURL
func (cfg *Config) RedirectURL() string {
return cfg.redirectURL
}
// Scopes 获取 scopes
func (cfg *Config) Scopes() string {
return cfg.scopes
}
// Token 获取 token
func (cfg *Config) Token() string {
return cfg.token
}
// Salt 获取 salt
func (cfg *Config) Salt() string {
return cfg.salt
}
// PrivateKey 获取 privateKey
func (cfg *Config) PrivateKey() string {
return cfg.privateKey
}
// PublicKey 获取 publicKey
func (cfg *Config) PublicKey() string {
return cfg.publicKey
}
// KeyVersion 获取 keyVersion
func (cfg *Config) KeyVersion() int {
return cfg.keyVersion
}
// KeyType 获取 keyType
func (cfg *Config) KeyType() Secret {
return cfg.keyType
}
// Cache 获取 cache
func (cfg *Config) Cache() cache.Cache {
return cfg.cache
}
// Request 获取 request
func (cfg *Config) Request() request.Request {
return cfg.request
}
// Logger 获取 logger
func (cfg *Config) Logger() logger.ILogger {
return cfg.logger
}