Skip to content

Commit 1d57f15

Browse files
authored
Merge pull request #616 from ArtisanCloud/develop
Develop
2 parents e465722 + c04852c commit 1d57f15

File tree

12 files changed

+88
-44
lines changed

12 files changed

+88
-44
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go 1.23
77
//replace github.com/ArtisanCloud/PowerSocialite/v3 => ../PowerSocialite
88

99
require (
10-
github.com/ArtisanCloud/PowerLibs/v3 v3.3.1
10+
github.com/ArtisanCloud/PowerLibs/v3 v3.3.2
1111
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.7
1212
github.com/go-playground/assert/v2 v2.0.1
1313
github.com/pkg/errors v0.9.1
@@ -27,5 +27,6 @@ require (
2727
go.uber.org/multierr v1.11.0 // indirect
2828
go.uber.org/zap v1.27.0 // indirect
2929
golang.org/x/crypto v0.28.0 // indirect
30+
gopkg.in/yaml.v2 v2.4.0 // indirect
3031
gopkg.in/yaml.v3 v3.0.1 // indirect
3132
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/ArtisanCloud/PowerLibs/v3 v3.3.1 h1:SsxBygxATQpFS92pKuVtGrgdawwsscj9Y0M0jks9rTo=
2-
github.com/ArtisanCloud/PowerLibs/v3 v3.3.1/go.mod h1:xFGsskCnzAu+6rFEJbGVAlwhrwZPXAny6m7j71S/B5k=
1+
github.com/ArtisanCloud/PowerLibs/v3 v3.3.2 h1:IInr1YWwkhwOykxDqux1Goym0uFhrYwBjmgLnEwCLqs=
2+
github.com/ArtisanCloud/PowerLibs/v3 v3.3.2/go.mod h1:xFGsskCnzAu+6rFEJbGVAlwhrwZPXAny6m7j71S/B5k=
33
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.7 h1:P+erNlErr+X2v7Et+yTWaTfIRhw+HfpAPdvNIEwk9Gw=
44
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.7/go.mod h1:VZQNCvcK/rldF3QaExiSl1gJEAkyc5/I8RLOd3WFZq4=
55
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
@@ -59,6 +59,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
5959
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6060
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
6161
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
62+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
63+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
6264
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6365
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
6466
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

src/kernel/accessToken.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ func NewAccessToken(app ApplicationInterface) (*AccessToken, error) {
4848
baseURI := config.GetString("http.base_uri", "/")
4949
proxyURI := config.GetString("http.proxy_uri", "")
5050

51+
objTransport := config.Get("http.transport", nil)
52+
var transport http.RoundTripper = nil
53+
if objTransport != nil {
54+
if t, ok := objTransport.(http.RoundTripper); ok {
55+
transport = t
56+
}
57+
}
58+
5159
var cacheClient cache.CacheInterface = nil
5260
c := config.Get("cache", nil)
5361
if c != nil {
@@ -57,7 +65,8 @@ func NewAccessToken(app ApplicationInterface) (*AccessToken, error) {
5765
h, err := helper.NewRequestHelper(&helper.Config{
5866
BaseUrl: baseURI,
5967
ClientConfig: &contract3.ClientConfig{
60-
ProxyURI: proxyURI,
68+
ProxyURI: proxyURI,
69+
Transport: transport,
6170
},
6271
})
6372
if err != nil {
@@ -247,7 +256,7 @@ func (accessToken *AccessToken) sendRequest(ctx context.Context, credential *obj
247256
df := accessToken.HttpHelper.Df().WithContext(ctx).Uri(strEndpoint).
248257
Method(accessToken.RequestMethod)
249258

250-
// 检查是否需要有请求参数配置
259+
// 检查是否需要有请求参数配置
251260
// set query key values
252261
if (*options)["query"] != nil {
253262
queries := (*options)["query"].(*object.StringMap)
@@ -349,24 +358,27 @@ func (accessToken *AccessToken) OverrideGetMiddlewareOfLog() {
349358
accessToken.GetMiddlewareOfLog = func(logger contract2.LoggerInterface) contract3.RequestMiddleware {
350359
return func(handle contract3.RequestHandle) contract3.RequestHandle {
351360
return func(request *http.Request) (response *http.Response, err error) {
352-
logger := logger.WithContext(request.Context())
353-
354-
// 此处请求前后日志根据 log 配置中的 level 判断是否打印
355-
request2.LogRequest(logger, request)
356-
361+
defer func() {
362+
config := (accessToken.App).GetConfig()
363+
debug := config.GetBool("http_debug", false)
364+
if debug {
365+
logger := logger.WithContext(request.Context())
366+
// 此处请求前后日志根据 log 配置中的 level 判断是否打印
367+
request2.LogRequest(logger, request)
368+
if response != nil {
369+
response2.LogResponse(logger, response)
370+
}
371+
}
372+
}()
357373
response, err = handle(request)
358374
if err != nil {
359375
return response, err
360376
}
361-
362-
response2.LogResponse(logger, response)
363-
364377
return
365378
}
366379
}
367380
}
368381
}
369-
370382
func (accessToken *AccessToken) OverrideGetEndpoint() {
371383
accessToken.GetEndpoint = func() (string, error) {
372384
if accessToken.EndpointToGetToken == "" {

src/kernel/baseClient.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,23 @@ func NewBaseClient(app *ApplicationInterface, token *AccessToken) (*BaseClient,
5555
baseURI := config.GetString("http.base_uri", "/")
5656
proxyURI := config.GetString("http.proxy_uri", "")
5757
timeout := config.GetFloat64("http.timeout", 5)
58-
58+
objTransport := config.Get("http.transport", nil)
59+
var transport http.RoundTripper = nil
60+
if objTransport != nil {
61+
if t, ok := objTransport.(http.RoundTripper); ok {
62+
transport = t
63+
}
64+
}
5965
if token == nil {
6066
token = (*app).GetAccessToken()
6167
}
6268

6369
h, err := helper.NewRequestHelper(&helper.Config{
6470
BaseUrl: baseURI,
6571
ClientConfig: &contract.ClientConfig{
66-
Timeout: time.Duration(timeout * float64(time.Second)),
67-
ProxyURI: proxyURI,
72+
Timeout: time.Duration(timeout * float64(time.Second)),
73+
ProxyURI: proxyURI,
74+
Transport: transport,
6875
},
6976
})
7077
if err != nil {
@@ -441,17 +448,22 @@ func (client *BaseClient) OverrideGetMiddlewareOfLog() {
441448
client.GetMiddlewareOfLog = func(logger contract2.LoggerInterface) contract.RequestMiddleware {
442449
return func(handle contract.RequestHandle) contract.RequestHandle {
443450
return func(request *http.Request) (response *http.Response, err error) {
444-
logger = logger.WithContext(request.Context())
445-
446-
// 此处请求前后日志根据 log 配置中的 level 判断是否打印
447-
request2.LogRequest(logger, request)
448-
451+
config := (*client.App).GetConfig()
452+
defer func() {
453+
debug := config.GetBool("http_debug", false)
454+
if debug {
455+
logger := logger.WithContext(request.Context())
456+
// 此处请求前后日志根据 log 配置中的 level 判断是否打印
457+
request2.LogRequest(logger, request)
458+
if response != nil {
459+
response2.LogResponse(logger, response)
460+
}
461+
}
462+
}()
449463
response, err = handle(request)
450464
if err != nil {
451465
return response, err
452466
}
453-
response2.LogResponse(logger, response)
454-
455467
return
456468
}
457469
}

src/kernel/request/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ func LogRequest(logger contract2.LoggerInterface, request *http.Request) {
3434
output.Write(body)
3535
}
3636

37-
logger.Info(output.String())
37+
logger.Debug(output.String())
3838
}

src/kernel/response/response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ func LogResponse(logger contract2.LoggerInterface, response *http.Response) {
1313
output.Write([]byte("response content:"))
1414
dumpRes, _ := httputil.DumpResponse(response, true)
1515
output.Write(dumpRes)
16-
logger.Info(output.String())
16+
logger.Debug(output.String())
1717
}

src/miniProgram/application.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/virtualPayment"
3838
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/wxaCode"
3939
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/server"
40+
"net/http"
4041
)
4142

4243
type MiniProgram struct {
@@ -130,9 +131,10 @@ type UserConfig struct {
130131
}
131132

132133
type Http struct {
133-
Timeout float64
134-
BaseURI string
135-
ProxyURI string
134+
Timeout float64
135+
BaseURI string
136+
ProxyURI string
137+
Transport http.RoundTripper
136138
}
137139

138140
type Log struct {
@@ -522,6 +524,7 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
522524
"timeout": userConfig.Http.Timeout,
523525
"base_uri": baseURI,
524526
"proxy_uri": userConfig.Http.ProxyURI,
527+
"transport": userConfig.Http.Transport,
525528
},
526529
"log": &object.HashMap{
527530
"driver": userConfig.Log.Driver,

src/officialAccount/application.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/user"
3939
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/user/tag"
4040
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/wifi"
41+
"net/http"
4142
)
4243

4344
type OfficialAccount struct {
@@ -111,9 +112,10 @@ type UserConfig struct {
111112
}
112113

113114
type Http struct {
114-
Timeout float64
115-
BaseURI string
116-
ProxyURI string
115+
Timeout float64
116+
BaseURI string
117+
ProxyURI string
118+
Transport http.RoundTripper
117119
}
118120

119121
type Log struct {
@@ -461,6 +463,7 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
461463
"timeout": timeout,
462464
"base_uri": baseURI,
463465
"proxy_uri": userConfig.Http.ProxyURI,
466+
"transport": userConfig.Http.Transport,
464467
},
465468
"log": &object.HashMap{
466469
"driver": userConfig.Log.Driver,

src/openPlatform/application.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package openPlatform
33
import (
44
"context"
55
"errors"
6+
"net/http"
67

78
"github.com/ArtisanCloud/PowerLibs/v3/cache"
89
"github.com/ArtisanCloud/PowerLibs/v3/logger"
@@ -62,9 +63,10 @@ type UserConfig struct {
6263
}
6364

6465
type Http struct {
65-
Timeout float64
66-
BaseURI string
67-
ProxyURI string
66+
Timeout float64
67+
BaseURI string
68+
ProxyURI string
69+
Transport http.RoundTripper
6870
}
6971

7072
type Log struct {
@@ -218,6 +220,7 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
218220
"timeout": timeout,
219221
"base_uri": baseURI,
220222
"proxy_uri": userConfig.Http.ProxyURI,
223+
"transport": userConfig.Http.Transport,
221224
},
222225
"log": &object.HashMap{
223226
"driver": userConfig.Log.Driver,

src/openWork/application.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/ArtisanCloud/PowerLibs/v3/logger"
66
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
77
"github.com/ArtisanCloud/PowerLibs/v3/object"
8+
"net/http"
89

910
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
1011
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/providers"
@@ -57,9 +58,10 @@ type UserConfig struct {
5758
}
5859

5960
type Http struct {
60-
Timeout float64
61-
BaseURI string
62-
ProxyURI string
61+
Timeout float64
62+
BaseURI string
63+
ProxyURI string
64+
Transport http.RoundTripper
6365
}
6466

6567
type Log struct {
@@ -205,6 +207,7 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
205207
"timeout": timeout,
206208
"base_uri": baseURI,
207209
"proxy_uri": userConfig.Http.ProxyURI,
210+
"transport": userConfig.Http.Transport,
208211
},
209212
"log": &object.HashMap{
210213
"driver": userConfig.Log.Driver,

src/payment/application.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ type OAuth struct {
111111
Scopes []string
112112
}
113113
type Http struct {
114-
Timeout float64
115-
BaseURI string
116-
ProxyURI string
114+
Timeout float64
115+
BaseURI string
116+
ProxyURI string
117+
Transport http.RoundTripper
117118
}
118119

119120
func NewPayment(config *UserConfig) (*Payment, error) {
@@ -424,6 +425,7 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
424425
"timeout": timeout,
425426
"base_uri": baseURI,
426427
"proxy_uri": userConfig.Http.ProxyURI,
428+
"transport": userConfig.Http.Transport,
427429
},
428430
"oauth.callbacks": userConfig.OAuth.Callback,
429431
"oauth.scopes": userConfig.OAuth.Scopes,

src/work/application.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import (
6363
"github.com/ArtisanCloud/PowerWeChat/v3/src/work/user/exportJobs"
6464
"github.com/ArtisanCloud/PowerWeChat/v3/src/work/user/linkedCorp"
6565
"github.com/ArtisanCloud/PowerWeChat/v3/src/work/user/tag"
66+
"net/http"
6667
)
6768

6869
type Work struct {
@@ -168,9 +169,10 @@ type UserConfig struct {
168169
}
169170

170171
type Http struct {
171-
Timeout float64
172-
BaseURI string
173-
ProxyURI string
172+
Timeout float64
173+
BaseURI string
174+
ProxyURI string
175+
Transport http.RoundTripper
174176
}
175177

176178
type Log struct {
@@ -537,6 +539,7 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
537539
"timeout": timeout,
538540
"base_uri": baseURI,
539541
"proxy_uri": userConfig.Http.ProxyURI,
542+
"transport": userConfig.Http.Transport,
540543
},
541544
"log": &object.HashMap{
542545
"driver": userConfig.Log.Driver,

0 commit comments

Comments
 (0)