1、收码
This commit is contained in:
@ -48,12 +48,14 @@ func (c *HTTPClient) applyHeaders(req *http.Request, customHeaders map[string]st
|
||||
// path: 请求路径,将与 BaseURL 拼接
|
||||
// requestBody: 请求体数据,如果为 GET/DELETE 请求则为 nil
|
||||
// customHeaders: 自定义请求头,将覆盖默认请求头
|
||||
// responseData: 用于存储响应数据的目标结构体(指针类型)
|
||||
// responseData: 用于存储响应数据的目标结构体(指针类型),如果为 nil 则表示不需要 JSON 解码
|
||||
// rawResponse: 用于存储原始响应体字节切片(*[]byte),如果为 nil 则表示不需要原始响应
|
||||
func (c *HTTPClient) doRequest(
|
||||
method, path string,
|
||||
requestBody interface{},
|
||||
customHeaders map[string]string,
|
||||
responseData interface{},
|
||||
rawResponse *[]byte, // 新增参数:指向字节切片的指针,用于存储原始响应
|
||||
) error {
|
||||
// 拼接完整的 URL
|
||||
url := c.BaseURL + path
|
||||
@ -93,7 +95,7 @@ func (c *HTTPClient) doRequest(
|
||||
return fmt.Errorf("http request failed with status: %d, body: %s", resp.StatusCode, string(bodyBytes))
|
||||
}
|
||||
|
||||
// 解码 JSON 响应(支持 gzip)
|
||||
// 解码响应(支持 gzip)
|
||||
var reader io.Reader = resp.Body
|
||||
if resp.Header.Get("Content-Encoding") == "gzip" {
|
||||
gzipReader, err := gzip.NewReader(resp.Body)
|
||||
@ -104,8 +106,20 @@ func (c *HTTPClient) doRequest(
|
||||
reader = gzipReader
|
||||
}
|
||||
|
||||
// 首先读取整个响应体,然后决定如何处理
|
||||
bodyBytes, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read response body failed: %w", err)
|
||||
}
|
||||
|
||||
// 如果提供了原始响应目标,则填充它
|
||||
if rawResponse != nil {
|
||||
*rawResponse = bodyBytes
|
||||
}
|
||||
|
||||
// 如果提供了 JSON 解码目标,则尝试解码
|
||||
if responseData != nil {
|
||||
err = json.NewDecoder(reader).Decode(responseData)
|
||||
err = json.Unmarshal(bodyBytes, responseData) // 直接对字节切片使用 Unmarshal
|
||||
if err != nil {
|
||||
return fmt.Errorf("json decode response body failed: %w", err)
|
||||
}
|
||||
@ -119,7 +133,7 @@ func (c *HTTPClient) doRequest(
|
||||
// customHeaders: 自定义请求头
|
||||
// responseData: 用于存储响应数据的目标结构体(指针类型)
|
||||
func (c *HTTPClient) Get(path string, customHeaders map[string]string, responseData interface{}) error {
|
||||
return c.doRequest(http.MethodGet, path, nil, customHeaders, responseData)
|
||||
return c.doRequest(http.MethodGet, path, nil, customHeaders, responseData, nil) // rawResponse 传递 nil
|
||||
}
|
||||
|
||||
// Post 发送 POST 请求
|
||||
@ -128,7 +142,7 @@ func (c *HTTPClient) Get(path string, customHeaders map[string]string, responseD
|
||||
// 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)
|
||||
return c.doRequest(http.MethodPost, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
|
||||
}
|
||||
|
||||
// PostWithContentType 发送 POST 请求,支持自定义 Content-Type(如 application/json 或 multipart/form-data)
|
||||
@ -196,8 +210,15 @@ func (c *HTTPClient) PostWithContentType(path string, requestBody interface{}, c
|
||||
reader = gzipReader
|
||||
}
|
||||
|
||||
// 首先读取整个响应体,然后决定如何处理
|
||||
bodyBytes, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read response body failed: %w", err)
|
||||
}
|
||||
|
||||
// 如果提供了 JSON 解码目标,则尝试解码
|
||||
if responseData != nil {
|
||||
err = json.NewDecoder(reader).Decode(responseData)
|
||||
err = json.Unmarshal(bodyBytes, responseData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("json decode response body failed: %w", err)
|
||||
}
|
||||
@ -212,7 +233,7 @@ func (c *HTTPClient) PostWithContentType(path string, requestBody interface{}, c
|
||||
// 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)
|
||||
return c.doRequest(http.MethodPut, path, requestBody, customHeaders, responseData, nil) // rawResponse 传递 nil
|
||||
}
|
||||
|
||||
// Delete 发送 DELETE 请求
|
||||
@ -221,7 +242,7 @@ func (c *HTTPClient) Put(path string, requestBody interface{}, customHeaders map
|
||||
// responseData: 用于存储响应数据的目标结构体(指针类型)
|
||||
func (c *HTTPClient) Delete(path string, customHeaders map[string]string, responseData interface{}) error {
|
||||
// DELETE 请求通常没有请求体,但某些 RESTful API 可能支持
|
||||
return c.doRequest(http.MethodDelete, path, nil, customHeaders, responseData)
|
||||
return c.doRequest(http.MethodDelete, path, nil, customHeaders, responseData, nil) // rawResponse 传递 nil
|
||||
}
|
||||
|
||||
// Patch 发送 PATCH 请求
|
||||
@ -230,5 +251,19 @@ func (c *HTTPClient) Delete(path string, customHeaders map[string]string, respon
|
||||
// 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)
|
||||
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) {
|
||||
var raw []byte
|
||||
// responseData 传递 nil,rawResponse 传递 &raw
|
||||
err := c.doRequest(http.MethodGet, path, nil, customHeaders, nil, &raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user