优化接口请求的响应数据
This commit is contained in:
parent
1019189ca7
commit
4a00f91d87
20
go.mod
20
go.mod
@ -3,7 +3,27 @@ module git.zhangdeman.cn/zhangdeman/network
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230826060251-8308761f72ca
|
||||||
github.com/ddliu/go-httpclient v0.7.1
|
github.com/ddliu/go-httpclient v0.7.1
|
||||||
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
|
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
|
||||||
github.com/mssola/user_agent v0.6.0
|
github.com/mssola/user_agent v0.6.0
|
||||||
|
github.com/tidwall/gjson v1.16.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230811030300-6f850372c88c // indirect
|
||||||
|
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 // indirect
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20230811032817-e6ad534a9a10 // indirect
|
||||||
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230811055014-2e36e7b1ac67 // indirect
|
||||||
|
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||||
|
github.com/Jeffail/gabs 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
|
||||||
|
github.com/mozillazg/go-pinyin v0.20.0 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||||
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
40
go.sum
40
go.sum
@ -1,8 +1,48 @@
|
|||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230811030300-6f850372c88c h1:Dan3iSVU6XTKt8r3/qixfPHPpfLZjkYlPmaJios7wtE=
|
||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230811030300-6f850372c88c/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||||
|
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 h1:+Lg4vXFEiWVKjhUJdXuoP0AgjGT49oqJ3301STnZErk=
|
||||||
|
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10/go.mod h1:+Lc0zYF8sylRi75A7NGmObrLxugwAZa8WVpWh2eh5X0=
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20230811032817-e6ad534a9a10 h1:orhcMAKrcOajsBJCgssnb9O8YcLsPJvWuXF511gs5dc=
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20230811032817-e6ad534a9a10/go.mod h1:CzX5/WwGDTnKmewarnjkK5XcSRbgszTQTdTL3OUc/s4=
|
||||||
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230811055014-2e36e7b1ac67 h1:risi3/+SuH+snq/+/1dvqDrNyiclCi5weHMixg7IgO8=
|
||||||
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230811055014-2e36e7b1ac67/go.mod h1:tPl6isAsRQLZrX8G3/8fCdhN4OcZypdmdHR/PDkd2PY=
|
||||||
|
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230826060251-8308761f72ca h1:UvHzyTPWaw21IY/V5FUfC7Kt39BRlMttMiXsgxQ4gLE=
|
||||||
|
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230826060251-8308761f72ca/go.mod h1:kvjAbtGTo14gKCS0X4rxnb2sPkskHOUy2NXcx34t6Mw=
|
||||||
|
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||||
|
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
|
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
|
||||||
|
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
|
||||||
|
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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/ddliu/go-httpclient v0.7.1 h1:COWYBalfbaFNe6e0eQU38++vCD5kzLh1H1RFs3xcn9g=
|
github.com/ddliu/go-httpclient v0.7.1 h1:COWYBalfbaFNe6e0eQU38++vCD5kzLh1H1RFs3xcn9g=
|
||||||
github.com/ddliu/go-httpclient v0.7.1/go.mod h1:uwipe9x9SYGk4JhBemO7+dD87QbiY224y0DLB9OY0Ik=
|
github.com/ddliu/go-httpclient v0.7.1/go.mod h1:uwipe9x9SYGk4JhBemO7+dD87QbiY224y0DLB9OY0Ik=
|
||||||
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=
|
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=
|
||||||
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
|
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
|
||||||
|
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||||
|
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
|
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||||
|
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
|
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
|
||||||
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
|
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
|
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||||
|
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||||
|
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
|
||||||
|
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||||
|
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
package httpclient
|
package httpclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
"github.com/ddliu/go-httpclient"
|
"github.com/ddliu/go-httpclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,6 +43,17 @@ const (
|
|||||||
DefaultReadTimeout = 1000
|
DefaultReadTimeout = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ResponseCodeFieldLocationBody 响应状态码在body
|
||||||
|
ResponseCodeFieldLocationBody = "body"
|
||||||
|
// ResponseCodeFieldLocationHeader 在header
|
||||||
|
ResponseCodeFieldLocationHeader = "header"
|
||||||
|
// ResponseCodeFieldLocationDefault 响应状态码位置默认值
|
||||||
|
ResponseCodeFieldLocationDefault = ResponseCodeFieldLocationBody
|
||||||
|
// ResponseBodyAsData 会用整体的body作为数据
|
||||||
|
ResponseBodyAsData = "__response_body__"
|
||||||
|
)
|
||||||
|
|
||||||
// ApiRequestConfig api请求的配置
|
// ApiRequestConfig api请求的配置
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -54,6 +66,7 @@ type ApiRequestConfig struct {
|
|||||||
Domain string `json:"domain"` // 请求域名
|
Domain string `json:"domain"` // 请求域名
|
||||||
URI string `json:"uri"` // 请求的路由
|
URI string `json:"uri"` // 请求的路由
|
||||||
ResponseCodeField string `json:"response_code_field"` // 业务状态码字段
|
ResponseCodeField string `json:"response_code_field"` // 业务状态码字段
|
||||||
|
ResponseCodeFieldLocation string `json:"response_code_field_location"` // 响应的业务状态码位置, 默认 : body . 可配置 body / header
|
||||||
ResponseMessageField string `json:"response_message_field"` // 业务状态码描述的字段
|
ResponseMessageField string `json:"response_message_field"` // 业务状态码描述的字段
|
||||||
ResponseDataField string `json:"response_data_field"` // 业务数据的字段
|
ResponseDataField string `json:"response_data_field"` // 业务数据的字段
|
||||||
SuccessCodeList []string `json:"success_code_list"` // 代表请求成功的code列表
|
SuccessCodeList []string `json:"success_code_list"` // 代表请求成功的code列表
|
||||||
@ -89,4 +102,7 @@ type ApiResponse struct {
|
|||||||
Exception *Exception `json:"exception"` // 异常信息
|
Exception *Exception `json:"exception"` // 异常信息
|
||||||
StartRequestTime int64 `json:"start_request_time"` // 开始请求时间, 纳秒
|
StartRequestTime int64 `json:"start_request_time"` // 开始请求时间, 纳秒
|
||||||
FinishRequestTime int64 `json:"finish_request_time"` // 完成请求时间,纳秒
|
FinishRequestTime int64 `json:"finish_request_time"` // 完成请求时间,纳秒
|
||||||
|
Code string `json:"code"` // 业务状态码
|
||||||
|
Message string `json:"message"` // 状态码描述
|
||||||
|
Data wrapper.String `json:"data"` // 响应数据
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,8 @@ const (
|
|||||||
DomainInvalid = "400001"
|
DomainInvalid = "400001"
|
||||||
// SendRequestError 请求发送出现异常
|
// SendRequestError 请求发送出现异常
|
||||||
SendRequestError = "400002"
|
SendRequestError = "400002"
|
||||||
|
// ReadResponseBodyError 读取响应数据体出现异常
|
||||||
|
ReadResponseBodyError = "400003"
|
||||||
// RequestMethodNotSupport 请求方法不支持
|
// RequestMethodNotSupport 请求方法不支持
|
||||||
RequestMethodNotSupport = "405001"
|
RequestMethodNotSupport = "405001"
|
||||||
)
|
)
|
||||||
|
@ -15,10 +15,15 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
|
|
||||||
"github.com/ddliu/go-httpclient"
|
"github.com/ddliu/go-httpclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -220,14 +225,21 @@ func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse {
|
|||||||
var (
|
var (
|
||||||
client *httpclient.HttpClient
|
client *httpclient.HttpClient
|
||||||
err error
|
err error
|
||||||
|
responseByte []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if len(apiConfig.ResponseCodeFieldLocation) == 0 {
|
||||||
|
apiConfig.ResponseCodeFieldLocation = ResponseCodeFieldLocationDefault
|
||||||
|
}
|
||||||
response := &ApiResponse{
|
response := &ApiResponse{
|
||||||
RequestConfig: apiConfig,
|
RequestConfig: apiConfig,
|
||||||
Response: nil,
|
Response: nil,
|
||||||
Exception: nil,
|
Exception: nil,
|
||||||
StartRequestTime: time.Now().UnixNano(),
|
StartRequestTime: time.Now().UnixNano(),
|
||||||
FinishRequestTime: 0,
|
FinishRequestTime: 0,
|
||||||
|
Code: "",
|
||||||
|
Message: "",
|
||||||
|
Data: wrapper.String("{}"),
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -237,6 +249,25 @@ func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse {
|
|||||||
client = getHttpClient(apiConfig, header)
|
client = getHttpClient(apiConfig, header)
|
||||||
if response.Response, err = client.Do(apiConfig.Method, apiConfig.FullURL, nil, bytes.NewReader(apiConfig.Body)); nil != err {
|
if response.Response, err = client.Do(apiConfig.Method, apiConfig.FullURL, nil, bytes.NewReader(apiConfig.Body)); nil != err {
|
||||||
response.Exception = NewException(SendRequestError, map[string]string{"real_reason": err.Error()})
|
response.Exception = NewException(SendRequestError, map[string]string{"real_reason": err.Error()})
|
||||||
}
|
return response
|
||||||
|
}
|
||||||
|
if responseByte, err = io.ReadAll(response.Response.Body); nil != err {
|
||||||
|
response.Exception = NewException(ReadResponseBodyError, map[string]string{"real_reason": err.Error()})
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
// 提取响应数据
|
||||||
|
if response.RequestConfig.ResponseDataField == ResponseBodyAsData {
|
||||||
|
response.Data = wrapper.String(string(responseByte))
|
||||||
|
} else {
|
||||||
|
response.Data = wrapper.String(gjson.GetBytes(responseByte, response.RequestConfig.ResponseDataField).String())
|
||||||
|
}
|
||||||
|
// 提取响应错误码
|
||||||
|
if response.RequestConfig.ResponseCodeFieldLocation == ResponseCodeFieldLocationHeader {
|
||||||
|
response.Code = response.Response.Header.Get(response.RequestConfig.ResponseCodeField)
|
||||||
|
} else {
|
||||||
|
response.Code = gjson.GetBytes(responseByte, response.RequestConfig.ResponseCodeField).String()
|
||||||
|
}
|
||||||
|
// 提取响应文案
|
||||||
|
response.Message = gjson.GetBytes(responseByte, response.RequestConfig.ResponseMessageField).String()
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user