支持自适应请求类型, 根据不同请求类型设置正确Body格式
This commit is contained in:
55
httpclient/implement/request/write_body.go
Normal file
55
httpclient/implement/request/write_body.go
Normal file
@ -0,0 +1,55 @@
|
||||
// Package request ...
|
||||
//
|
||||
// Description : request ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-08 14:21
|
||||
package request
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/abstract"
|
||||
"resty.dev/v3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
// WriteBodyInstanceTable 请求类型 => 请求类型写入的实现
|
||||
WriteBodyInstanceTable = map[string]abstract.IRequestBodyWrite{
|
||||
"json": &WriteJson{},
|
||||
"xml": nil,
|
||||
"x-www-form-urlencoded": &WriteForm{},
|
||||
}
|
||||
)
|
||||
|
||||
func SetWriteBodyInstance(cType string, instance abstract.IRequestBodyWrite) {
|
||||
WriteBodyInstanceTable[cType] = instance
|
||||
}
|
||||
|
||||
func DeleteWriteBodyInstance(cType string) {
|
||||
delete(WriteBodyInstanceTable, cType)
|
||||
}
|
||||
|
||||
// WriteBody 数据写入body
|
||||
func WriteBody(contentType string, request *resty.Request, bodyData map[string]any) error {
|
||||
if nil == bodyData {
|
||||
// body为nil, 无需写入, 否则即使为空map也要正常写入
|
||||
return nil
|
||||
}
|
||||
if nil == request {
|
||||
return errors.New("request is nil")
|
||||
}
|
||||
contentType = strings.TrimSpace(strings.Split(contentType, ";")[0])
|
||||
if len(contentType) == 0 {
|
||||
return errors.New("contentType is empty or invalid")
|
||||
}
|
||||
contentTypeArr := strings.Split(contentType, "/")
|
||||
realType := contentTypeArr[len(contentTypeArr)-1]
|
||||
writeInstance, ok := WriteBodyInstanceTable[realType]
|
||||
if !ok || writeInstance == nil {
|
||||
return errors.New(realType + " is not support, writeInstance is nil")
|
||||
}
|
||||
writeInstance.Write(request, bodyData)
|
||||
return nil
|
||||
}
|
33
httpclient/implement/request/write_form.go
Normal file
33
httpclient/implement/request/write_form.go
Normal file
@ -0,0 +1,33 @@
|
||||
// Package request ...
|
||||
//
|
||||
// Description : request ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-08 14:39
|
||||
package request
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"resty.dev/v3"
|
||||
)
|
||||
|
||||
// WriteForm application/x-www-form-urlencoded 实现
|
||||
type WriteForm struct {
|
||||
}
|
||||
|
||||
func (w *WriteForm) Write(request *resty.Request, bodyData map[string]any) error {
|
||||
if len(bodyData) == 0 {
|
||||
return nil
|
||||
}
|
||||
bodyStr := serialize.JSON.MarshalForStringIgnoreError(bodyData)
|
||||
formatBodyData := map[string]string{}
|
||||
jsonObj := gjson.Parse(bodyStr)
|
||||
jsonObj.ForEach(func(key, value gjson.Result) bool {
|
||||
formatBodyData[key.String()] = value.String()
|
||||
return true
|
||||
})
|
||||
request.SetFormData(formatBodyData)
|
||||
return nil
|
||||
}
|
21
httpclient/implement/request/write_json.go
Normal file
21
httpclient/implement/request/write_json.go
Normal file
@ -0,0 +1,21 @@
|
||||
// Package request ...
|
||||
//
|
||||
// Description : request ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-08 14:21
|
||||
package request
|
||||
|
||||
import (
|
||||
"resty.dev/v3"
|
||||
)
|
||||
|
||||
// WriteJson application/json写入
|
||||
type WriteJson struct {
|
||||
}
|
||||
|
||||
func (w *WriteJson) Write(request *resty.Request, bodyData map[string]any) error {
|
||||
request.SetBody(bodyData)
|
||||
return nil
|
||||
}
|
46
httpclient/implement/request/write_xml.go
Normal file
46
httpclient/implement/request/write_xml.go
Normal file
@ -0,0 +1,46 @@
|
||||
// Package request ...
|
||||
//
|
||||
// Description : request ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-08 14:21
|
||||
package request
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"resty.dev/v3"
|
||||
"git.zhangdeman.cn/zhangdeman/dynamic-struct/wrapper"
|
||||
)
|
||||
|
||||
// WriteXml application/xml body写入
|
||||
type WriteXml struct{}
|
||||
|
||||
func (w *WriteXml) Write(request *resty.Request, bodyData map[string]any) error {
|
||||
if nil == bodyData {
|
||||
return nil
|
||||
}
|
||||
var (
|
||||
err error
|
||||
bodyBytes []byte
|
||||
xmlBodyByte []byte
|
||||
)
|
||||
|
||||
if bodyBytes, err = serialize.JSON.MarshalForByte(bodyData); nil != err {
|
||||
return err
|
||||
}
|
||||
instance, err := wrapper.NewJson(string(bodyBytes), &wrapper.Option{XmlName: "RequestBody"})
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
if nil == instance {
|
||||
return errors.New("xml generate instance is nil")
|
||||
}
|
||||
if xmlBodyByte, err = instance.Marshal("xml"); nil != err {
|
||||
return err
|
||||
}
|
||||
request.SetBody(bytes.NewReader(xmlBodyByte))
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user