-
Notifications
You must be signed in to change notification settings - Fork 143
/
baseClient.go
800 lines (653 loc) · 20.8 KB
/
baseClient.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
package kernel
import (
"bytes"
"context"
"crypto"
"crypto/sha1"
"errors"
"fmt"
fmt2 "github.com/ArtisanCloud/PowerLibs/v3/fmt"
"github.com/ArtisanCloud/PowerLibs/v3/http/contract"
"github.com/ArtisanCloud/PowerLibs/v3/http/helper"
contract2 "github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
"github.com/ArtisanCloud/PowerLibs/v3/object"
os2 "github.com/ArtisanCloud/PowerLibs/v3/os"
"github.com/ArtisanCloud/PowerLibs/v3/security/sign"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
response2 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/support"
"io"
"log"
"net/http"
"os"
)
type BaseClient struct {
kernel.BaseClient
RsaOAEP *sign.RSASigner
App *ApplicationPaymentInterface
}
func NewBaseClient(app *ApplicationPaymentInterface) (*BaseClient, error) {
config := (*app).GetConfig()
baseURI := config.GetString("http.base_uri", "/")
httpRequest, err := helper.NewRequestHelper(&helper.Config{
BaseUrl: baseURI,
})
if err != nil {
return nil, err
}
client := &BaseClient{
BaseClient: kernel.BaseClient{
HttpHelper: httpRequest,
Signer: &support.SHA256WithRSASigner{
MchID: config.GetString("mch_id", ""),
CertificateSerialNo: config.GetString("serial_no", ""),
PrivateKeyPath: config.GetString("key_path", ""),
},
},
App: app,
}
client.RsaOAEP, err = sign.NewRSASigner(crypto.SHA1)
if err != nil {
return nil, err
}
RSAPublicKeyPath := config.GetString("certificate_key_path", "")
if RSAPublicKeyPath != "" {
client.RsaOAEP.RSAEncryptor.PublicKeyPath = RSAPublicKeyPath
_, err = client.RsaOAEP.RSAEncryptor.LoadPublicKeyByPath()
if err != nil {
return nil, err
}
}
// to be setup middleware here
client.OverrideGetMiddlewares()
client.RegisterHttpMiddlewares()
return client, nil
}
func (client *BaseClient) prepends() *object.HashMap {
return &object.HashMap{}
}
func (client *BaseClient) PlainRequest(ctx context.Context, endpoint string, params *object.StringMap, method string, options *object.HashMap,
returnRaw bool, outHeader interface{}, outBody interface{},
) (response *http.Response, err error) {
//config := (*client.App).GetConfig()
base := &object.HashMap{}
// init options
if options == nil {
options = &object.HashMap{}
}
options = object.MergeHashMap(base, client.prepends(), options)
options = object.FilterEmptyHashMap(options)
// check need sign body or not
signBody := ""
if "get" != object.Lower(method) {
signBody, err = object.JsonEncode(options)
if err != nil {
return nil, err
}
}
authorization, err := client.Signer.GenerateRequestSign(&support.RequestSignChain{
Method: method,
CanonicalURL: endpoint,
SignBody: signBody,
})
if err != nil {
return nil, err
}
df := client.HttpHelper.Df().
WithContext(ctx).
Uri(endpoint).Method(method)
// 检查是否需要有请求参数配置
if options != nil {
// set query key values
if (*options)["query"] != nil {
queries := (*options)["query"].(*object.StringMap)
if queries != nil {
for k, v := range *queries {
df.Query(k, v)
}
}
}
config := (*client.App).GetConfig()
// 微信如果需要传debug模式
debug := config.GetBool("debug", false)
if debug {
df.Query("debug", "1")
}
// set body json
if signBody != "" {
r := bytes.NewBufferString(signBody)
df.Body(r)
}
// set header
df.
Header("content-type", "application/json").
Header("Authorization", authorization)
}
returnResponse, err := df.Request()
if err != nil {
return returnResponse, err
}
// 微信返回204或者其他状态,直接抛给用户处理
if returnResponse.StatusCode == http.StatusNoContent {
return returnResponse, err
}
// decode response body to outBody
err = client.HttpHelper.ParseResponseBodyContent(returnResponse, outBody)
return returnResponse, err
}
func (client *BaseClient) RequestV2(ctx context.Context, endpoint string, params *object.HashMap, method string, option *object.HashMap,
returnRaw bool, outHeader interface{}, outBody interface{},
) (response *http.Response, err error) {
config := (*client.App).GetConfig()
base := &object.HashMap{
// 微信的接口如果传入接口以外的参数,签名会失败所以这里需要区分对待参数
"mch_id": config.GetString("mch_id", ""),
"nonce_str": object.RandStringBytesMask(32),
"sub_mch_id": config.GetString("sub_mch_id", ""),
"sub_appid": config.GetString("sub_appid", ""),
}
params = object.MergeHashMap(params, base)
if (*params)["mchid"] == nil {
(*params)["mch_id"] = config.GetString("mch_id", "")
} else {
(*params)["mch_id"] = nil
}
params = object.FilterEmptyHashMap(params)
//options, err := client.AuthSignRequestV2(endpoint, method, params, option)
options, err := client.AuthSignRequestV2(endpoint, method, params, option)
if err != nil {
return nil, err
}
// http client request
df := client.HttpHelper.Df().
WithContext(ctx).
Uri(endpoint).Method(method)
// 检查是否需要有请求参数配置
if options != nil {
// set query key values
if (*options)["query"] != nil {
queries := (*options)["query"].(*object.StringMap)
if queries != nil {
for k, v := range *queries {
df.Query(k, v)
}
}
}
config := (*client.App).GetConfig()
// 微信如果需要传debug模式
debug := config.GetBool("debug", false)
if debug {
df.Query("debug", "1")
}
// set body json
if (*options)["body"] != nil {
r := bytes.NewBufferString((*options)["body"].(string))
df.Body(r)
}
}
returnResponse, err := df.Request()
if err != nil {
return returnResponse, err
}
return returnResponse, err
}
func (client *BaseClient) Request(ctx context.Context, endpoint string, params *object.StringMap, method string, options *object.HashMap,
returnRaw bool, outHeader interface{}, outBody interface{},
) (response *http.Response, err error) {
config := (*client.App).GetConfig()
// 签名访问的URL,请确保url后面不要跟参数,因为签名的参数,不包含?参数
// 比如需要在请求的时候,把debug=false,这样url后面就不会多出 "?debug=true"
options, err = client.AuthSignRequest(ctx, config, endpoint, method, params, options)
if err != nil {
return nil, err
}
// to be setup middleware here
//client.PushMiddleware(client.logMiddleware(), "access_token")
// http client request
df := client.HttpHelper.Df().
WithContext(ctx).
Uri(endpoint).Method(method)
// 检查是否需要有请求参数配置
if options != nil {
// set query key values
if (*options)["query"] != nil {
queries := (*options)["query"].(*object.StringMap)
if queries != nil {
for k, v := range *queries {
df.Query(k, v)
}
}
}
config := (*client.App).GetConfig()
// 微信如果需要传debug模式
debug := config.GetBool("debug", false)
if debug {
df.Query("debug", "1")
}
// set body json
if (*options)["body"] != nil {
// signBody是合成了,业务参数和签名参数的参数集合
r := bytes.NewBufferString((*options)["body"].(string))
df.Body(r)
}
// set header
df.
Header("content-type", "application/json").
Header("Authorization", (*(*options)["headers"].(*object.HashMap))["Authorization"].(string))
// 微信支付时候,需要配置平台证书的序列号
// https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_1.shtml
wechatPaySerial := config.GetString("wechat_pay_serial", "")
if wechatPaySerial != "" {
df.Header("Wechatpay-Serial", wechatPaySerial)
}
}
returnResponse, err := df.Request()
if err != nil {
return returnResponse, err
}
// decode response body to outBody
err = client.HttpHelper.ParseResponseBodyContent(returnResponse, outBody)
return returnResponse, err
}
func (client *BaseClient) RequestRawXML(ctx context.Context, url string, params *object.StringMap, method string, options *object.HashMap, outHeader interface{}, outBody interface{}) (interface{}, error) {
config := (*client.App).GetConfig()
base := &object.StringMap{
// 微信的接口如果传入接口以外的参数,签名会失败所以这里需要区分对待参数
"mch_id": config.GetString("mch_id", ""),
"nonce_str": object.RandStringBytesMask(32),
}
params = object.MergeStringMap(params, base)
params = object.FilterEmptyStringMap(params)
// 签名访问的URL,请确保url后面不要跟参数,因为签名的参数,不包含?参数
// 比如需要在请求的时候,把debug=false,这样url后面就不会多出 "?debug=true"
options, err := client.AuthSignRequestSimple(config, url, method, params, options)
if err != nil {
return nil, err
}
// to be setup middleware here
//client.PushMiddleware(client.logMiddleware(), "access_token")
httpConfig := client.HttpHelper.GetClient().GetConfig()
httpConfig.Cert.CertFile = config.GetString("cert_path", "")
httpConfig.Cert.KeyFile = config.GetString("key_path", "")
client.HttpHelper.GetClient().SetConfig(&httpConfig)
// http client request
df := client.HttpHelper.Df().
WithContext(ctx).
Url(url).Method(method)
// 检查是否需要有请求参数配置
if options != nil {
// set query key values
if (*options)["query"] != nil {
queries := (*options)["query"].(*object.StringMap)
if queries != nil {
for k, v := range *queries {
df.Query(k, v)
}
}
}
config := (*client.App).GetConfig()
// 微信如果需要传debug模式
debug := config.GetBool("debug", false)
if debug {
df.Query("debug", "1")
}
// set body json
if (*options)["body"] != nil {
r := bytes.NewBufferString((*options)["body"].(string))
df.Body(r)
}
// set header
df.
Header("content-type", "application/xml").
//Header("Authorization", (*(*options)["headers"].(*object.HashMap))["Authorization"].(string)).
Header("Wechatpay-Serial", (*(*options)["headers"].(*object.HashMap))["Wechatpay-Serial"].(string))
}
returnResponse, err := df.Request()
if err != nil {
return returnResponse, err
}
// decode response body to outBody
err = client.HttpHelper.ParseResponseBodyContent(returnResponse, outBody)
return returnResponse, err
}
func (client *BaseClient) HttpUploadJson(ctx context.Context, url string, files *object.HashMap, form *kernel.UploadForm, options *object.HashMap, queries *object.StringMap, outHeader interface{}, outBody interface{}) (interface{}, error) {
config := (*client.App).GetConfig()
method := http.MethodPost
// 签名访问的URL,请确保url后面不要跟参数,因为签名的参数,不包含?参数
// 比如需要在请求的时候,把debug=false,这样url后面就不会多出 "?debug=true"
signBody, err := object.JsonEncode(options)
authorization, err := client.Signer.GenerateRequestSign(&support.RequestSignChain{
Method: method,
CanonicalURL: url,
SignBody: signBody,
})
if err != nil {
return nil, err
}
options = object.MergeHashMap(&object.HashMap{
"headers": &object.HashMap{
"Authorization": authorization,
},
"body": signBody,
}, options)
// http client request
df := client.HttpHelper.Df().WithContext(ctx).Uri(url).Method(method)
var mems map[string]io.Reader
// 遍历表单的数据
if form != nil {
mems = make(map[string]io.Reader)
for _, content := range form.Contents {
value, err := os2.ConvertFileHandleToReader(content.Value)
if err != nil {
return nil, err
}
mems[content.Name] = value
}
}
// set query key values
if queries != nil {
if queries != nil {
for k, v := range *queries {
df.Query(k, v)
}
}
}
// 微信如果需要传debug模式
debug := config.GetBool("debug", false)
if debug {
df.Query("debug", "1")
}
// set body json
if options != nil {
// set header
df.
Header("content-type", "application/json").
Header("Authorization", (*(*options)["headers"].(*object.HashMap))["Authorization"].(string))
//body := &power.JsonEncoder{
// Data: (*options)["body"],
//}
//df.Any(body)
//df.Json((*options)["body"])
}
//df.Multipart(func(multipart contract.MultipartDfInterface) {
// multipart.FieldValue((*options)["body"].(string))
//})
df.Multipart(func(multipart contract.MultipartDfInterface) {
multipart.FieldValue("meta", (*options)["body"].(string))
// 遍历文件目录
if files != nil {
for name, path := range *files {
multipart.FileByPath(name, path.(string))
}
}
for k, v := range mems {
multipart.FileMem(form.FileName, k, v)
}
})
response, err := df.Request()
if err != nil {
return response, err
}
// decode response body to outBody
if outBody != nil {
err = client.HttpHelper.ParseResponseBodyContent(response, outBody)
if err != nil {
return nil, err
}
}
return response, err
}
func (client *BaseClient) StreamDownload(ctx context.Context, requestDownload *power.RequestDownload, filePath string) (int64, error) {
fileHandler, err := os.Create(filePath)
if err != nil {
return 0, err
}
//defer fileHandler.Close()
config := (*client.App).GetConfig()
method := http.MethodGet
options, err := client.AuthSignRequest(ctx, config, requestDownload.DownloadURL, method, nil, nil)
if err != nil {
return 0, err
}
rs, err := client.HttpHelper.Df().Url(requestDownload.DownloadURL).Method(method).Json(options).Request()
if err != nil {
return 0, err
}
result, err := fileHandler.ReadFrom(rs.Body)
if err != nil {
return result, err
}
fmt2.Dump("http stream download file size:", result)
// 校验已下载文件
downloadedHandler, err := os.Open(filePath)
if err != nil {
return 0, err
}
//defer downloadedHandler.Close()
fileMd5 := sha1.New()
totalSize, err := io.Copy(fileMd5, downloadedHandler)
if err != nil {
return 0, err
}
//fmt2.Dump(totalSize)
if requestDownload.HashValue != "" {
fmt2.Dump(fileMd5.Sum(nil), requestDownload.HashValue)
if fmt.Sprintf("%x", fileMd5.Sum(nil)) != requestDownload.HashValue {
return 0, errors.New("文件损坏")
} else {
log.Println("文件SHA-256校验成功")
}
}
return totalSize, err
}
func (client *BaseClient) RequestArray(ctx context.Context, url string, method string, options *object.HashMap, outHeader interface{}, outBody interface{}) (*object.HashMap, error) {
returnResponse, err := client.Request(ctx, url, nil, method, options, true, outHeader, outBody)
if err != nil {
return nil, err
}
result, err := client.CastResponseToType(returnResponse, response2.TYPE_RAW)
return result.(*object.HashMap), err
}
func (client *BaseClient) SafeRequestV3(ctx context.Context, url string, params *object.StringMap, method string, option *object.HashMap, outHeader interface{}, outBody interface{}) (interface{}, error) {
config := (*client.App).GetConfig()
httpConfig := client.HttpHelper.GetClient().GetConfig()
httpConfig.Cert.CertFile = config.GetString("cert_path", "")
httpConfig.Cert.KeyFile = config.GetString("key_path", "")
client.HttpHelper.GetClient().SetConfig(&httpConfig)
// get xml string result from return raw as true
rs, err := client.Request(
ctx,
url,
params,
method,
option,
true,
outHeader,
outBody,
)
if err != nil {
return nil, err
}
// get out result
client.HttpHelper.ParseResponseBodyContent(rs, outBody)
return outBody, err
}
func (client *BaseClient) SafeRequest(ctx context.Context, url string, params *object.HashMap, method string, option *object.HashMap, outHeader interface{}, outBody interface{}) (interface{}, error) {
config := (*client.App).GetConfig()
httpConfig := client.HttpHelper.GetClient().GetConfig()
httpConfig.Cert.CertFile = config.GetString("cert_path", "")
httpConfig.Cert.KeyFile = config.GetString("key_path", "")
client.HttpHelper.GetClient().SetConfig(&httpConfig)
strOutBody := ""
// get xml string result from return raw as true
rs, err := client.RequestV2(
ctx,
url,
params,
method,
option,
true,
outHeader,
&strOutBody,
)
if err != nil {
return nil, err
}
// get out result
client.HttpHelper.ParseResponseBodyContent(rs, outBody)
return outBody, err
}
func (client *BaseClient) Wrap(endpoint string) string {
if (*client.App).InSandbox() {
return "sandboxnew/" + endpoint
} else {
return endpoint
}
}
func (client *BaseClient) AuthSignRequest(context context.Context, config *kernel.Config, endpoint string, method string, params *object.StringMap, options *object.HashMap) (*object.HashMap, error) {
var err error
base := &object.HashMap{}
// init query parameters into body
if params != nil {
endpoint += "?" + object.GetJoinedWithKSort(params)
(*options)["query"] = params
} else {
(*options)["query"] = nil
}
options = object.MergeHashMap(base, client.prepends(), options)
options = object.FilterEmptyHashMap(options)
// check need sign body or not
signBody := ""
if "get" != object.Lower(method) {
signBody, err = object.JsonEncode(options)
if err != nil {
return nil, err
}
}
authorization, err := client.Signer.GenerateRequestSign(&support.RequestSignChain{
Method: method,
CanonicalURL: endpoint,
SignBody: signBody,
})
if err != nil {
return nil, err
}
options = object.MergeHashMap(&object.HashMap{
"headers": &object.HashMap{
"Authorization": authorization,
},
"body": signBody,
}, options)
return options, err
}
func (client *BaseClient) AuthSignRequestSimple(config *kernel.Config, endpoint string, method string, params *object.StringMap, options *object.HashMap) (*object.HashMap, error) {
var err error
secretKey, err := (*client.App).GetKey(endpoint)
if err != nil {
return nil, err
}
// convert StringMap to Power StringMap
powerStrMapParams, err := power.StringMapToPower(params)
if err != nil {
return nil, err
}
(*powerStrMapParams)["sign_type"] = "MD5"
// generate md5 signature with power StringMap
(*powerStrMapParams)["sign"] = support.GenerateSignMD5(powerStrMapParams, secretKey)
// convert signature to xml content
var signBody = ""
if "get" != object.Lower(method) {
// check need sign body or not
objPara, err := power.PowerStringMapToObjectStringMap(powerStrMapParams)
if err != nil {
return nil, err
}
signBody = object.StringMap2Xml(objPara)
}
// set body content
options = object.MergeHashMap(&object.HashMap{
"headers": &object.HashMap{
//"Authorization": authorization,
"Wechatpay-Serial": config.GetString("serial_no", ""),
},
"body": signBody,
}, options)
return options, err
}
func (client *BaseClient) AuthSignRequestV2(endpoint string, method string, params *object.HashMap, options *object.HashMap) (*object.HashMap, error) {
var err error
secretKey, err := (*client.App).GetKey(endpoint)
if err != nil {
return nil, err
}
strMapParams, err := object.HashMapToStringMap(params)
if err != nil {
return nil, err
}
// convert StringMap to Power StringMap
powerStrMapParams, err := power.StringMapToPower(strMapParams)
if err != nil {
return nil, err
}
// generate md5 signature with power StringMap
(*powerStrMapParams)["sign"] = support.GenerateSignMD5(powerStrMapParams, secretKey)
// convert signature to xml content
var signBody = ""
if "get" != object.Lower(method) {
// check need sign body or not
objPara, err := power.PowerStringMapToObjectStringMap(powerStrMapParams)
if err != nil {
return nil, err
}
signBody = object.StringMap2Xml(objPara)
}
// set body content
options = object.MergeHashMap(&object.HashMap{
"body": signBody,
}, options)
return options, err
}
// ----------------------------------------------------------------------
func (client *BaseClient) RegisterHttpMiddlewares() {
// access token
//accessTokenMiddleware := client.GetMiddlewareOfAccessToken
// check invalid access token
//checkAccessTokenMiddleware := client.GetMiddlewareOfRefreshAccessToken
// log
logMiddleware := client.GetMiddlewareOfLog
config := (*client.App).GetConfig()
logger := (*client.App).GetComponent("Logger").(contract2.LoggerInterface)
client.HttpHelper.WithMiddleware(
//accessTokenMiddleware,
logMiddleware(logger),
//checkAccessTokenMiddleware(3),
helper.HttpDebugMiddleware(config.GetBool("http_debug", false)),
)
}
func (client *BaseClient) OverrideGetMiddlewares() {
client.OverrideGetMiddlewareOfLog()
//client.OverrideGetMiddlewareOfAccessToken()
//client.OverrideGetMiddlewareOfRefreshAccessToken()
}
func (client *BaseClient) OverrideGetMiddlewareOfAccessToken() {
client.GetMiddlewareOfAccessToken = func(handle contract.RequestHandle) contract.RequestHandle {
return func(request *http.Request) (response *http.Response, err error) {
// 前置中间件
//fmt.Println("获取access token, 在请求前执行")
accessToken := (*client.App).GetAccessToken()
if accessToken != nil {
config := (*client.App).GetContainer().Config
_, err = accessToken.ApplyToRequest(request, config)
}
if err != nil {
return nil, err
}
response, err = handle(request)
// handle 执行之后就可以操作 response 和 err
// 后置中间件
//fmt.Println("获取access token, 在请求后执行")
return
}
}
}