1、新接textverified

This commit is contained in:
2025-08-23 16:38:04 +08:00
parent 6c95e6fb04
commit 82d5acc873
45 changed files with 3154 additions and 461 deletions

View File

@ -50,13 +50,13 @@ func (c *HTTPClient) applyHeaders(req *http.Request, customHeaders map[string]st
// customHeaders: 自定义请求头,将覆盖默认请求头
// responseData: 用于存储响应数据的目标结构体(指针类型),如果为 nil 则表示不需要 JSON 解码
// rawResponse: 用于存储原始响应体字节切片(*[]byte如果为 nil 则表示不需要原始响应
func (c *HTTPClient) doRequest(
func (c *HTTPClient) DoRequest(
method, path string,
requestBody interface{},
customHeaders map[string]string,
responseData interface{},
rawResponse *[]byte, // 新增参数:指向字节切片的指针,用于存储原始响应
) error {
) (int, error) {
// 拼接完整的 URL
url := c.BaseURL + path
@ -65,7 +65,7 @@ func (c *HTTPClient) doRequest(
// 将请求体编码为 JSON
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return fmt.Errorf("json marshal request body failed: %w", err)
return -1, fmt.Errorf("json marshal request body failed: %w", err)
}
reqBodyReader = bytes.NewBuffer(jsonBody)
}
@ -73,7 +73,7 @@ func (c *HTTPClient) doRequest(
// 创建新的 HTTP 请求
req, err := http.NewRequest(method, url, reqBodyReader)
if err != nil {
return fmt.Errorf("create http request failed: %w", err)
return -1, fmt.Errorf("create http request failed: %w", err)
}
// 应用请求头
@ -85,14 +85,14 @@ func (c *HTTPClient) doRequest(
// 发送请求
resp, err := c.Client.Do(req)
if err != nil {
return fmt.Errorf("send http request failed: %w", err)
return -1, fmt.Errorf("send http request failed: %w", err)
}
defer resp.Body.Close()
// 检查 HTTP 状态码
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
bodyBytes, _ := io.ReadAll(resp.Body) // 读取错误响应体
return fmt.Errorf("http request failed with status: %d, body: %s", resp.StatusCode, string(bodyBytes))
return resp.StatusCode, fmt.Errorf("http request failed with status: %d, body: %s", resp.StatusCode, string(bodyBytes))
}
// 解码响应(支持 gzip
@ -100,7 +100,7 @@ func (c *HTTPClient) doRequest(
if resp.Header.Get("Content-Encoding") == "gzip" {
gzipReader, err := gzip.NewReader(resp.Body)
if err != nil {
return fmt.Errorf("create gzip reader failed: %w", err)
return http.StatusOK, fmt.Errorf("create gzip reader failed: %w", err)
}
defer gzipReader.Close()
reader = gzipReader
@ -109,7 +109,7 @@ func (c *HTTPClient) doRequest(
// 首先读取整个响应体,然后决定如何处理
bodyBytes, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("read response body failed: %w", err)
return http.StatusOK, fmt.Errorf("read response body failed: %w", err)
}
// 如果提供了原始响应目标,则填充它
@ -121,19 +121,19 @@ func (c *HTTPClient) doRequest(
if responseData != nil {
err = json.Unmarshal(bodyBytes, responseData) // 直接对字节切片使用 Unmarshal
if err != nil {
return fmt.Errorf("json decode response body failed: %w", err)
return http.StatusOK, fmt.Errorf("json decode response body failed: %w", err)
}
}
return nil
return http.StatusOK, nil
}
// Get 发送 GET 请求
// path: 请求路径
// customHeaders: 自定义请求头
// responseData: 用于存储响应数据的目标结构体(指针类型)
func (c *HTTPClient) Get(path string, customHeaders map[string]string, responseData interface{}) error {
return c.doRequest(http.MethodGet, path, nil, customHeaders, responseData, nil) // rawResponse 传递 nil
func (c *HTTPClient) Get(path string, customHeaders map[string]string, responseData interface{}) (int, error) {
return c.DoRequest(http.MethodGet, path, nil, customHeaders, responseData, nil) // rawResponse 传递 nil
}
// Post 发送 POST 请求
@ -141,8 +141,8 @@ func (c *HTTPClient) Get(path string, customHeaders map[string]string, responseD
// requestBody: 请求体数据
// customHeaders: 自定义请求头
// responseData: 用于存储响应数据的目标结构体(指针类型)
func (c *HTTPClient) Post(path string, requestBody interface{}, customHeaders map[string]string, responseData interface{}) error {
return c.doRequest(http.MethodPost, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
func (c *HTTPClient) Post(path string, requestBody interface{}, customHeaders map[string]string, responseData interface{}) (int, error) {
return c.DoRequest(http.MethodPost, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
}
// PostWithContentType 发送 POST 请求,支持自定义 Content-Type如 application/json 或 multipart/form-data
@ -232,17 +232,19 @@ func (c *HTTPClient) PostWithContentType(path string, requestBody interface{}, c
// requestBody: 请求体数据
// customHeaders: 自定义请求头
// responseData: 用于存储响应数据的目标结构体(指针类型)
func (c *HTTPClient) Put(path string, requestBody interface{}, customHeaders map[string]string, responseData interface{}) error {
return c.doRequest(http.MethodPut, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
// returns 状态码 和 错误信息
func (c *HTTPClient) Put(path string, requestBody interface{}, customHeaders map[string]string, responseData interface{}) (int, error) {
return c.DoRequest(http.MethodPut, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
}
// Delete 发送 DELETE 请求
// path: 请求路径
// customHeaders: 自定义请求头
// responseData: 用于存储响应数据的目标结构体(指针类型)
func (c *HTTPClient) Delete(path string, customHeaders map[string]string, responseData interface{}) error {
// returns 状态码 和 错误信息
func (c *HTTPClient) Delete(path string, customHeaders map[string]string, responseData interface{}) (int, error) {
// DELETE 请求通常没有请求体,但某些 RESTful API 可能支持
return c.doRequest(http.MethodDelete, path, nil, customHeaders, responseData, nil) // rawResponse 传递 nil
return c.DoRequest(http.MethodDelete, path, nil, customHeaders, responseData, nil) // rawResponse 传递 nil
}
// Patch 发送 PATCH 请求
@ -250,20 +252,39 @@ func (c *HTTPClient) Delete(path string, customHeaders map[string]string, respon
// requestBody: 请求体数据
// customHeaders: 自定义请求头
// responseData: 用于存储响应数据的目标结构体(指针类型)
func (c *HTTPClient) Patch(path string, requestBody interface{}, customHeaders map[string]string, responseData interface{}) error {
return c.doRequest(http.MethodPatch, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
// returns 状态码 和 错误信息
func (c *HTTPClient) Patch(path string, requestBody interface{}, customHeaders map[string]string, responseData interface{}) (int, error) {
return c.DoRequest(http.MethodPatch, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
}
// GetRaw 发送 GET 请求并返回原始响应体
// path: 请求路径
// customHeaders: 自定义请求头
// 返回值: 原始响应体字节切片或错误
func (c *HTTPClient) GetRaw(path string, customHeaders map[string]string) ([]byte, error) {
// 状态码
// 错误信息
func (c *HTTPClient) GetRaw(path string, customHeaders map[string]string) ([]byte, int, error) {
var raw []byte
// responseData 传递 nilrawResponse 传递 &raw
err := c.doRequest(http.MethodGet, path, nil, customHeaders, nil, &raw)
statusCode, err := c.DoRequest(http.MethodGet, path, nil, customHeaders, nil, &raw)
if err != nil {
return nil, err
return nil, statusCode, err
}
return raw, nil
return raw, statusCode, nil
}
// PostRaw 发送 POST 请求并返回原始响应体
// path: 请求路径
// customHeaders: 自定义请求头
// 返回值: 原始响应体字节切片或错误
// 状态码
// 错误信息
func (c *HTTPClient) PostRaw(path string, customHeaders map[string]string) ([]byte, int, error) {
var raw []byte
// responseData 传递 nilrawResponse 传递 &raw
statusCode, err := c.DoRequest(http.MethodPost, path, nil, customHeaders, nil, &raw)
if err != nil {
return nil, statusCode, err
}
return raw, statusCode, nil
}