基础http请求库,半成品,未开发完

This commit is contained in:
2021-08-12 22:18:47 +08:00
parent 54870add95
commit 0140502988
2 changed files with 147 additions and 17 deletions

View File

@ -13,12 +13,12 @@ package request
//
// Date : 8:29 下午 2021/8/1
type APIMethod struct {
ServiceDomain string `json:"service_domain"` // 调用服务的域名
URI string `json:"uri"` // 调用的URI
Method string `json:"method"` // 请求方法
ISRestfulURI bool `json:"is_restful_uri"` // 是否为restful uri
Header map[string]string `json:"header"` // 请求header
ServiceDomain string `json:"service_domain"` // 调用服务的域名
URI string `json:"uri"` // 调用的URI
Method string `json:"method"` // 请求方法
ISRestfulURI bool `json:"is_restful_uri"` // 是否为restful uri
Header map[string]string `json:"header"` // 请求header
Parameter map[string]interface{} `json:"parameter"` // 请求参数
}
// ResponseConfig ...
@ -27,12 +27,17 @@ type APIMethod struct {
//
// Date : 8:32 下午 2021/8/1
type ResponseConfig struct {
ISJson bool `json:"is_json"` // 响应数据是否为json
CodeKey string `json:"code_key"` // 代表业务Code的Key
MessageKey string `json:"message_key"` // 描述业务code的key
DataKey string `json:"data_key"` // 返回数据的key
SuccessRule string `json:"success_rule"` // 请求成功的规则, http_code / business_code http状态码或者业务状态码, 如果是通过http code 判断是否为请求成功 code key / message key 配置无效
SuccessCode []string `json:"success_code"` // 哪些状态码被认为是请求成功
RetryRule int `json:"retry_rule"` // 重试规则 0 - http error 1 - business error 2 - both 0 && 1
RetryCnt int `json:"retry_cnt"` // 重试次数(重试次数 1, 最多请求两次)
RetryInterval int `json:"retry_interval"` // 重试的时间间隔, 单位ms, 为 0 立即重试, > 0 休眠指定ms后重试
ISJson bool `json:"is_json"` // 响应数据是否为json
CodeKey string `json:"code_key"` // 代表业务Code的Key
MessageKey string `json:"message_key"` // 描述业务code的key
TraceKey string `json:"trace_key"` // 接口返回的trace追踪字段key
DataKey string `json:"data_key"` // 返回数据的key
CostKey string `json:"cost_key"` // 代表接口耗时的key
SuccessRule string `json:"success_rule"` // 请求成功的规则, http_code / business_code http状态码或者业务状态码, 如果是通过http code 判断是否为请求成功 code key / message key 配置无效
SuccessCode []string `json:"success_code"` // 哪些状态码被认为是请求成功
}
// APIResponse API响应数据
@ -41,8 +46,34 @@ type ResponseConfig struct {
//
// Date : 8:54 下午 2021/8/1
type APIResponse struct {
Data string `json:"data"` // 响应数据
Cost int64 `json:"cost"` // 接口耗时
StartRequestTime int64 `json:"start_request_time"` // 开始请求时间
FinishRequestTime int64 `json:"finish_request_time"` // 完成请求时间
Code string `json:"code"` // 响应状态码
Message string `json:"message"` // 响应状态码的描述
Data string `json:"data"` // 响应数据
Success bool `json:"success"` // 请求是否成功
TraceID string `json:"trace_id"` // 日志追踪traceID
Cost int64 `json:"cost"` // 接口耗时(第三方接口返回)
RealCost int64 `json:"real_cost"` // 内部从发起请求,到拿到结果的耗时
TotalCost int64 `json:"total_cost"` // 累计请求加一起耗时
TotalRealCost int64 `json:"total_real_cost"` // 累计真实耗时
ErrorList []error `json:"error_list"` // 重试过程中累计出现过的异常
StartRequestTime int64 `json:"start_request_time"` // 开始请求时间
FinishRequestTime int64 `json:"finish_request_time"` // 完成请求时间
}
const (
// ResponseErrorTypeInternal 内部异常,请求没发出去
ResponseErrorTypeInternal = "internal"
// ResponseErrorTypeHttpStatus http 状态码异常
ResponseErrorTypeHttpStatus = "http"
// ResponseErrorTypeBusinessCode 业务状态码异常
ResponseErrorTypeBusinessCode = "business"
)
const (
// RetryRuleOnlyHttpError 仅 http 异常重试
RetryRuleOnlyHttpError = 0
// RetryRuleOnlyBusinessError 仅业务状态码异常重试
RetryRuleOnlyBusinessError = 1
// RetryRuleMixed 任意一种异常出现,均重试
RetryRuleMixed = 2
)