Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持设置全局HTTPClient #761

Merged
merged 6 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
golangci:
strategy:
matrix:
go-version: [ '1.16','1.17','1.18','1.19','1.20','1.21' ]
go-version: [ '1.16','1.17','1.18','1.19','1.20','1.21.4' ]
name: golangci-lint
runs-on: ubuntu-latest
steps:
Expand Down
24 changes: 13 additions & 11 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type URIModifier func(uri string) string

var uriModifier URIModifier

// DefaultHTTPClient 默认httpClient
var DefaultHTTPClient = http.DefaultClient

// SetURIModifier 设置URI修改器
func SetURIModifier(fn URIModifier) {
uriModifier = fn
Expand All @@ -41,7 +44,7 @@ func HTTPGetContext(ctx context.Context, uri string) ([]byte, error) {
if err != nil {
return nil, err
}
response, err := http.DefaultClient.Do(request)
response, err := DefaultHTTPClient.Do(request)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -73,7 +76,7 @@ func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[st
request.Header.Set(key, value)
}

response, err := http.DefaultClient.Do(request)
response, err := DefaultHTTPClient.Do(request)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -102,7 +105,7 @@ func PostJSONContext(ctx context.Context, uri string, obj interface{}) ([]byte,
return nil, err
}
req.Header.Set("Content-Type", "application/json;charset=utf-8")
response, err := http.DefaultClient.Do(req)
response, err := DefaultHTTPClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -129,7 +132,7 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
return nil, "", err
}

response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf)
response, err := DefaultHTTPClient.Post(uri, "application/json;charset=utf-8", jsonBuf)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -205,7 +208,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()

resp, e := http.Post(uri, contentType, bodyBuf)
resp, e := DefaultHTTPClient.Post(uri, contentType, bodyBuf)
if e != nil {
err = e
return
Expand All @@ -229,7 +232,7 @@ func PostXML(uri string, obj interface{}) ([]byte, error) {
}

body := bytes.NewBuffer(xmlData)
response, err := http.Post(uri, "application/xml;charset=utf-8", body)
response, err := DefaultHTTPClient.Post(uri, "application/xml;charset=utf-8", body)
if err != nil {
return nil, err
}
Expand All @@ -252,11 +255,10 @@ func httpWithTLS(rootCa, key string) (*http.Client, error) {
config := &tls.Config{
Certificates: []tls.Certificate{cert},
}
tr := &http.Transport{
TLSClientConfig: config,
DisableCompression: true,
}
client = &http.Client{Transport: tr}
trans := (DefaultHTTPClient.Transport.(*http.Transport)).Clone()
trans.TLSClientConfig = config
trans.DisableCompression = true
client = &http.Client{Transport: trans}
return client, nil
}

Expand Down
7 changes: 7 additions & 0 deletions wechat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wechat

import (
"net/http"
"os"

log "github.com/sirupsen/logrus"
Expand All @@ -14,6 +15,7 @@ import (
openConfig "github.com/silenceper/wechat/v2/openplatform/config"
"github.com/silenceper/wechat/v2/pay"
payConfig "github.com/silenceper/wechat/v2/pay/config"
"github.com/silenceper/wechat/v2/util"
"github.com/silenceper/wechat/v2/work"
workConfig "github.com/silenceper/wechat/v2/work/config"
)
Expand Down Expand Up @@ -81,3 +83,8 @@ func (wc *Wechat) GetWork(cfg *workConfig.Config) *work.Work {
}
return work.NewWork(cfg)
}

// SetHTTPClient 设置HTTPClient
func (wc *Wechat) SetHTTPClient(client *http.Client) {
util.DefaultHTTPClient = client
}
Loading