From 24fe4c9adcc067f74ab113768b23a9b0ce3b1d57 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 17:52:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=B7=E6=B1=82=E5=8F=91?= =?UTF-8?q?=E9=80=81=E4=BB=A5=E5=8F=8A=E5=93=8D=E5=BA=94=E4=BD=93=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpc.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/rpc.go b/rpc.go index a31cff6..326543d 100644 --- a/rpc.go +++ b/rpc.go @@ -8,8 +8,11 @@ package rpc import ( + "bytes" + "encoding/json" "errors" "fmt" + "io" "net/http" "strings" "sync" @@ -261,12 +264,21 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par err error fullURL string client *httpclient.HttpClient + body []byte + response *httpclient.Response + responseBody []byte ) if serviceConfig, apiConfig, err = r.GetServiceApi(serviceFlag, apiFlag); nil != err { return err } // 完整的请求地址 - fullURL = r.getFullURL(serviceConfig, apiConfig, parameter) + fullURL, body = r.getFullURLAndBody(serviceConfig, apiConfig, parameter) + if response, err = client.Do(apiConfig.Method, fullURL, apiConfig.Header, bytes.NewReader(body)); nil != err { + return err + } + if responseBody, err = io.ReadAll(response.Body); nil != err { + return err + } return nil } @@ -295,12 +307,13 @@ func (r *request) GetHttpClient(header map[string]string, timeout ApiTimeout) *h // Author : go_developer@163.com<白茶清欢> // // Date : 17:23 2022/6/30 -func (r *request) getFullURL(serviceConfig *Service, apiConfig *Api, parameter map[string]interface{}) string { +func (r *request) getFullURLAndBody(serviceConfig *Service, apiConfig *Api, parameter map[string]interface{}) (string, []byte) { fullURL := strings.ReplaceAll(serviceConfig.Domain+"/"+apiConfig.URI, "//", "/") for name, val := range parameter { fullURL = strings.ReplaceAll(fullURL, "{{"+name+"}}", fmt.Sprintf("%v", val)) } parameterPair := make([]string, 0) + var body []byte switch strings.ToUpper(apiConfig.Method) { case http.MethodGet: fallthrough @@ -318,10 +331,16 @@ func (r *request) getFullURL(serviceConfig *Service, apiConfig *Api, parameter m _ = util.ConvertAssign(&valStr, val) parameterPair = append(parameterPair, fmt.Sprintf("%v=%v", name, valStr)) } + case http.MethodPost: + fallthrough + case http.MethodPut: + fallthrough + case http.MethodDelete: + body, _ = json.Marshal(parameter) } query := strings.Join(parameterPair, "&") if len(query) == 0 { - return strings.ReplaceAll(fullURL, "//", "/") + return strings.ReplaceAll(fullURL, "//", "/"), body } - return strings.ReplaceAll(fullURL, "//", "/") + "?" + query + return strings.ReplaceAll(fullURL, "//", "/") + "?" + query, body }