2021-08-01 21:13:17 +08:00
|
|
|
// Package request...
|
|
|
|
//
|
|
|
|
// Description : http 请求
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-08-01 8:47 下午
|
|
|
|
package request
|
|
|
|
|
2021-08-12 22:18:47 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
2021-08-01 21:13:17 +08:00
|
|
|
|
2021-08-12 22:18:47 +08:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
|
|
|
"github.com/ddliu/go-httpclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
// request 发送请求
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:27 下午 2021/8/12
|
2021-09-18 17:24:09 +08:00
|
|
|
func Request(apiMethod *APIMethod, responseConfig *ResponseConfig) (*APIResponse, bool) {
|
2021-08-12 22:18:47 +08:00
|
|
|
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
|
|
|
|
)
|
2021-08-13 19:13:01 +08:00
|
|
|
client = httpclient.NewHttpClient()
|
|
|
|
inputByteData, _ := json.Marshal(apiMethod.Parameter)
|
2021-08-12 22:18:47 +08:00
|
|
|
requestData := bytes.NewReader(inputByteData)
|
2021-08-13 19:13:01 +08:00
|
|
|
if response, err = client.Do(apiMethod.Method, apiMethod.ServiceDomain+apiMethod.URI, apiMethod.Header, requestData); nil != err {
|
|
|
|
apiResponse.Success = false
|
2021-08-12 22:18:47 +08:00
|
|
|
apiResponse.ErrorList = append(apiResponse.ErrorList, err)
|
|
|
|
return apiResponse, false
|
|
|
|
}
|
|
|
|
if responseData, err = ioutil.ReadAll(response.Body); nil != err {
|
2021-08-13 19:13:01 +08:00
|
|
|
apiResponse.Success = false
|
|
|
|
apiResponse.ErrorList = append(apiResponse.ErrorList, err)
|
2021-08-12 22:18:47 +08:00
|
|
|
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()
|
2021-08-13 19:13:01 +08:00
|
|
|
apiResponse.Success = isSuccessCode(apiResponse.Code, responseConfig.SuccessCode)
|
|
|
|
|
|
|
|
if apiResponse.Success {
|
|
|
|
return apiResponse, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 非成功, 判断是否需要重试
|
|
|
|
return apiResponse, isNeedRetry(apiMethod, ResponseErrorTypeBusinessCode)
|
2021-08-12 22:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// isNeedRetry 是否需要重试
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 3:01 下午 2021/8/12
|
|
|
|
func isNeedRetry(apiMethod *APIMethod, errType string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-13 19:13:01 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:18:47 +08:00
|
|
|
// 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
|
2021-08-01 21:13:17 +08:00
|
|
|
}
|