From bc59aba0ecdd87921bb3530d34e445551ff17821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 30 Jun 2022 18:09:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=93=8D=E5=BA=94=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=A0=81=E5=88=A4=E6=96=AD=20+=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E9=BB=98=E8=AE=A4=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 17 +++++----- rpc.go | 99 +++++++++++++++---------------------------------------- 2 files changed, 36 insertions(+), 80 deletions(-) diff --git a/define.go b/define.go index 1d8407e..e3c2716 100644 --- a/define.go +++ b/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 接口的数据结构 diff --git a/rpc.go b/rpc.go index 326543d..7134860 100644 --- a/rpc.go +++ b/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 }