升级构建 full url

This commit is contained in:
2022-06-30 17:41:41 +08:00
parent f8a6482e0a
commit 091571128e
3 changed files with 59 additions and 6 deletions

32
rpc.go
View File

@ -10,10 +10,13 @@ package rpc
import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"
"git.zhangdeman.cn/zhangdeman/util"
"github.com/ddliu/go-httpclient"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
@ -256,10 +259,14 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
serviceConfig *Service
apiConfig *Api
err error
fullURL string
client *httpclient.HttpClient
)
if serviceConfig, apiConfig, err = r.GetServiceApi(serviceFlag, apiFlag); nil != err {
return err
}
// 完整的请求地址
fullURL = r.getFullURL(serviceConfig, apiConfig, parameter)
return nil
}
@ -293,5 +300,28 @@ func (r *request) getFullURL(serviceConfig *Service, apiConfig *Api, parameter m
for name, val := range parameter {
fullURL = strings.ReplaceAll(fullURL, "{{"+name+"}}", fmt.Sprintf("%v", val))
}
return fullURL
parameterPair := make([]string, 0)
switch strings.ToUpper(apiConfig.Method) {
case http.MethodGet:
fallthrough
case http.MethodHead:
fallthrough
case http.MethodOptions:
fallthrough
case http.MethodConnect:
fallthrough
case http.MethodPatch:
fallthrough
case http.MethodTrace:
for name, val := range parameter {
var valStr string
_ = util.ConvertAssign(&valStr, val)
parameterPair = append(parameterPair, fmt.Sprintf("%v=%v", name, valStr))
}
}
query := strings.Join(parameterPair, "&")
if len(query) == 0 {
return strings.ReplaceAll(fullURL, "//", "/")
}
return strings.ReplaceAll(fullURL, "//", "/") + "?" + query
}