扩展事件定义

This commit is contained in:
2024-06-01 18:41:26 +08:00
parent 196e81191a
commit 4d38cc11c3
6 changed files with 125 additions and 75 deletions

View File

@ -11,6 +11,7 @@ import (
"git.zhangdeman.cn/gateway/httpclient/define"
"git.zhangdeman.cn/gateway/httpclient/validate"
"github.com/go-resty/resty/v2"
"time"
)
// NewHttpClient 获取http client
@ -25,9 +26,13 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
}
restyClient, restyRequest := NewRestyClient(reqConfig)
hc := &HttpClient{
Client: restyClient,
request: restyRequest,
reqConfig: reqConfig,
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),
}
return hc, nil
}
@ -39,8 +44,49 @@ func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
// Date : 15:27 2024/5/31
type HttpClient struct {
*resty.Client
request *resty.Request
reqConfig *define.Request
request *resty.Request
reqConfig *define.Request
http4xxHandler []define.Http4xxHandler
http5xxHandler []define.Http5xxHandler
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
requestSendErrorHandler []define.RequestSendErrorHandler
requestFinishHandler []define.RequestFinishHandler
}
// 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
@ -95,7 +141,23 @@ func (hc *HttpClient) Request() *define.Response {
err error
)
response := &define.Response{
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
response := hc.newResponse(i+1, hc.reqConfig.RetryRule.RetryCount+1)
if response.RestyResponse, err = hc.request.Send(); nil != err {
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
}
}
return nil
}
// newResponse 默认返回数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2024/6/1
func (hc *HttpClient) newResponse(seq int, requestCount int) *define.Response {
return &define.Response{
Header: map[string]string{},
Cookie: map[string]string{},
Body: map[string]any{},
@ -105,19 +167,13 @@ func (hc *HttpClient) Request() *define.Response {
HttpCode: 0,
HttpCodeStatus: "",
ResponseDataRule: nil,
Seq: 0,
Seq: seq,
RequestStartTime: 0,
FinishRequestTime: 0,
UsedTime: 0,
RestyResponse: nil,
IsSuccess: false,
RequestCount: 0,
FailInfoList: make([]any, 0),
RequestCount: requestCount,
FailInfo: nil,
}
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
if response.RestyResponse, err = hc.request.Send(); nil != err {
}
}
return nil
}