186 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			186 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Package curl ...
 | |
| //
 | |
| // Author: go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Description:
 | |
| //
 | |
| // File: request.go
 | |
| //
 | |
| // Version: 1.0.0
 | |
| //
 | |
| // Date: 2022/05/01 21:25:03
 | |
| package curl
 | |
| 
 | |
| import (
 | |
| 	"encoding/json"
 | |
| 	"fmt"
 | |
| 	"net/http"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/ddliu/go-httpclient"
 | |
| )
 | |
| 
 | |
| // Request 发送请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:26:02
 | |
| 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, header, param)
 | |
| 	case http.MethodPost:
 | |
| 		return POST(apiConfig, header, param)
 | |
| 	case http.MethodPut:
 | |
| 		return PUT(apiConfig, header, param)
 | |
| 	case http.MethodDelete:
 | |
| 		return DELETE(apiConfig, header, param)
 | |
| 	case http.MethodConnect:
 | |
| 		return Connect(apiConfig, header, param)
 | |
| 	case http.MethodOptions:
 | |
| 		return OPTION(apiConfig, header, param)
 | |
| 	case http.MethodTrace:
 | |
| 		return Trace(apiConfig, header, param)
 | |
| 	case http.MethodPatch:
 | |
| 		return Patch(apiConfig, header, param)
 | |
| 	default:
 | |
| 		return &ApiResponse{
 | |
| 			RequestConfig: apiConfig,
 | |
| 			HttpCode:      0,
 | |
| 			Body:          nil,
 | |
| 			BodyType:      "",
 | |
| 			Exception:     NewException(RequestMethodNotSupport, map[string]string{"method": apiConfig.Method}),
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // GET ...
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date: 2022/05/01 21:29:09
 | |
| func GET(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
 | |
| 	buildRequestURLAndParam(apiConfig, param)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // POST post请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:31:36
 | |
| func POST(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
 | |
| 	buildRequestURLAndParam(apiConfig, param)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // PUT put请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:31:52
 | |
| func PUT(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
 | |
| 	buildRequestURLAndParam(apiConfig, param)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // DELETE delete请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:32:08
 | |
| func DELETE(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
 | |
| 	buildRequestURLAndParam(apiConfig, param)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // OPTION option请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:32:18
 | |
| func OPTION(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
 | |
| 	buildRequestURLAndParam(apiConfig, param)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // Patch patch请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:36:12
 | |
| func Patch(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
 | |
| 	buildRequestURLAndParam(apiConfig, param)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // Trace trace请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:36:24
 | |
| func Trace(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
 | |
| 	buildRequestURLAndParam(apiConfig, param)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // Connect connect请求
 | |
| //
 | |
| // Author : go_developer@163.com<白茶清欢>
 | |
| //
 | |
| // Date : 2022/05/01 21:36:39
 | |
| 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<白茶清欢>
 | |
| //
 | |
| // 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, "//", "/")
 | |
| 
 | |
| }
 |