完成client的构建

This commit is contained in:
白茶清欢 2024-05-31 18:00:24 +08:00
parent 2be9c90b79
commit a0adefcc0c
7 changed files with 77 additions and 10 deletions

View File

@ -10,4 +10,8 @@
## 配置说明 ## 配置说明
## 默认配置
- AllowGetMethodPayload : 允许GET请求带Body, 默认值 **`true`** , 如需关闭, 可通过 HttpClient.GetRestyClient().SetAllowGetMethodPayload(false) 关闭
## 使用方式 ## 使用方式

View File

@ -19,14 +19,16 @@ import (
// //
// Date : 15:27 2024/5/31 // Date : 15:27 2024/5/31
func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) { func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
hc := &HttpClient{
Client: NewRestyClient(reqConfig),
reqConfig: reqConfig,
}
// 验证配置正确性以及初始化默认值 // 验证配置正确性以及初始化默认值
if err := validate.RequestConfig(reqConfig); nil != err { if err := validate.RequestConfig(reqConfig); nil != err {
return nil, err return nil, err
} }
restyClient, restyRequest := NewRestyClient(reqConfig)
hc := &HttpClient{
Client: restyClient,
request: restyRequest,
reqConfig: reqConfig,
}
return hc, nil return hc, nil
} }
@ -37,6 +39,7 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
// Date : 15:27 2024/5/31 // Date : 15:27 2024/5/31
type HttpClient struct { type HttpClient struct {
*resty.Client *resty.Client
request *resty.Request
reqConfig *define.Request reqConfig *define.Request
} }

View File

@ -13,6 +13,7 @@ package define
// //
// Date : 17:10 2024/5/24 // Date : 17:10 2024/5/24
type Request struct { type Request struct {
PathParam map[string]string `json:"path_param"` // 替换url中的占位符
Body map[string]any `json:"body"` // 请求Body Body map[string]any `json:"body"` // 请求Body
Header map[string]string `json:"header"` // 请求Header Header map[string]string `json:"header"` // 请求Header
Cookie map[string]string `json:"cookie"` // 请求Cookie Cookie map[string]string `json:"cookie"` // 请求Cookie

2
go.mod
View File

@ -12,7 +12,7 @@ require (
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 // indirect git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 // indirect
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306 // indirect git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect github.com/BurntSushi/toml v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/go-ini/ini v1.67.0 // indirect github.com/go-ini/ini v1.67.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect

2
go.sum
View File

@ -14,6 +14,8 @@ git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306 h1:Iy36o
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240506070032-e228983e7306/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=

View File

@ -8,9 +8,14 @@
package httpclient package httpclient
import ( import (
"encoding/json"
"git.zhangdeman.cn/gateway/httpclient/define" "git.zhangdeman.cn/gateway/httpclient/define"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
"net/http"
"net/textproto" "net/textproto"
"strings"
) )
// NewRestyClient 获取resty client // NewRestyClient 获取resty client
@ -18,13 +23,56 @@ import (
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:00 2024/5/31 // Date : 15:00 2024/5/31
func NewRestyClient(requestConfig *define.Request) *resty.Client { func NewRestyClient(reqConfig *define.Request) (*resty.Client, *resty.Request) {
formatHeader(requestConfig)
client := resty.New() client := resty.New()
if nil == requestConfig { request := client.R()
return client if nil == reqConfig {
return client, request
} }
return client formatHeader(reqConfig)
client.SetAllowGetMethodPayload(true) // 配置 GET 请求允许带 Body
client.SetJSONMarshaler(json.Marshal) // 序列化方法
client.SetJSONEscapeHTML(true) // 处理html实体字符
client.SetJSONUnmarshaler(serialize.JSON.UnmarshalWithNumber) // 反序列化方法
request.SetPathParams(reqConfig.PathParam) // 设置path中的参数
request.SetQueryParams(reqConfig.Query) // 设置query参数
request.SetHeaders(reqConfig.Header) // 设置header
cookieList := make([]*http.Cookie, 0)
for cookieName, cookieValue := range reqConfig.Cookie {
cookieList = append(cookieList, &http.Cookie{
Name: cookieName,
Value: cookieValue,
})
}
request.SetCookies(cookieList) // 设置cookie
setRestyBody(reqConfig, request)
return client, request
}
// setRestyBody 设置请求BODY
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:18 2024/5/31
func setRestyBody(reqConfig *define.Request, request *resty.Request) {
if nil == reqConfig.Body || len(reqConfig.Body) == 0 {
return
}
if strings.Contains(strings.ToLower(reqConfig.ContentType), "application/json") {
request.SetBody(reqConfig.Body)
return
}
if strings.Contains(strings.ToLower(reqConfig.ContentType), "application/x-www-form-urlencodeds") {
bodyStr := serialize.JSON.MarshalForString(reqConfig.Body)
bodyData := map[string]string{}
jsonObj := gjson.Parse(bodyStr)
jsonObj.ForEach(func(key, value gjson.Result) bool {
bodyData[key.String()] = value.String()
return true
})
}
return
} }
// formatHeader 格式化header // formatHeader 格式化header

View File

@ -38,6 +38,15 @@ type requestConfig struct {
// //
// Date : 16:25 2024/5/31 // Date : 16:25 2024/5/31
func (rc *requestConfig) initDefaultConfig(reqConfig *define.Request) { func (rc *requestConfig) initDefaultConfig(reqConfig *define.Request) {
if nil == reqConfig.Header {
reqConfig.Header = map[string]string{}
}
if len(reqConfig.ContentType) > 0 {
reqConfig.Header["Content-Type"] = reqConfig.ContentType
}
if nil == reqConfig.Cookie {
reqConfig.Cookie = map[string]string{}
}
if reqConfig.ConnectTimeout <= 0 { if reqConfig.ConnectTimeout <= 0 {
reqConfig.ConnectTimeout = define.DefaultConnectTimeout reqConfig.ConnectTimeout = define.DefaultConnectTimeout
} }