增加请求路由与参数格式化
This commit is contained in:
parent
82aa9d1e32
commit
cedfa9c3bf
@ -40,12 +40,14 @@ type ApiRequestConfig struct {
|
|||||||
Method string `json:"method"` // 请求方法
|
Method string `json:"method"` // 请求方法
|
||||||
Domain string `json:"domain"` // 请求域名
|
Domain string `json:"domain"` // 请求域名
|
||||||
URI string `json:"uri"` // 请求的路由
|
URI string `json:"uri"` // 请求的路由
|
||||||
BindRouterParam []string `json:"bind_router_param"` // 绑定到路由的参数列表
|
|
||||||
ResponseCodeField string `json:"response_code_field"` // 业务状态码字段
|
ResponseCodeField string `json:"response_code_field"` // 业务状态码字段
|
||||||
ResponseMessageField string `json:"response_message_field"` // 业务状态码描述的字段
|
ResponseMessageField string `json:"response_message_field"` // 业务状态码描述的字段
|
||||||
ResponseDataField string `json:"response_data_field"` // 业务数据的字段
|
ResponseDataField string `json:"response_data_field"` // 业务数据的字段
|
||||||
SuccessCodeList []string `json:"success_code_list"` // 代表请求成功的code列表
|
SuccessCodeList []string `json:"success_code_list"` // 代表请求成功的code列表
|
||||||
SuccessHttpCodelist []string `json:"success_http_code_list"` // 代表成功的http code列表
|
SuccessHttpCodeList []string `json:"success_http_code_list"` // 代表成功的http code列表
|
||||||
|
Parameter map[string]interface{} `json:"parameter"` // 传入的请求参数
|
||||||
|
Body []byte `json:"-"` // 请求体
|
||||||
|
FullURL string `json:"full_url"` // 完整请求地址
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApiResponse 接口响应结果
|
// ApiResponse 接口响应结果
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
package curl
|
package curl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -57,6 +59,7 @@ func Request(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResp
|
|||||||
//
|
//
|
||||||
// Date: 2022/05/01 21:29:09
|
// Date: 2022/05/01 21:29:09
|
||||||
func GET(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func GET(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +69,7 @@ func GET(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse
|
|||||||
//
|
//
|
||||||
// Date : 2022/05/01 21:31:36
|
// Date : 2022/05/01 21:31:36
|
||||||
func POST(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func POST(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +79,7 @@ func POST(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespons
|
|||||||
//
|
//
|
||||||
// Date : 2022/05/01 21:31:52
|
// Date : 2022/05/01 21:31:52
|
||||||
func PUT(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func PUT(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +89,7 @@ func PUT(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse
|
|||||||
//
|
//
|
||||||
// Date : 2022/05/01 21:32:08
|
// Date : 2022/05/01 21:32:08
|
||||||
func DELETE(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func DELETE(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +99,7 @@ func DELETE(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespo
|
|||||||
//
|
//
|
||||||
// Date : 2022/05/01 21:32:18
|
// Date : 2022/05/01 21:32:18
|
||||||
func OPTION(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func OPTION(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,6 +109,7 @@ func OPTION(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespo
|
|||||||
//
|
//
|
||||||
// Date : 2022/05/01 21:36:12
|
// Date : 2022/05/01 21:36:12
|
||||||
func Patch(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func Patch(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,6 +119,7 @@ func Patch(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespon
|
|||||||
//
|
//
|
||||||
// Date : 2022/05/01 21:36:24
|
// Date : 2022/05/01 21:36:24
|
||||||
func Trace(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func Trace(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,5 +129,39 @@ func Trace(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespon
|
|||||||
//
|
//
|
||||||
// Date : 2022/05/01 21:36:39
|
// Date : 2022/05/01 21:36:39
|
||||||
func Connect(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
func Connect(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse {
|
||||||
|
buildRequestURLAndParam(apiConfig, param)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// buildRequestURLAndParam 构建完整请求URL与请求参数
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 21:55 2022/5/1
|
||||||
|
func buildRequestURLAndParam(apiConfig *ApiRequestConfig, param map[string]interface{}) {
|
||||||
|
apiConfig.Method = strings.ToUpper(apiConfig.Method)
|
||||||
|
formatParam := make(map[string]interface{})
|
||||||
|
for paramName, paramValue := range param {
|
||||||
|
uriTemplate := "{" + paramName + "}"
|
||||||
|
if strings.Contains(apiConfig.URI, uriTemplate) {
|
||||||
|
apiConfig.URI = strings.ReplaceAll(apiConfig.URI, uriTemplate, fmt.Sprintf("%v", paramValue))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
formatParam[paramName] = paramValue
|
||||||
|
}
|
||||||
|
apiConfig.Parameter = formatParam
|
||||||
|
paramPair := make([]string, 0)
|
||||||
|
switch apiConfig.Method {
|
||||||
|
case http.MethodPost:
|
||||||
|
apiConfig.Body, _ = json.Marshal(formatParam)
|
||||||
|
default:
|
||||||
|
for paramName, paramValue := range formatParam {
|
||||||
|
paramPair = append(paramPair, fmt.Sprintf("%v=%v", paramName, paramValue))
|
||||||
|
}
|
||||||
|
if len(paramPair) > 0 {
|
||||||
|
apiConfig.URI = apiConfig.URI + "?" + strings.Join(paramPair, "&")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
apiConfig.FullURL = strings.ReplaceAll(apiConfig.Domain+"/"+apiConfig.URI, "//", "/")
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user