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

支持在请求前修改各api的地址 #736

Merged
merged 4 commits into from
Nov 2, 2023
Merged
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
28 changes: 28 additions & 0 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@ import (
"golang.org/x/crypto/pkcs12"
)

// URIModifier URI修改器
type URIModifier func(uri string) string

var uriModifier URIModifier

// SetURIModifier 设置URI修改器
func SetURIModifier(fn URIModifier) {
uriModifier = fn
}

// HTTPGet get 请求
func HTTPGet(uri string) ([]byte, error) {
return HTTPGetContext(context.Background(), uri)
}

// HTTPGetContext get 请求
func HTTPGetContext(ctx context.Context, uri string) ([]byte, error) {
if uriModifier != nil {
uri = uriModifier(uri)
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
Expand All @@ -47,6 +60,9 @@ func HTTPPost(uri string, data string) ([]byte, error) {

// HTTPPostContext post 请求
func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[string]string) ([]byte, error) {
if uriModifier != nil {
uri = uriModifier(uri)
}
body := bytes.NewBuffer(data)
request, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, body)
if err != nil {
Expand All @@ -71,6 +87,9 @@ func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[st

// PostJSONContext post json 数据请求
func PostJSONContext(ctx context.Context, uri string, obj interface{}) ([]byte, error) {
if uriModifier != nil {
uri = uriModifier(uri)
}
jsonBuf := new(bytes.Buffer)
enc := json.NewEncoder(jsonBuf)
enc.SetEscapeHTML(false)
Expand Down Expand Up @@ -146,6 +165,9 @@ type MultipartFormField struct {

// PostMultipartForm 上传文件或其他多个字段
func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte, err error) {
if uriModifier != nil {
uri = uriModifier(uri)
}
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)

Expand Down Expand Up @@ -198,6 +220,9 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte

// PostXML perform a HTTP/POST request with XML body
func PostXML(uri string, obj interface{}) ([]byte, error) {
if uriModifier != nil {
uri = uriModifier(uri)
}
xmlData, err := xml.Marshal(obj)
if err != nil {
return nil, err
Expand Down Expand Up @@ -259,6 +284,9 @@ func pkcs12ToPem(p12 []byte, password string) tls.Certificate {

// PostXMLWithTLS perform a HTTP/POST request with XML body and TLS
func PostXMLWithTLS(uri string, obj interface{}, ca, key string) ([]byte, error) {
if uriModifier != nil {
uri = uriModifier(uri)
}
xmlData, err := xml.Marshal(obj)
if err != nil {
return nil, err
Expand Down