20260507213409
This commit is contained in:
1 parent
3070eac71b
commit
4c7f516a03
204 files changed
+3623
No files matched your search
@@ -0,0 +1,130 @@
|
||||
package requestx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3/client"
|
||||
)
|
||||
|
||||
// 全局 Fiber Client
|
||||
var cc = client.New()
|
||||
|
||||
func init() {
|
||||
cc.SetTimeout(10 * time.Second)
|
||||
}
|
||||
|
||||
// mergeConfig 合并默认配置和用户配置
|
||||
func mergeConfig(defaultCfg, userCfg client.Config) client.Config {
|
||||
if userCfg.Ctx != nil {
|
||||
defaultCfg.Ctx = userCfg.Ctx
|
||||
}
|
||||
if userCfg.Body != nil {
|
||||
defaultCfg.Body = userCfg.Body
|
||||
}
|
||||
if userCfg.Header != nil {
|
||||
if defaultCfg.Header == nil {
|
||||
defaultCfg.Header = map[string]string{}
|
||||
}
|
||||
for k, v := range userCfg.Header {
|
||||
defaultCfg.Header[k] = v
|
||||
}
|
||||
}
|
||||
if userCfg.Param != nil {
|
||||
defaultCfg.Param = userCfg.Param
|
||||
}
|
||||
if userCfg.PathParam != nil {
|
||||
defaultCfg.PathParam = userCfg.PathParam
|
||||
}
|
||||
if userCfg.FormData != nil {
|
||||
defaultCfg.FormData = userCfg.FormData
|
||||
}
|
||||
if userCfg.Timeout != 0 {
|
||||
defaultCfg.Timeout = userCfg.Timeout
|
||||
}
|
||||
if userCfg.UserAgent != "" {
|
||||
defaultCfg.UserAgent = userCfg.UserAgent
|
||||
}
|
||||
if userCfg.Referer != "" {
|
||||
defaultCfg.Referer = userCfg.Referer
|
||||
}
|
||||
defaultCfg.DisablePathNormalizing = userCfg.DisablePathNormalizing
|
||||
defaultCfg.MaxRedirects = userCfg.MaxRedirects
|
||||
|
||||
return defaultCfg
|
||||
}
|
||||
|
||||
func Get(url string, cfg client.Config) (string, int, error) {
|
||||
defaultCfg := client.Config{
|
||||
Ctx: context.Background(),
|
||||
}
|
||||
finalCfg := mergeConfig(defaultCfg, cfg)
|
||||
|
||||
resp, err := cc.Get(url, finalCfg)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
|
||||
return string(resp.Body()), resp.StatusCode(), nil
|
||||
}
|
||||
|
||||
func PostJSON(url string, data any, cfg client.Config) (string, int, error) {
|
||||
defaultCfg := client.Config{
|
||||
Ctx: context.Background(),
|
||||
Body: data,
|
||||
Header: map[string]string{"Content-Type": "application/json"},
|
||||
}
|
||||
finalCfg := mergeConfig(defaultCfg, cfg)
|
||||
|
||||
resp, err := cc.Post(url, finalCfg)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
return string(resp.Body()), resp.StatusCode(), nil
|
||||
}
|
||||
|
||||
func PostForm(url string, formData map[string]string, cfg client.Config) (string, int, error) {
|
||||
defaultCfg := client.Config{
|
||||
Ctx: context.Background(),
|
||||
FormData: formData,
|
||||
Header: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
|
||||
}
|
||||
finalCfg := mergeConfig(defaultCfg, cfg)
|
||||
|
||||
resp, err := cc.Post(url, finalCfg)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
return string(resp.Body()), resp.StatusCode(), nil
|
||||
}
|
||||
|
||||
func GetFileStream(ctx context.Context, url string) (io.ReadCloser, int64, string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0")
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
err := resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
return nil, 0, "", fmt.Errorf("request failed: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return resp.Body, resp.ContentLength, resp.Header.Get("Content-Type"), nil
|
||||
}
|
||||
Reference in new issue
Block a user