增加响应状态码判断 + 配置的默认值处理
This commit is contained in:
parent
24fe4c9adc
commit
bc59aba0ec
17
define.go
17
define.go
@ -13,14 +13,15 @@ package rpc
|
||||
//
|
||||
// Date : 14:08 2022/6/29
|
||||
type Service struct {
|
||||
Flag string `json:"flag" yaml:"flag"` // 服务标识, 全局唯一
|
||||
Domain string `json:"domain" yaml:"domain"` // 域名服务
|
||||
CodeField string `json:"code_field" yaml:"code_field"` // 状态码字段
|
||||
MessageField string `json:"message_field" yaml:"message_field"` // 消息字段
|
||||
DataField string `json:"data_field" yaml:"data_field"` // 数据字段
|
||||
SuccessCodeList []string `json:"success_code_list" yaml:"success_code_list"` // 成功的值
|
||||
ApiTable map[string]*Api `json:"api_table" yaml:"api_table"` // api列表
|
||||
ApiRetry ApiRetry `json:"api_retry" yaml:"api_retry"` // 重试策略
|
||||
Flag string `json:"flag" yaml:"flag"` // 服务标识, 全局唯一
|
||||
Domain string `json:"domain" yaml:"domain"` // 域名服务
|
||||
CodeField string `json:"code_field" yaml:"code_field"` // 状态码字段
|
||||
MessageField string `json:"message_field" yaml:"message_field"` // 消息字段
|
||||
DataField string `json:"data_field" yaml:"data_field"` // 数据字段
|
||||
SuccessCodeList []string `json:"success_code_list" yaml:"success_code_list"` // 成功的值
|
||||
SuccessHttpCodeList []int `json:"success_http_code_list" yaml:"success_http_code_list"` // 任务成功的http状态码, 不配置默认只有200(优先级高, 会覆盖)
|
||||
ApiTable map[string]*Api `json:"api_table" yaml:"api_table"` // api列表
|
||||
ApiRetry ApiRetry `json:"api_retry" yaml:"api_retry"` // 重试策略
|
||||
}
|
||||
|
||||
// Api 接口的数据结构
|
||||
|
99
rpc.go
99
rpc.go
@ -67,6 +67,12 @@ func (r *request) AddService(serviceInfo *Service) error {
|
||||
if nil == serviceInfo {
|
||||
return errors.New("service info is nil")
|
||||
}
|
||||
if nil == serviceInfo.SuccessCodeList {
|
||||
serviceInfo.SuccessCodeList = []string{"0"}
|
||||
}
|
||||
if nil == serviceInfo.SuccessHttpCodeList {
|
||||
serviceInfo.SuccessHttpCodeList = []int{http.StatusOK}
|
||||
}
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
if _, exist := r.serviceTable[serviceInfo.Flag]; exist {
|
||||
@ -120,6 +126,12 @@ func (r *request) AddServiceApi(serviceFlag string, apiConfig *Api) error {
|
||||
if serviceInfo, err = r.GetServiceInfo(serviceFlag); nil != err {
|
||||
return err
|
||||
}
|
||||
if len(apiConfig.SuccessCodeList) == 0 {
|
||||
apiConfig.SuccessCodeList = serviceInfo.SuccessCodeList
|
||||
}
|
||||
if len(apiConfig.SuccessHttpCodeList) == 0 {
|
||||
apiConfig.SuccessHttpCodeList = serviceInfo.SuccessHttpCodeList
|
||||
}
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
if nil == serviceInfo.ApiTable {
|
||||
@ -149,6 +161,7 @@ func (r *request) GetServiceApi(serviceFlag string, apiFlag string) (*Service, *
|
||||
if apiInfo, exist = serviceInfo.ApiTable[apiFlag]; !exist {
|
||||
return nil, nil, errors.New(serviceFlag + " : " + apiFlag + " -> api")
|
||||
}
|
||||
|
||||
return serviceInfo, apiInfo, nil
|
||||
}
|
||||
|
||||
@ -180,78 +193,6 @@ func (r *request) Get() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Post ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:25 2022/6/30
|
||||
func (r *request) Post() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Head ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:25 2022/6/30
|
||||
func (r *request) Head() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Put ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:24 2022/6/30
|
||||
func (r *request) Put() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Trace ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:24 2022/6/30
|
||||
func (r *request) Trace() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:24 2022/6/30
|
||||
func (r *request) Delete() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Options ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:24 2022/6/30
|
||||
func (r *request) Options() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Patch ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:24 2022/6/30
|
||||
func (r *request) Patch() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Connect ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:24 2022/6/30
|
||||
func (r *request) Connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send 统一的发送请求方法
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -271,11 +212,25 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
||||
if serviceConfig, apiConfig, err = r.GetServiceApi(serviceFlag, apiFlag); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
// 完整的请求地址
|
||||
fullURL, body = r.getFullURLAndBody(serviceConfig, apiConfig, parameter)
|
||||
if response, err = client.Do(apiConfig.Method, fullURL, apiConfig.Header, bytes.NewReader(body)); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
// 判断状态码
|
||||
isHttpSuccess := false
|
||||
for _, successCode := range apiConfig.SuccessHttpCodeList {
|
||||
if successCode == response.StatusCode {
|
||||
isHttpSuccess = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isHttpSuccess {
|
||||
return fmt.Errorf("http响应状态码异常 -> %v", response.StatusCode)
|
||||
}
|
||||
|
||||
if responseBody, err = io.ReadAll(response.Body); nil != err {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user