From 793e5d615607806cc3f7c237d802e1a49d129cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 13 Dec 2025 22:14:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8D=87=E7=BA=A7HTTP=E8=AF=B7?= =?UTF-8?q?=E6=B1=82,=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89messa?= =?UTF-8?q?ge=20location=20=E5=92=8C=20=E8=A7=A3=E6=9E=90=E6=95=B4?= =?UTF-8?q?=E4=B8=AA=E5=93=8D=E5=BA=94body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 42 +++++++++++++++++++++++++++++++++--------- define/request.go | 34 ++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/client.go b/client.go index 6de1ff5..5e15c34 100644 --- a/client.go +++ b/client.go @@ -9,15 +9,17 @@ package httpclient import ( "fmt" - "git.zhangdeman.cn/gateway/httpclient/cache" - "git.zhangdeman.cn/gateway/httpclient/define" - "git.zhangdeman.cn/gateway/httpclient/validate" - "git.zhangdeman.cn/zhangdeman/serialize" - "github.com/go-resty/resty/v2" - "github.com/tidwall/gjson" "net/http" "strings" "time" + + "git.zhangdeman.cn/gateway/httpclient/cache" + "git.zhangdeman.cn/gateway/httpclient/define" + "git.zhangdeman.cn/gateway/httpclient/validate" + "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/go-resty/resty/v2" + "github.com/tidwall/gjson" ) // NewHttpClient 获取http client @@ -329,9 +331,31 @@ func (hc *HttpClient) fillResponseCookie(response *define.Response) { // Date : 21:38 2024/6/5 func (hc *HttpClient) fillResponseBody(response *define.Response) { response.Data = string(response.RestyResponse.Body()) - response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String() - response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String() - businessData := gjson.Get(response.Data, hc.reqConfig.DataField) + if hc.reqConfig.CodeFieldLocation == consts.ResponseDataLocationBody.String() { + // 状态码位置在body + response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String() + } else { + // 非 Body 认为是 header + response.Code = fmt.Sprintf("%v", response.HttpCode) + } + if hc.reqConfig.MessageFieldLocation == "" { + // 未单独指定 message 字段位置, 则和code 同位置 + hc.reqConfig.MessageFieldLocation = hc.reqConfig.CodeFieldLocation + } + if hc.reqConfig.CodeFieldLocation == consts.ResponseDataLocationBody.String() { + // 状态码位置在body + response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String() + } else { + // 非 Body 认为是 header + response.Message = response.RestyResponse.Status() + } + var businessData gjson.Result + if hc.reqConfig.DataField == "" || hc.reqConfig.DataField == consts.ResponseDataLocationBodyRoot.String() { + // 整个 Body 均是数据 + businessData = gjson.Parse(response.Data) + } else { + businessData = gjson.Get(response.Data, hc.reqConfig.DataField) + } if businessData.Value() == nil { // data为空指针, 归一化成空对象 response.Body = map[string]any{} diff --git a/define/request.go b/define/request.go index e300e2e..6ce2b4a 100644 --- a/define/request.go +++ b/define/request.go @@ -13,22 +13,24 @@ 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 - Query map[string]string `json:"query"` // 请求query - FullUrl string `json:"full_url"` // 完整的请求URL - ContentType string `json:"content_type"` // 请求类型 - Method string `json:"method"` // 请求方法 - DataField string `json:"data_field"` // 数据字段 - CodeField string `json:"code_field"` // 业务状态码字段 - MessageField string `json:"message_field"` // code描述字段 - DataReceiver interface{} `json:"-"` // 响应data部分数据解析 - SuccessCodeList []string `json:"success_code_list"` // 哪些业务状态码视为成功 - ConnectTimeout int64 `json:"connect_timeout"` // 连接超时时间: ms - ReadTimeout int64 `json:"read_timeout"` // 读取超时时间 - RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则 + 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 + Query map[string]string `json:"query"` // 请求query + FullUrl string `json:"full_url"` // 完整的请求URL + ContentType string `json:"content_type"` // 请求类型 + Method string `json:"method"` // 请求方法 + DataField string `json:"data_field"` // 数据字段 BODY_ROOT 代表整个BODY 都是带解析数据 + CodeField string `json:"code_field"` // 业务状态码字段 + CodeFieldLocation string `json:"code_field_location"` // 业务状态码字段位置 + MessageField string `json:"message_field"` // code描述字段 + MessageFieldLocation string `json:"message_field_location"` // code 描述字段位置 + DataReceiver interface{} `json:"-"` // 响应data部分数据解析 + SuccessCodeList []string `json:"success_code_list"` // 哪些业务状态码视为成功 + ConnectTimeout int64 `json:"connect_timeout"` // 连接超时时间: ms + ReadTimeout int64 `json:"read_timeout"` // 读取超时时间 + RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则 } // RequestRetryRule 重试规则