codex optz.
This commit is contained in:
61
utils/net.go
61
utils/net.go
@@ -56,6 +56,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -202,6 +203,30 @@ func createHTTPClient(timeout time.Duration) *http.Client {
|
||||
}
|
||||
}
|
||||
|
||||
func readHTTPResponse(resp *http.Response) ([]byte, error) {
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := checkHTTPStatus(resp, respBytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return respBytes, nil
|
||||
}
|
||||
|
||||
func checkHTTPStatus(resp *http.Response, body []byte) error {
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
respBytes := body
|
||||
if respBytes == nil && resp.Body != nil {
|
||||
respBytes, _ = io.ReadAll(resp.Body)
|
||||
}
|
||||
return fmt.Errorf("http status %d: %s", resp.StatusCode, string(respBytes))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HttpGet 发送HTTP GET请求
|
||||
// url: 请求地址
|
||||
// timeout: 超时时间(可选,默认30秒),可以传入多个,只使用第一个
|
||||
@@ -224,7 +249,7 @@ func HttpGet(url string, timeout ...time.Duration) ([]byte, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
return readHTTPResponse(resp)
|
||||
}
|
||||
|
||||
// HttpPostJSON 发送HTTP POST JSON请求
|
||||
@@ -232,13 +257,13 @@ func HttpGet(url string, timeout ...time.Duration) ([]byte, error) {
|
||||
// header: 请求头
|
||||
// data: 请求数据(将被序列化为JSON)
|
||||
// 返回: 响应体和错误信息
|
||||
func HttpPostJSON(url string, header map[string]string, data map[string]any) ([]byte, error) {
|
||||
func HttpPostJSON(url string, header map[string]string, data map[string]any, timeout ...time.Duration) ([]byte, error) {
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal json failed: %w", err)
|
||||
}
|
||||
|
||||
return HttpPost(url, header, jsonBytes)
|
||||
return HttpPost(url, header, jsonBytes, timeout...)
|
||||
}
|
||||
|
||||
// HttpPost 发送HTTP POST请求
|
||||
@@ -274,16 +299,7 @@ func HttpPost(url string, header map[string]string, data []byte, timeout ...time
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("http status %d: %s", resp.StatusCode, string(respBytes))
|
||||
}
|
||||
|
||||
return respBytes, nil
|
||||
return readHTTPResponse(resp)
|
||||
}
|
||||
|
||||
// HttpRequest 执行HTTP请求
|
||||
@@ -291,11 +307,14 @@ func HttpPost(url string, header map[string]string, data []byte, timeout ...time
|
||||
// timeout: 超时时间(可选,默认30秒),可以传入多个,只使用第一个
|
||||
// 返回: 响应体和错误信息
|
||||
func HttpRequest(r *http.Request, timeout ...time.Duration) ([]byte, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("http request is nil")
|
||||
}
|
||||
|
||||
timeoutDuration := getTimeoutDuration(timeout, DefaultHTTPTimeout)
|
||||
|
||||
// 如果请求还没有设置context,添加一个带超时的context
|
||||
if r.Context() == context.Background() || r.Context() == nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)
|
||||
if _, ok := r.Context().Deadline(); !ok {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), timeoutDuration)
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
}
|
||||
@@ -307,7 +326,7 @@ func HttpRequest(r *http.Request, timeout ...time.Duration) ([]byte, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
return readHTTPResponse(resp)
|
||||
}
|
||||
|
||||
// DownloadFile 下载文件
|
||||
@@ -333,6 +352,10 @@ func DownloadFile(url, saveTo string, fb func(length, downLen int64), timeout ..
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := checkHTTPStatus(resp, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.Body == nil {
|
||||
return fmt.Errorf("response body is nil for %s", url)
|
||||
}
|
||||
@@ -344,6 +367,10 @@ func DownloadFile(url, saveTo string, fb func(length, downLen int64), timeout ..
|
||||
fsize = -1
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(saveTo), 0755); err != nil {
|
||||
return fmt.Errorf("create dir for %s error: %w", saveTo, err)
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
file, err := os.Create(saveTo)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user