完成一版请求发送

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

View File

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

View File

@ -12,10 +12,12 @@
package curl
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"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)
default:
return &ApiResponse{
RequestConfig: apiConfig,
HttpCode: 0,
Body: nil,
BodyType: "",
Exception: NewException(RequestMethodNotSupport, map[string]string{"method": apiConfig.Method}),
RequestConfig: apiConfig,
Exception: NewException(RequestMethodNotSupport, map[string]string{"method": apiConfig.Method}),
Response: nil,
StartRequestTime: time.Now().UnixNano(),
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
func GET(apiConfig *ApiRequestConfig, header map[string]string, param map[string]interface{}) *ApiResponse {
buildRequestURLAndParam(apiConfig, param)
return nil
apiConfig.Method = http.MethodGet
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// POST post请求
@ -71,8 +74,9 @@ func GET(apiConfig *ApiRequestConfig, header map[string]string, param map[string
//
// 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
apiConfig.Method = http.MethodPost
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// PUT put请求
@ -81,8 +85,9 @@ func POST(apiConfig *ApiRequestConfig, header map[string]string, param map[strin
//
// 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
apiConfig.Method = http.MethodPut
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// DELETE delete请求
@ -91,8 +96,9 @@ func PUT(apiConfig *ApiRequestConfig, header map[string]string, param map[string
//
// 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
apiConfig.Method = http.MethodDelete
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// OPTION option请求
@ -101,8 +107,9 @@ func DELETE(apiConfig *ApiRequestConfig, header map[string]string, param map[str
//
// 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
apiConfig.Method = http.MethodOptions
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// Patch patch请求
@ -111,8 +118,9 @@ func OPTION(apiConfig *ApiRequestConfig, header map[string]string, param map[str
//
// 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
apiConfig.Method = http.MethodPatch
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// Trace trace请求
@ -121,8 +129,9 @@ func Patch(apiConfig *ApiRequestConfig, header map[string]string, param map[stri
//
// 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
apiConfig.Method = http.MethodTrace
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// Connect connect请求
@ -131,8 +140,9 @@ func Trace(apiConfig *ApiRequestConfig, header map[string]string, param map[stri
//
// 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
apiConfig.Method = http.MethodConnect
buildRequestURLAndParam(apiConfig, header, param)
return send(apiConfig, header)
}
// getHttpClient 获取httpclient实例
@ -140,7 +150,7 @@ func Connect(apiConfig *ApiRequestConfig, header map[string]string, param map[st
// 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 {
func getHttpClient(apiConfig *ApiRequestConfig, header map[string]string) *httpclient.HttpClient {
fullHeader := make(map[string]string)
for name, val := range apiConfig.CommonHeader {
fullHeader[name] = val
@ -156,7 +166,7 @@ func getHttpClient(apiConfig *ApiRequestConfig, header map[string]string, param
// Author : go_developer@163.com<白茶清欢>
//
// 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)
formatParam := make(map[string]interface{})
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, "//", "/")
}
// 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
}