完成一版请求发送

This commit is contained in:
白茶清欢 2022-05-02 15:17:21 +08:00
parent 55e9974c25
commit 2ac4f096d7
3 changed files with 74 additions and 30 deletions

View File

@ -11,7 +11,9 @@
// Date: 2022/05/01 19:56:48 // Date: 2022/05/01 19:56:48
package curl package curl
import "net/http" import (
"github.com/ddliu/go-httpclient"
)
const ( const (
// ContentTypeFormData form-data 请求 // ContentTypeFormData form-data 请求
@ -72,9 +74,9 @@ type Timeout struct {
// //
// Date: 2022/05/01 20:25:39 // Date: 2022/05/01 20:25:39
type ApiResponse struct { type ApiResponse struct {
RequestConfig *ApiRequestConfig `json:"request_config"` // 请求配置 RequestConfig *ApiRequestConfig `json:"request_config"` // 请求配置
Response *http.Response `json:"response"` // 响应体 Response *httpclient.Response `json:"response"` // 响应体
Exception *Exception `json:"exception"` // 异常信息 Exception *Exception `json:"exception"` // 异常信息
StartRequestTime int64 `json:"start_request_time"` // 开始请求时间, 纳秒 StartRequestTime int64 `json:"start_request_time"` // 开始请求时间, 纳秒
FinishRequestTime int64 `json:"finish_request_time"` // 完成请求时间,纳秒 FinishRequestTime int64 `json:"finish_request_time"` // 完成请求时间,纳秒
} }

View File

@ -36,6 +36,8 @@ const (
ResponseDataNotFound = "404004" ResponseDataNotFound = "404004"
// DomainInvalid 域名设置错误, 必须是 http:// 或者 https:// 开头 // DomainInvalid 域名设置错误, 必须是 http:// 或者 https:// 开头
DomainInvalid = "400001" DomainInvalid = "400001"
// SendRequestError 请求发送出现异常
SendRequestError = "400002"
// RequestMethodNotSupport 请求方法不支持 // RequestMethodNotSupport 请求方法不支持
RequestMethodNotSupport = "405001" RequestMethodNotSupport = "405001"
) )
@ -54,6 +56,7 @@ var exceptionTable = map[string]string{
ResponseDataNotFound: "基于配置无法获取响应体状态码, 请检查 {response_code_data} 的配置, 当前配置 {response_code_data}", ResponseDataNotFound: "基于配置无法获取响应体状态码, 请检查 {response_code_data} 的配置, 当前配置 {response_code_data}",
DomainInvalid: "api域名配置非法, 必须以 http:// 或者 https:// 开头, 当前配置 {domain}", DomainInvalid: "api域名配置非法, 必须以 http:// 或者 https:// 开头, 当前配置 {domain}",
RequestMethodNotSupport: "当前请求方法 {method} 不支持, 请核对相关配置", RequestMethodNotSupport: "当前请求方法 {method} 不支持, 请核对相关配置",
SendRequestError: "请求发送出现异常, 异常原因 : {real_reason}",
} }
// NewException 实力化异常 // NewException 实力化异常

View File

@ -12,10 +12,12 @@
package curl package curl
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/ddliu/go-httpclient" "github.com/ddliu/go-httpclient"
) )
@ -46,11 +48,11 @@ func Request(apiConfig *ApiRequestConfig, header map[string]string, param map[st
return Patch(apiConfig, header, param) return Patch(apiConfig, header, param)
default: default:
return &ApiResponse{ return &ApiResponse{
RequestConfig: apiConfig, RequestConfig: apiConfig,
HttpCode: 0, Exception: NewException(RequestMethodNotSupport, map[string]string{"method": apiConfig.Method}),
Body: nil, Response: nil,
BodyType: "", StartRequestTime: time.Now().UnixNano(),
Exception: NewException(RequestMethodNotSupport, map[string]string{"method": apiConfig.Method}), FinishRequestTime: time.Now().UnixNano(),
} }
} }
} }
@ -61,8 +63,9 @@ func Request(apiConfig *ApiRequestConfig, header map[string]string, param map[st
// //
// Date: 2022/05/01 21:29:09 // Date: 2022/05/01 21:29:09
func GET(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func GET(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodGet
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// POST post请求 // POST post请求
@ -71,8 +74,9 @@ func GET(apiConfig *ApiRequestConfig, header map[string]string, param map[string
// //
// Date : 2022/05/01 21:31:36 // Date : 2022/05/01 21:31:36
func POST(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func POST(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodPost
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// PUT put请求 // PUT put请求
@ -81,8 +85,9 @@ func POST(apiConfig *ApiRequestConfig, header map[string]string, param map[strin
// //
// Date : 2022/05/01 21:31:52 // Date : 2022/05/01 21:31:52
func PUT(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func PUT(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodPut
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// DELETE delete请求 // DELETE delete请求
@ -91,8 +96,9 @@ func PUT(apiConfig *ApiRequestConfig, header map[string]string, param map[string
// //
// Date : 2022/05/01 21:32:08 // Date : 2022/05/01 21:32:08
func DELETE(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func DELETE(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodDelete
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// OPTION option请求 // OPTION option请求
@ -101,8 +107,9 @@ func DELETE(apiConfig *ApiRequestConfig, header map[string]string, param map[str
// //
// Date : 2022/05/01 21:32:18 // Date : 2022/05/01 21:32:18
func OPTION(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func OPTION(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodOptions
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// Patch patch请求 // Patch patch请求
@ -111,8 +118,9 @@ func OPTION(apiConfig *ApiRequestConfig, header map[string]string, param map[str
// //
// Date : 2022/05/01 21:36:12 // Date : 2022/05/01 21:36:12
func Patch(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func Patch(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodPatch
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// Trace trace请求 // Trace trace请求
@ -121,8 +129,9 @@ func Patch(apiConfig *ApiRequestConfig, header map[string]string, param map[stri
// //
// Date : 2022/05/01 21:36:24 // Date : 2022/05/01 21:36:24
func Trace(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func Trace(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodTrace
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// Connect connect请求 // Connect connect请求
@ -131,8 +140,9 @@ func Trace(apiConfig *ApiRequestConfig, header map[string]string, param map[stri
// //
// Date : 2022/05/01 21:36:39 // Date : 2022/05/01 21:36:39
func Connect(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse { func Connect(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param) apiConfig.Method = http.MethodConnect
return nil buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
} }
// getHttpClient 获取httpclient实例 // getHttpClient 获取httpclient实例
@ -140,7 +150,7 @@ func Connect(apiConfig *ApiRequestConfig, header map[string]string, param map[st
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 22:27 2022/5/1 // Date : 22:27 2022/5/1
func getHttpClient(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *httpclient.HttpClient { func getHttpClient(apiConfig *ApiRequestConfig, header map[string]string) *httpclient.HttpClient {
fullHeader := make(map[string]string) fullHeader := make(map[string]string)
for name, val := range apiConfig.CommonHeader { for name, val := range apiConfig.CommonHeader {
fullHeader[name] = val fullHeader[name] = val
@ -156,7 +166,7 @@ func getHttpClient(apiConfig *ApiRequestConfig, header map[string]string, param
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:55 2022/5/1 // Date : 21:55 2022/5/1
func buildRequestURLAndParam(apiConfig *ApiRequestConfig, param map[string]interface{}) { func buildRequestURLAndParam(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) {
apiConfig.Method = strings.ToUpper(apiConfig.Method) apiConfig.Method = strings.ToUpper(apiConfig.Method)
formatParam := make(map[string]interface{}) formatParam := make(map[string]interface{})
for paramName, paramValue := range param { for paramName, paramValue := range param {
@ -181,5 +191,34 @@ func buildRequestURLAndParam(apiConfig *ApiRequestConfig, param map[string]inter
} }
} }
apiConfig.FullURL = strings.ReplaceAll(apiConfig.Domain+"/"+apiConfig.URI, "//", "/") apiConfig.FullURL = strings.ReplaceAll(apiConfig.Domain+"/"+apiConfig.URI, "//", "/")
}
// send 发送请求
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:53 2022/5/2
func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse {
var (
client *httpclient.HttpClient
err error
)
response := &ApiResponse{
RequestConfig: apiConfig,
Response: nil,
Exception: nil,
StartRequestTime: time.Now().UnixNano(),
FinishRequestTime: 0,
}
defer func() {
response.FinishRequestTime = time.Now().UnixNano()
}()
client = getHttpClient(apiConfig, header)
if response.Response, err = client.Do(apiConfig.Method, apiConfig.FullURL, nil, bytes.NewReader(apiConfig.Body)); nil != err {
response.Exception = NewException(SendRequestError, map[string]string{"real_reason": err.Error()})
}
return response
} }