// Package request...
//
// Description : 定义请求的数据结构
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-08-01 8:28 下午
package request

// APIMethod 定义API请求的信息
//
// Author : go_developer@163.com<白茶清欢>
//
// 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
	Parameter     map[string]interface{} `json:"parameter"`      // 请求参数
}

// ResponseConfig ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:32 下午 2021/8/1
type ResponseConfig struct {
	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"`      // 哪些状态码被认为是请求成功
	SuccessHttpCode []string `json:"success_http_code"` // 被认为是成功的http code
}

// APIResponse API响应数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:54 下午 2021/8/1
type APIResponse struct {
	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
)