u
This commit is contained in:
1 parent
4ce951dd56
commit
8a3dea8545
44 files changed
+1599
-30
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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
|
||||
}
|
||||
Reference in new issue
Block a user