优化接口请求的响应数据
This commit is contained in:
@ -12,6 +12,7 @@
|
||||
package httpclient
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"github.com/ddliu/go-httpclient"
|
||||
)
|
||||
|
||||
@ -42,6 +43,17 @@ const (
|
||||
DefaultReadTimeout = 1000
|
||||
)
|
||||
|
||||
const (
|
||||
// ResponseCodeFieldLocationBody 响应状态码在body
|
||||
ResponseCodeFieldLocationBody = "body"
|
||||
// ResponseCodeFieldLocationHeader 在header
|
||||
ResponseCodeFieldLocationHeader = "header"
|
||||
// ResponseCodeFieldLocationDefault 响应状态码位置默认值
|
||||
ResponseCodeFieldLocationDefault = ResponseCodeFieldLocationBody
|
||||
// ResponseBodyAsData 会用整体的body作为数据
|
||||
ResponseBodyAsData = "__response_body__"
|
||||
)
|
||||
|
||||
// ApiRequestConfig api请求的配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -50,20 +62,21 @@ const (
|
||||
//
|
||||
// Date: 2022/05/01 20:14:18
|
||||
type ApiRequestConfig struct {
|
||||
Method string `json:"method"` // 请求方法
|
||||
Domain string `json:"domain"` // 请求域名
|
||||
URI string `json:"uri"` // 请求的路由
|
||||
ResponseCodeField string `json:"response_code_field"` // 业务状态码字段
|
||||
ResponseMessageField string `json:"response_message_field"` // 业务状态码描述的字段
|
||||
ResponseDataField string `json:"response_data_field"` // 业务数据的字段
|
||||
SuccessCodeList []string `json:"success_code_list"` // 代表请求成功的code列表
|
||||
SuccessHttpCodeList []string `json:"success_http_code_list"` // 代表成功的http code列表
|
||||
Parameter map[string]interface{} `json:"parameter"` // 传入的请求参数
|
||||
CommonHeader map[string]string `json:"common_header"` // 全部请求都要传的header
|
||||
Body []byte `json:"-"` // 请求体
|
||||
FullURL string `json:"full_url"` // 完整请求地址
|
||||
Timeout Timeout `json:"timeout"` // 超时配置
|
||||
ContentType string `json:"content_type"` // 请求方法
|
||||
Method string `json:"method"` // 请求方法
|
||||
Domain string `json:"domain"` // 请求域名
|
||||
URI string `json:"uri"` // 请求的路由
|
||||
ResponseCodeField string `json:"response_code_field"` // 业务状态码字段
|
||||
ResponseCodeFieldLocation string `json:"response_code_field_location"` // 响应的业务状态码位置, 默认 : body . 可配置 body / header
|
||||
ResponseMessageField string `json:"response_message_field"` // 业务状态码描述的字段
|
||||
ResponseDataField string `json:"response_data_field"` // 业务数据的字段
|
||||
SuccessCodeList []string `json:"success_code_list"` // 代表请求成功的code列表
|
||||
SuccessHttpCodeList []string `json:"success_http_code_list"` // 代表成功的http code列表
|
||||
Parameter map[string]interface{} `json:"parameter"` // 传入的请求参数
|
||||
CommonHeader map[string]string `json:"common_header"` // 全部请求都要传的header
|
||||
Body []byte `json:"-"` // 请求体
|
||||
FullURL string `json:"full_url"` // 完整请求地址
|
||||
Timeout Timeout `json:"timeout"` // 超时配置
|
||||
ContentType string `json:"content_type"` // 请求方法
|
||||
}
|
||||
|
||||
// Timeout 超时配置
|
||||
@ -89,4 +102,7 @@ type ApiResponse struct {
|
||||
Exception *Exception `json:"exception"` // 异常信息
|
||||
StartRequestTime int64 `json:"start_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"
|
||||
// SendRequestError 请求发送出现异常
|
||||
SendRequestError = "400002"
|
||||
// ReadResponseBodyError 读取响应数据体出现异常
|
||||
ReadResponseBodyError = "400003"
|
||||
// RequestMethodNotSupport 请求方法不支持
|
||||
RequestMethodNotSupport = "405001"
|
||||
)
|
||||
|
@ -15,10 +15,15 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
|
||||
"github.com/ddliu/go-httpclient"
|
||||
)
|
||||
|
||||
@ -218,16 +223,23 @@ func buildRequestURLAndParam(apiConfig *ApiRequestConfig, header map[string]stri
|
||||
// Date : 14:53 2022/5/2
|
||||
func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse {
|
||||
var (
|
||||
client *httpclient.HttpClient
|
||||
err error
|
||||
client *httpclient.HttpClient
|
||||
err error
|
||||
responseByte []byte
|
||||
)
|
||||
|
||||
if len(apiConfig.ResponseCodeFieldLocation) == 0 {
|
||||
apiConfig.ResponseCodeFieldLocation = ResponseCodeFieldLocationDefault
|
||||
}
|
||||
response := &ApiResponse{
|
||||
RequestConfig: apiConfig,
|
||||
Response: nil,
|
||||
Exception: nil,
|
||||
StartRequestTime: time.Now().UnixNano(),
|
||||
FinishRequestTime: 0,
|
||||
Code: "",
|
||||
Message: "",
|
||||
Data: wrapper.String("{}"),
|
||||
}
|
||||
|
||||
defer func() {
|
||||
@ -237,6 +249,25 @@ func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse {
|
||||
client = getHttpClient(apiConfig, header)
|
||||
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()})
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user