upgrade http client
This commit is contained in:
20
httpclient/define/cache.go
Normal file
20
httpclient/define/cache.go
Normal file
@ -0,0 +1,20 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-10-09 18:39
|
||||
package define
|
||||
|
||||
// CachePreHeatConfig 缓存预热配置, MinPercent / MinTTL 同时配置, 则任意一个满足, 均进行预热
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:40 2024/10/9
|
||||
type CachePreHeatConfig struct {
|
||||
Enable bool `json:"enable"` // 缓存预热是否可用
|
||||
MinPercent int64 `json:"min_percent"` // 最小百分比, 剩余有效期低于此百分比进行预热
|
||||
MinTTL int64 `json:"min_ttl"` // 最小剩余生命周期, 低于此百分比进行预热
|
||||
Force bool `json:"force"` // 启用预热的情况下, 强制预热, 会忽略 MinPercent / MinTTL 的配置
|
||||
}
|
17
httpclient/define/error.go
Normal file
17
httpclient/define/error.go
Normal file
@ -0,0 +1,17 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-05-31 16:02
|
||||
package define
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrRequestConfigNil = errors.New("REQUEST_CONFIG_NIL") // 请求配置 nil
|
||||
ErrFullUrlEmpty = errors.New("FULL_URL_EMPTY") // 没传 full_url
|
||||
ErrFullUrlInvalid = errors.New("FULL_URL_Invalid") // 请求 full_url 不是 http 或者 https 开头
|
||||
ErrMethodIsNotSupport = errors.New("METHOD_IS_NOT_SUPPORT") // 请求 method不支持
|
||||
)
|
43
httpclient/define/event.go
Normal file
43
httpclient/define/event.go
Normal file
@ -0,0 +1,43 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-05-31 14:51
|
||||
package define
|
||||
|
||||
// Http4xxHandler 4xx handler
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:54 2024/5/31
|
||||
type Http4xxHandler func(req *Request, rep *Response)
|
||||
|
||||
// Http5xxHandler 5xx handler
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:55 2024/5/31
|
||||
type Http5xxHandler func(req *Request, rep *Response)
|
||||
|
||||
// HttpBusinessErrorHandler 接口请求业务错误
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:04 2024/6/1
|
||||
type HttpBusinessErrorHandler func(req *Request, rep *Response)
|
||||
|
||||
// RequestSendErrorHandler 请求发送失败的处理逻辑
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:23 2024/6/1
|
||||
type RequestSendErrorHandler func(req *Request)
|
||||
|
||||
// RequestFinishHandler 请求最终完成事件, 不区分成功 OR 失败
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:34 2024/6/1
|
||||
type RequestFinishHandler func(req *Request, rep *Response)
|
56
httpclient/define/request.go
Normal file
56
httpclient/define/request.go
Normal file
@ -0,0 +1,56 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-05-24 17:09
|
||||
package define
|
||||
|
||||
// Request 请求配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:10 2024/5/24
|
||||
type Request struct {
|
||||
PathParam map[string]string `json:"path_param"` // 替换url中的占位符
|
||||
Body map[string]any `json:"body"` // 请求Body
|
||||
Header map[string]string `json:"header"` // 请求Header
|
||||
Cookie map[string]string `json:"cookie"` // 请求Cookie
|
||||
Query map[string]string `json:"query"` // 请求query
|
||||
FullUrl string `json:"full_url"` // 完整的请求URL
|
||||
ContentType string `json:"content_type"` // 请求类型
|
||||
Method string `json:"method"` // 请求方法
|
||||
DataField string `json:"data_field"` // 数据字段
|
||||
CodeField string `json:"code_field"` // 业务状态码字段
|
||||
MessageField string `json:"message_field"` // code描述字段
|
||||
DataReceiver interface{} `json:"-"` // 响应data部分数据解析
|
||||
SuccessCodeList []string `json:"success_code_list"` // 哪些业务状态码视为成功
|
||||
ConnectTimeout int64 `json:"connect_timeout"` // 连接超时时间: ms
|
||||
ReadTimeout int64 `json:"read_timeout"` // 读取超时时间
|
||||
RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则
|
||||
}
|
||||
|
||||
// RequestRetryRule 重试规则
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:26 2024/5/31
|
||||
type RequestRetryRule struct {
|
||||
RetryCount int `json:"retry_count"` // 重试次数
|
||||
RetryTimeInterval int64 `json:"retry_time_interval"` // 重试的时间间隔 1 - 10 之间, 单位毫秒
|
||||
RetryHttpCodeList []int64 `json:"retry_http_code_list"` // 哪些http状态码需要重试
|
||||
RetryBusinessCodeList []string `json:"retry_business_code_list"` // 哪些业务状态码需要重试
|
||||
}
|
||||
|
||||
const (
|
||||
DefaultConnectTimeout = 1000 // 默认连接超时时间
|
||||
DefaultReadTimeout = 1000 // 默认连接读取时间
|
||||
DefaultCodeField = "code" // 默认业务状态码字段
|
||||
DefaultMessageField = "message" // 默认状态码描述字段
|
||||
DefaultDataField = "data" // 默认数据字段
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultSuccessCodeList = []string{"0"} // 默认成功业务状态码
|
||||
)
|
70
httpclient/define/response.go
Normal file
70
httpclient/define/response.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-05-31 12:34
|
||||
package define
|
||||
|
||||
import (
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
// Response 响应的数据结构定义
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:34 2024/5/31
|
||||
type Response struct {
|
||||
Header map[string]string `json:"header"` // 响应header
|
||||
Cookie map[string]string `json:"cookie"` // 响应cookie
|
||||
Data string `json:"data"` // 响应body
|
||||
Code string `json:"code"` // 业务状态码
|
||||
Message string `json:"message"` // 业务状态码描述
|
||||
Body map[string]any `json:"body"` // 响应数据
|
||||
ExtendData map[string]string `json:"extend_data"` // 除去 code / message / data 之外的其他数据
|
||||
HttpCode int `json:"http_code"` // http状态码
|
||||
HttpCodeStatus string `json:"http_code_status"` // http状态码描述
|
||||
ResponseDataRule map[string]any `json:"response_data_rule"` // 返回数据的验证规则
|
||||
Seq int `json:"seq"` // 第几次请求
|
||||
RequestStartTime int64 `json:"request_start_time"` // 请求开始时间 : ms
|
||||
RequestFinishTime int64 `json:"request_finish_time"` // 请求完成时间 : ms
|
||||
UsedTime int64 `json:"used_time"` // 请求耗时 : ms
|
||||
RestyResponse *resty.Response `json:"-"` // 请求返回
|
||||
IsSuccess bool `json:"is_success"` // 是否请求成功
|
||||
RequestCount int `json:"request_count"` // 请求次数
|
||||
FailInfo *ResponseFailInfo `json:"fail_info"` // 请求失败信息记录
|
||||
CacheInfo *ResponseCacheInfo `json:"cache_info"` // 缓存信息
|
||||
}
|
||||
|
||||
// ResponseFailInfo 失败信息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:48 2024/6/1
|
||||
type ResponseFailInfo struct {
|
||||
Type string `json:"type"` // 失败类型
|
||||
Message string `json:"message"` // 失败信息
|
||||
}
|
||||
|
||||
// ResponseCacheInfo 缓存信息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:26 2024/6/14
|
||||
type ResponseCacheInfo struct {
|
||||
IsCache bool `json:"is_cache"` // 是否命中缓存
|
||||
SetCache bool `json:"set_cache"` // 是否设置缓存
|
||||
CacheKey string `json:"cache_key"` // 缓存key
|
||||
CacheValue string `json:"cache_value"` // 缓存值
|
||||
CacheEnable bool `json:"cache_enable"` // 是否允许缓存
|
||||
CacheError error `json:"-"` // 缓存是否异常
|
||||
}
|
||||
|
||||
const (
|
||||
RequestFailTypeSend = "SEND_REQUEST_FAIL" // 发送请求即失败, 问题出现在客户端
|
||||
RequestFailTypeClientError = "CLIENT_REQUEST_ERROR" // 请求失败, 原因出在客户端, 对应http code 4xx
|
||||
RequestFailTypeServerError = "SERVER_DEAL_ERROR" // 服务端处理失败, 对应 http code 5xx
|
||||
RequestFailTypeBusinessError = "SERVICE_BUSINESS_ERROR" // 返回状态码为200, 但是业务状态码非成功
|
||||
)
|
Reference in New Issue
Block a user