diff --git a/curl/define.go b/curl/define.go index 98dcc4f..16b42f2 100644 --- a/curl/define.go +++ b/curl/define.go @@ -46,6 +46,7 @@ type ApiRequestConfig struct { SuccessCodeList []string `json:"success_code_list"` // 代表请求成功的code列表 SuccessHttpCodeList []string `json:"success_http_code_list"` // 代表成功的http code列表 Parameter map[string]interface{} `json:"parameter"` // 传入的请求参数 + CommonHeader map[string]string `json:"common_header"` // 全部请求都要传的header Body []byte `json:"-"` // 请求体 FullURL string `json:"full_url"` // 完整请求地址 } diff --git a/curl/request.go b/curl/request.go index 6ea46a5..262ec7f 100644 --- a/curl/request.go +++ b/curl/request.go @@ -16,6 +16,8 @@ import ( "fmt" "net/http" "strings" + + "github.com/ddliu/go-httpclient" ) // Request 发送请求 @@ -23,25 +25,25 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:26:02 -func Request(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func Request(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { apiConfig.Method = strings.ToUpper(apiConfig.Method) switch apiConfig.Method { case http.MethodGet: - return GET(apiConfig, param) + return GET(apiConfig, header, param) case http.MethodPost: - return POST(apiConfig, param) + return POST(apiConfig, header, param) case http.MethodPut: - return PUT(apiConfig, param) + return PUT(apiConfig, header, param) case http.MethodDelete: - return DELETE(apiConfig, param) + return DELETE(apiConfig, header, param) case http.MethodConnect: - return Connect(apiConfig, param) + return Connect(apiConfig, header, param) case http.MethodOptions: - return OPTION(apiConfig, param) + return OPTION(apiConfig, header, param) case http.MethodTrace: - return Trace(apiConfig, param) + return Trace(apiConfig, header, param) case http.MethodPatch: - return Patch(apiConfig, param) + return Patch(apiConfig, header, param) default: return &ApiResponse{ RequestConfig: apiConfig, @@ -58,7 +60,7 @@ func Request(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResp // Author : go_developer@163.com<白茶清欢> // // Date: 2022/05/01 21:29:09 -func GET(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func GET(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } @@ -68,7 +70,7 @@ func GET(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:31:36 -func POST(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func POST(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } @@ -78,7 +80,7 @@ func POST(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespons // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:31:52 -func PUT(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func PUT(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } @@ -88,7 +90,7 @@ func PUT(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:32:08 -func DELETE(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func DELETE(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } @@ -98,7 +100,7 @@ func DELETE(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespo // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:32:18 -func OPTION(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func OPTION(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } @@ -108,7 +110,7 @@ func OPTION(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespo // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:36:12 -func Patch(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func Patch(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } @@ -118,7 +120,7 @@ func Patch(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespon // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:36:24 -func Trace(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func Trace(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } @@ -128,11 +130,27 @@ func Trace(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiRespon // Author : go_developer@163.com<白茶清欢> // // Date : 2022/05/01 21:36:39 -func Connect(apiConfig *ApiRequestConfig, param map[string]interface{}) *ApiResponse { +func Connect(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { buildRequestURLAndParam(apiConfig, param) return nil } +// getHttpClient 获取httpclient实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:27 2022/5/1 +func getHttpClient(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *httpclient.HttpClient { + fullHeader := make(map[string]string) + for name, val := range apiConfig.CommonHeader { + fullHeader[name] = val + } + for name, val := range header { + fullHeader[name] = val + } + return httpclient.NewHttpClient().WithHeaders(fullHeader) +} + // buildRequestURLAndParam 构建完整请求URL与请求参数 // // Author : go_developer@163.com<白茶清欢>