fix: 修复导入路径以支持模块化结构
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"test/providers/postgres"
|
"{{.ModuleName}}/providers/postgres"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
package req
|
package req
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"test/providers/req/cookiejar"
|
"{{.ModuleName}}/providers/req/cookiejar"
|
||||||
|
|
||||||
"github.com/imroc/req/v3"
|
"github.com/imroc/req/v3"
|
||||||
"go.ipao.vip/atom/container"
|
"go.ipao.vip/atom/container"
|
||||||
"go.ipao.vip/atom/opt"
|
"go.ipao.vip/atom/opt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -64,25 +64,25 @@ func Provide(opts ...opt.Option) error {
|
|||||||
client.EnableInsecureSkipVerify()
|
client.EnableInsecureSkipVerify()
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.UserAgent != "" {
|
if config.UserAgent != "" {
|
||||||
client.SetUserAgent(config.UserAgent)
|
client.SetUserAgent(config.UserAgent)
|
||||||
}
|
}
|
||||||
if config.BaseURL != "" {
|
if config.BaseURL != "" {
|
||||||
client.SetBaseURL(config.BaseURL)
|
client.SetBaseURL(config.BaseURL)
|
||||||
}
|
}
|
||||||
if config.Timeout > 0 {
|
if config.Timeout > 0 {
|
||||||
client.SetTimeout(time.Duration(config.Timeout) * time.Second)
|
client.SetTimeout(time.Duration(config.Timeout) * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.CommonHeaders != nil {
|
if config.CommonHeaders != nil {
|
||||||
client.SetCommonHeaders(config.CommonHeaders)
|
client.SetCommonHeaders(config.CommonHeaders)
|
||||||
}
|
}
|
||||||
if config.CommonQuery != nil {
|
if config.CommonQuery != nil {
|
||||||
client.SetCommonQueryParams(config.CommonQuery)
|
client.SetCommonQueryParams(config.CommonQuery)
|
||||||
}
|
}
|
||||||
if config.ContentType != "" {
|
if config.ContentType != "" {
|
||||||
client.SetCommonContentType(config.ContentType)
|
client.SetCommonContentType(config.ContentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.AuthBasic.Username != "" && config.AuthBasic.Password != "" {
|
if config.AuthBasic.Username != "" && config.AuthBasic.Password != "" {
|
||||||
client.SetCommonBasicAuth(config.AuthBasic.Username, config.AuthBasic.Password)
|
client.SetCommonBasicAuth(config.AuthBasic.Username, config.AuthBasic.Password)
|
||||||
@@ -100,12 +100,12 @@ func Provide(opts ...opt.Option) error {
|
|||||||
client.SetRedirectPolicy(parsePolicies(config.RedirectPolicy)...)
|
client.SetRedirectPolicy(parsePolicies(config.RedirectPolicy)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.client = client
|
c.client = client
|
||||||
if c.jar != nil {
|
if c.jar != nil {
|
||||||
container.AddCloseAble(func() { _ = c.jar.Save() })
|
container.AddCloseAble(func() { _ = c.jar.Save() })
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}, o.DiOptions()...)
|
}, o.DiOptions()...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parsePolicies(policies []string) []req.RedirectPolicy {
|
func parsePolicies(policies []string) []req.RedirectPolicy {
|
||||||
@@ -140,11 +140,11 @@ func parsePolicies(policies []string) []req.RedirectPolicy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) R() *req.Request {
|
func (c *Client) R() *req.Request {
|
||||||
return c.client.R()
|
return c.client.R()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) RWithCtx(ctx context.Context) *req.Request {
|
func (c *Client) RWithCtx(ctx context.Context) *req.Request {
|
||||||
return c.client.R().SetContext(ctx)
|
return c.client.R().SetContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SaveCookJar() error {
|
func (c *Client) SaveCookJar() error {
|
||||||
@@ -166,78 +166,78 @@ func (c *Client) AllCookiesKV() map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetCookie(u *url.URL, cookies []*http.Cookie) {
|
func (c *Client) SetCookie(u *url.URL, cookies []*http.Cookie) {
|
||||||
c.jar.SetCookies(u, cookies)
|
c.jar.SetCookies(u, cookies)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DoJSON(ctx context.Context, method, url string, in any, out any) error {
|
func (c *Client) DoJSON(ctx context.Context, method, url string, in any, out any) error {
|
||||||
r := c.RWithCtx(ctx)
|
r := c.RWithCtx(ctx)
|
||||||
if in != nil {
|
if in != nil {
|
||||||
r.SetBody(in)
|
r.SetBody(in)
|
||||||
}
|
}
|
||||||
if out != nil {
|
if out != nil {
|
||||||
r.SetSuccessResult(out)
|
r.SetSuccessResult(out)
|
||||||
}
|
}
|
||||||
var resp *req.Response
|
var resp *req.Response
|
||||||
var err error
|
var err error
|
||||||
switch strings.ToUpper(method) {
|
switch strings.ToUpper(method) {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
resp, err = r.Get(url)
|
resp, err = r.Get(url)
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
resp, err = r.Post(url)
|
resp, err = r.Post(url)
|
||||||
case http.MethodPut:
|
case http.MethodPut:
|
||||||
resp, err = r.Put(url)
|
resp, err = r.Put(url)
|
||||||
case http.MethodPatch:
|
case http.MethodPatch:
|
||||||
resp, err = r.Patch(url)
|
resp, err = r.Patch(url)
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
resp, err = r.Delete(url)
|
resp, err = r.Delete(url)
|
||||||
default:
|
default:
|
||||||
resp, err = r.Send(method, url)
|
resp, err = r.Send(method, url)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return resp.Err
|
return resp.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetJSON(ctx context.Context, url string, out any, query map[string]string) error {
|
func (c *Client) GetJSON(ctx context.Context, url string, out any, query map[string]string) error {
|
||||||
r := c.RWithCtx(ctx)
|
r := c.RWithCtx(ctx)
|
||||||
if query != nil {
|
if query != nil {
|
||||||
r.SetQueryParams(query)
|
r.SetQueryParams(query)
|
||||||
}
|
}
|
||||||
r.SetSuccessResult(out)
|
r.SetSuccessResult(out)
|
||||||
resp, err := r.Get(url)
|
resp, err := r.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return resp.Err
|
return resp.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PostJSON(ctx context.Context, url string, in any, out any) error {
|
func (c *Client) PostJSON(ctx context.Context, url string, in any, out any) error {
|
||||||
r := c.RWithCtx(ctx)
|
r := c.RWithCtx(ctx)
|
||||||
if in != nil {
|
if in != nil {
|
||||||
r.SetBody(in)
|
r.SetBody(in)
|
||||||
}
|
}
|
||||||
if out != nil {
|
if out != nil {
|
||||||
r.SetSuccessResult(out)
|
r.SetSuccessResult(out)
|
||||||
}
|
}
|
||||||
resp, err := r.Post(url)
|
resp, err := r.Post(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return resp.Err
|
return resp.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Download(ctx context.Context, url, filepath string) error {
|
func (c *Client) Download(ctx context.Context, url, filepath string) error {
|
||||||
r := c.RWithCtx(ctx)
|
r := c.RWithCtx(ctx)
|
||||||
resp, err := r.Get(url)
|
resp, err := r.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f, err := os.Create(filepath)
|
f, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
_, err = io.Copy(f, resp.Body)
|
_, err = io.Copy(f, resp.Body)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user