123 lines
3.3 KiB
Go
123 lines
3.3 KiB
Go
// Package request...
|
|
//
|
|
// Description : http 请求
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2021-08-01 8:47 下午
|
|
package request
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"time"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/ddliu/go-httpclient"
|
|
)
|
|
|
|
// request 发送请求
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2:27 下午 2021/8/12
|
|
func Request(apiMethod *APIMethod, responseConfig *ResponseConfig) (*APIResponse, bool) {
|
|
apiResponse := &APIResponse{
|
|
Code: "",
|
|
Message: "",
|
|
Data: "",
|
|
Success: false,
|
|
TraceID: "",
|
|
Cost: 0,
|
|
RealCost: 0,
|
|
TotalCost: 0,
|
|
TotalRealCost: 0,
|
|
ErrorList: make([]error, 0),
|
|
StartRequestTime: time.Now().Unix(),
|
|
FinishRequestTime: 0,
|
|
}
|
|
defer func(apiResponse *APIResponse) {
|
|
apiResponse.FinishRequestTime = time.Now().Unix()
|
|
apiResponse.RealCost = apiResponse.FinishRequestTime - apiResponse.StartRequestTime
|
|
}(apiResponse)
|
|
var (
|
|
client *httpclient.HttpClient
|
|
response *httpclient.Response
|
|
err error
|
|
responseData []byte
|
|
)
|
|
client = httpclient.NewHttpClient()
|
|
inputByteData, _ := json.Marshal(apiMethod.Parameter)
|
|
requestData := bytes.NewReader(inputByteData)
|
|
if response, err = client.Do(apiMethod.Method, apiMethod.ServiceDomain+apiMethod.URI, apiMethod.Header, requestData); nil != err {
|
|
apiResponse.Success = false
|
|
apiResponse.ErrorList = append(apiResponse.ErrorList, err)
|
|
return apiResponse, false
|
|
}
|
|
if responseData, err = ioutil.ReadAll(response.Body); nil != err {
|
|
apiResponse.Success = false
|
|
apiResponse.ErrorList = append(apiResponse.ErrorList, err)
|
|
return apiResponse, false
|
|
}
|
|
orgData := string(responseData)
|
|
apiResponse.Data = gjson.Get(orgData, responseConfig.DataKey).String()
|
|
apiResponse.Code = fmt.Sprintf("%v", gjson.Get(orgData, responseConfig.CodeKey).Value())
|
|
apiResponse.Message = gjson.Get(orgData, responseConfig.MessageKey).String()
|
|
apiResponse.Cost = gjson.Get(orgData, responseConfig.CostKey).Int()
|
|
apiResponse.Success = isSuccessCode(apiResponse.Code, responseConfig.SuccessCode)
|
|
|
|
if apiResponse.Success {
|
|
return apiResponse, false
|
|
}
|
|
|
|
// 非成功, 判断是否需要重试
|
|
return apiResponse, isNeedRetry(apiMethod, ResponseErrorTypeBusinessCode)
|
|
}
|
|
|
|
// isNeedRetry 是否需要重试
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 3:01 下午 2021/8/12
|
|
func isNeedRetry(apiMethod *APIMethod, errType string) bool {
|
|
return false
|
|
}
|
|
|
|
// isSuccessCode 是否为业务处理成功状态码
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 3:43 下午 2021/8/13
|
|
func isSuccessCode(responseCode string, successResponseCodeList []string) bool {
|
|
isSuccess := false
|
|
for _, itemCode := range successResponseCodeList {
|
|
if responseCode == itemCode {
|
|
isSuccess = true
|
|
break
|
|
}
|
|
}
|
|
return isSuccess
|
|
}
|
|
|
|
// buildResponseData 构建响应结果
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2:57 下午 2021/8/12
|
|
func buildResponseData(response *httpclient.Response) *APIResponse {
|
|
return nil
|
|
}
|
|
|
|
// buildRequestHeader 构建请求header
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 9:41 下午 2021/8/11
|
|
func buildRequestHeader(apiMethod *APIMethod) map[string]string {
|
|
header := make(map[string]string)
|
|
return header
|
|
}
|