From a0adefcc0cb6aec6b408101c106cd4cdb5d4daf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 31 May 2024 18:00:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90client=E7=9A=84=E6=9E=84?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ client.go | 11 +++++--- define/request.go | 1 + go.mod | 2 +- go.sum | 2 ++ resty.go | 58 ++++++++++++++++++++++++++++++++++---- validate/request_config.go | 9 ++++++ 7 files changed, 77 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6d7dcc5..93e147c 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,8 @@ ## 配置说明 +## 默认配置 + +- AllowGetMethodPayload : 允许GET请求带Body, 默认值 **`true`** , 如需关闭, 可通过 HttpClient.GetRestyClient().SetAllowGetMethodPayload(false) 关闭 + ## 使用方式 diff --git a/client.go b/client.go index 079ddcf..8f9def7 100644 --- a/client.go +++ b/client.go @@ -19,14 +19,16 @@ import ( // // Date : 15:27 2024/5/31 func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) { - hc := &HttpClient{ - Client: NewRestyClient(reqConfig), - reqConfig: reqConfig, - } // 验证配置正确性以及初始化默认值 if err := validate.RequestConfig(reqConfig); nil != err { return nil, err } + restyClient, restyRequest := NewRestyClient(reqConfig) + hc := &HttpClient{ + Client: restyClient, + request: restyRequest, + reqConfig: reqConfig, + } return hc, nil } @@ -37,6 +39,7 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) { // Date : 15:27 2024/5/31 type HttpClient struct { *resty.Client + request *resty.Request reqConfig *define.Request } diff --git a/define/request.go b/define/request.go index fc89126..f8d0db4 100644 --- a/define/request.go +++ b/define/request.go @@ -13,6 +13,7 @@ package define // // Date : 17:10 2024/5/24 type Request struct { + PathParam map[string]string `json:"path_param"` // 替换url中的占位符 Body map[string]any `json:"body"` // 请求Body Header map[string]string `json:"header"` // 请求Header Cookie map[string]string `json:"cookie"` // 请求Cookie diff --git a/go.mod b/go.mod index 24b9d0c..f90006f 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( 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/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/go-ini/ini v1.67.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect diff --git a/go.sum b/go.sum index 79012e2..f2e2840 100644 --- a/go.sum +++ b/go.sum @@ -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= 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.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/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= diff --git a/resty.go b/resty.go index dff4ac5..7309836 100644 --- a/resty.go +++ b/resty.go @@ -8,9 +8,14 @@ package httpclient import ( + "encoding/json" "git.zhangdeman.cn/gateway/httpclient/define" + "git.zhangdeman.cn/zhangdeman/serialize" "github.com/go-resty/resty/v2" + "github.com/tidwall/gjson" + "net/http" "net/textproto" + "strings" ) // NewRestyClient 获取resty client @@ -18,13 +23,56 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 15:00 2024/5/31 -func NewRestyClient(requestConfig *define.Request) *resty.Client { - formatHeader(requestConfig) +func NewRestyClient(reqConfig *define.Request) (*resty.Client, *resty.Request) { client := resty.New() - if nil == requestConfig { - return client + request := client.R() + 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 diff --git a/validate/request_config.go b/validate/request_config.go index 14513a2..0b91a0d 100644 --- a/validate/request_config.go +++ b/validate/request_config.go @@ -38,6 +38,15 @@ type requestConfig struct { // // Date : 16:25 2024/5/31 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 { reqConfig.ConnectTimeout = define.DefaultConnectTimeout }