httpclient/client.go
2024-06-05 21:55:27 +08:00

320 lines
9.6 KiB
Go

// Package httpclient ...
//
// Description : httpclient ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-31 15:22
package httpclient
import (
"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"
"strings"
"time"
)
// NewHttpClient 获取http client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:27 2024/5/31
func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*HttpClient, error) {
// 验证配置正确性以及初始化默认值
if err := validate.RequestConfig(reqConfig); nil != err {
return nil, err
}
restyClient, restyRequest := NewRestyClient(reqConfig)
hc := &HttpClient{
Client: restyClient,
request: restyRequest,
reqConfig: reqConfig,
http4xxHandler: make([]define.Http4xxHandler, 0),
http5xxHandler: make([]define.Http5xxHandler, 0),
httpBusinessErrorHandler: make([]define.HttpBusinessErrorHandler, 0),
requestFinishHandler: make([]define.RequestFinishHandler, 0),
cacheInstance: cacheInstance,
}
return hc, nil
}
// HttpClient 请求客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:27 2024/5/31
type HttpClient struct {
*resty.Client
request *resty.Request
reqConfig *define.Request
http4xxHandler []define.Http4xxHandler
http5xxHandler []define.Http5xxHandler
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
requestSendErrorHandler []define.RequestSendErrorHandler
requestFinishHandler []define.RequestFinishHandler
cacheInstance cache.ICache
}
// OnResponse4xx 4xx处理逻辑
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:20 2024/6/1
func (hc *HttpClient) OnResponse4xx(handlerList ...define.Http4xxHandler) {
hc.http4xxHandler = append(hc.http4xxHandler, handlerList...)
}
// OnResponse5xx 5xx处理逻辑
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:21 2024/6/1
func (hc *HttpClient) OnResponse5xx(handlerList ...define.Http5xxHandler) {
hc.http5xxHandler = append(hc.http5xxHandler, handlerList...)
}
// OnResponseBusinessError 业务错误出路逻辑
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:22 2024/6/1
func (hc *HttpClient) OnResponseBusinessError(handlerList ...define.HttpBusinessErrorHandler) {
hc.httpBusinessErrorHandler = append(hc.httpBusinessErrorHandler, handlerList...)
}
// OnRequestFinish 请求完成时间
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:36 2024/6/1
func (hc *HttpClient) OnRequestFinish(handlerList ...define.RequestFinishHandler) {
hc.requestFinishHandler = append(hc.requestFinishHandler, handlerList...)
}
// getRequestValidateMiddleware 请求验证的Middleware
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:40 2024/5/31
func (hc *HttpClient) getRequestValidateMiddleware() resty.RequestMiddleware {
return func(client *resty.Client, request *resty.Request) error {
return nil
}
}
// getResponseValidateMiddleware 获取相应数据验证的middleware
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:42 2024/5/31
func (hc *HttpClient) getResponseValidateMiddleware() resty.ResponseMiddleware {
return func(client *resty.Client, response *resty.Response) error {
return nil
}
}
// SetRestyClient 设置client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:54 2024/5/31
func (hc *HttpClient) SetRestyClient(restyClient *resty.Client) {
hc.Client = restyClient
}
// GetRestyClient 获取 resty client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:57 2024/5/31
func (hc *HttpClient) GetRestyClient() *resty.Client {
return hc.Client
}
// Request 发送请求
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:52 2024/5/31
func (hc *HttpClient) Request() *define.Response {
var (
cacheResult *define.Response
)
if cacheResult = hc.getCacheResult(); nil != cacheResult {
return cacheResult
}
hc.Client.OnBeforeRequest(hc.getRequestValidateMiddleware()) // 请求参数验证中间件必注册
hc.Client.OnAfterResponse(hc.getResponseValidateMiddleware()) // 响应验证中间件必注册
var (
err error
)
response := hc.newResponse()
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
response.Seq++
response.RequestCount++
if response.RestyResponse, err = hc.request.Send(); nil != err {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeSend,
Message: "response instance is nil",
}
for _, itemAfterResponse := range hc.requestFinishHandler {
itemAfterResponse(hc.reqConfig, response)
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
if nil == response.RestyResponse {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeSend,
Message: "response instance is nil",
}
for _, itemAfterResponse := range hc.requestFinishHandler {
itemAfterResponse(hc.reqConfig, response)
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
// 解析返回信息
// 设置缓存
response.IsCache, response.CacheError = hc.setCacheResult(response)
}
return nil
}
// newResponse 默认返回数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2024/6/1
func (hc *HttpClient) newResponse() *define.Response {
return &define.Response{
Header: map[string]string{},
Cookie: map[string]string{},
Body: "",
Code: "",
Message: "",
Data: "",
HttpCode: 0,
HttpCodeStatus: "",
ResponseDataRule: nil,
Seq: 0,
RequestStartTime: 0,
RequestFinishTime: 0,
UsedTime: 0,
RestyResponse: nil,
IsSuccess: false,
RequestCount: 0,
FailInfo: nil,
}
}
// fillResponseHeader 填充响应header
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:30 2024/6/5
func (hc *HttpClient) fillResponseHeader(response *define.Response) {
response.Header = map[string]string{} // 清空已有数据
response.HttpCode = response.RestyResponse.StatusCode() // http状态码
response.HttpCodeStatus = response.RestyResponse.Status() // http状态码描述
for headerName, headerValue := range response.RestyResponse.Header() {
if len(headerValue) > 0 {
response.Header[headerName] = headerValue[0]
} else {
response.Header[headerName] = ""
}
}
}
// fillResponseCookie 填充cookie
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:32 2024/6/5
func (hc *HttpClient) fillResponseCookie(response *define.Response) {
response.Cookie = map[string]string{} // 清空已有数据
for _, cookieValue := range response.RestyResponse.Cookies() {
response.Cookie[cookieValue.Name] = cookieValue.Value
}
}
// fillResponseBody 填充响应body
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:38 2024/6/5
func (hc *HttpClient) fillResponseBody(response *define.Response) {
response.Body = string(response.RestyResponse.Body())
response.Code = gjson.Get(response.Body, hc.reqConfig.CodeField).String()
response.Message = gjson.Get(response.Body, hc.reqConfig.MessageField).String()
response.Data = gjson.Get(response.Body, hc.reqConfig.DataField).String()
response.ExtendData = map[string]string{}
gjson.Parse(response.Body).ForEach(func(key, value gjson.Result) bool {
if key.String() == hc.reqConfig.CodeField ||
key.String() == hc.reqConfig.MessageField ||
key.String() == hc.reqConfig.DataField {
return true
}
response.ExtendData[key.String()] = value.String()
return true
})
}
// getCacheResult 获取缓存结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:04 2024/6/3
func (hc *HttpClient) getCacheResult() *define.Response {
if nil == hc.cacheInstance || !hc.cacheInstance.Enable() {
return nil
}
startTime := time.Now().UnixMilli()
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
cacheValue := strings.TrimSpace(hc.cacheInstance.GetValue(cacheKey))
if len(cacheValue) == 0 {
return nil
}
response := hc.newResponse()
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
return nil
}
response.IsCache = true // 设置缓存标记
response.RequestStartTime = startTime // 开始时间
response.RequestFinishTime = time.Now().UnixMilli() // 结束时间
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
response.CacheKey = cacheKey // 缓存key
response.CacheValue = cacheValue // 缓存值
return response
}
// setCacheResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:24 2024/6/3
func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
if nil == response || nil == hc.cacheInstance {
return false, nil
}
// 全局未开启或者当前请求不支持缓存
if !hc.cacheInstance.Enable() || !hc.cacheInstance.IsAllow(hc.reqConfig, response) {
return false, nil
}
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
if err := hc.cacheInstance.SetValue(cacheKey, serialize.JSON.MarshalForString(response)); nil != err {
return false, err
}
return true, nil
}