Documentation
¶
Index ¶
- Constants
- Variables
- func Content(api API) ([]byte, error)
- func ContentWithContext(ctx context.Context, api API) ([]byte, error)
- func Do(api API) (*http.Response, error)
- func DoWithContext(ctx context.Context, api API) (*http.Response, error)
- func GetCookieJar(v any, u *url.URL) http.CookieJar
- func JSON(api API) (any, error)
- func JSONWithContext(ctx context.Context, api API) (any, error)
- func MustParseURL(rawURL string) *url.URL
- func NewRequest(api API) (*http.Request, error)
- func NewRequestWithContext(ctx context.Context, api API) (*http.Request, error)
- func Result[T any](api API) (result T, err error)
- func ResultWithContext[T any](ctx context.Context, api API) (result T, err error)
- func Text(api API) (string, error)
- func TextWithContext(ctx context.Context, api API) (string, error)
- func WithDelay(delayer Delayer) func(yield func(times int, delay time.Duration) bool)
- func WithMap(parent context.Context, m map[string]any) context.Context
- func WithValues(parent context.Context, values ...context.Context) context.Context
- func Write(api API, name string, perm os.FileMode) error
- func WriteWithContext(ctx context.Context, api API, name string, perm os.FileMode) error
- type API
- type APIBody
- type APICookie
- type APICustom
- type APIHeader
- type APIQuery
- type APIXSRF
- type BeforeRequest
- type CheckResponse
- type DelayFunc
- type DelayTicker
- type Delayer
- type DoubleDelayer
- type FibonacciDelayer
- type ForeverDelayer
- type Get
- type Pool
- type PostForm
- type PostJSON
- type PostMultipartForm
- type RandomDelayer
- type Session
- func (s *Session) Authorization() string
- func (s *Session) Clone() *Session
- func (s *Session) Content(api API) ([]byte, error)
- func (s *Session) ContentWithContext(ctx context.Context, api API) ([]byte, error)
- func (s *Session) CreateRequest(ctx context.Context, api API, task method.Task, value reflect.Value) (req *http.Request, err error)
- func (s *Session) Do(api API) (*http.Response, error)
- func (s *Session) DoWithContext(ctx context.Context, api API) (resp *http.Response, err error)
- func (s *Session) JSON(api API) (data any, err error)
- func (s *Session) JSONWithContext(ctx context.Context, api API) (data any, err error)
- func (s *Session) NewRequest(api API) (req *http.Request, err error)
- func (s *Session) NewRequestWithContext(ctx context.Context, api API) (req *http.Request, err error)
- func (s *Session) Result(api API, result any) (err error)
- func (s *Session) ResultWithContext(ctx context.Context, api API, result any) (err error)
- func (s *Session) Set(key string, value any) error
- func (s *Session) SetAuthorization(auth string)
- func (s *Session) SetBaseURL(rawURL string) *Session
- func (s *Session) SetHeader(m map[string]string) *Session
- func (s *Session) SetUserAgent(val string)
- func (s *Session) Text(api API) (string, error)
- func (s *Session) TextWithContext(ctx context.Context, api API) (string, error)
- func (s *Session) URL(rawURL string) string
- func (s *Session) UserAgent() string
- func (s *Session) Value(key string) any
- func (s *Session) Write(api API, name string, perm os.FileMode) error
- func (s *Session) WriteWithContext(ctx context.Context, api API, name string, perm os.FileMode) error
- type SetOnlyCookieJar
- type Unwrap
Constants ¶
const EOD time.Duration = -1
EOD 延迟器结束符 End Of Delayer
const UserAgent string = "" /* 129-byte string literal not displayed */
UserAgent 默认 UA 请求头
Variables ¶
var DefaultSession = &Session{ Header: http.Header{ "User-Agent": {UserAgent}, }, }
DefaultSession 默认会话
var ErrInvalidKeyPrefix = errors.New("req: key must start with '$'")
ErrInvalidKeyPrefix 无效的自定义变量名
var ErrPoolClosed = errors.New("req: pool has been closed")
ErrPoolClosed 请求池已经关闭
var WithDefaultDelay = WithDelay(DefaultDelayer)
WithDefaultDelay 迭代一个默认延迟器
Functions ¶
func ContentWithContext ¶
ContentWithContext 携带上下文获取请求结果
func DoWithContext ¶
DoWithContext 携带上下文发送请求
func GetCookieJar ¶
GetCookieJar 从对象中获取可用的 http.CookieJar ,如果仅在结构体中嵌入了字段,但值不可用,仍返回空
func JSONWithContext ¶
JSONWithContext 携带上下文将请求结果以 JSON 格式反序列化进接口
func NewRequestWithContext ¶
NewRequestWithContext 携带上下文新建请求
func ResultWithContext ¶
ResultWithContext 携带上下文将请求结果以 JSON 格式反序列化进对象
func TextWithContext ¶
TextWithContext 携带上下文获取请求结果字符串
func WithMap ¶
WithMap 创建一个新上下文,用于存储和获取额外的上下文值
parent 为新上下文的父上下文,不能为空
m 为要附加的键值对映射,会被复制到新的上下文中,后续对 m 的修改不会影响上下文中的值
Value 首先尝试从父上下文的 Value 方法获取值,如果未找到,则从当前 mapCtx 的映射中查找键对应的值
func WithValues ¶
WithValues 创建一个新的上下文,用于聚合多个上下文的值
parent 为新上下文的父上下文,不能为空
values 为要附加的子上下文列表
Value 首先从父上下文中查找键对应的值,如果未找到,则依次从子上下文中查找,返回第一个找到的值
Types ¶
type BeforeRequest ¶
BeforeRequest 请求前钩子
type CheckResponse ¶
type CheckResponse interface {
CheckResponse(cli *http.Client, resp *http.Response, api API) error
}
CheckResponse 检验响应,出现错误时必须自行调用 resp.Body.Close()
type DelayTicker ¶
DelayTicker 到达延迟延迟器返回的下次延迟时长时,会发送当前时间到通道。当延迟器不再延迟时,会自动关闭通道
func NewDelayTicker ¶
func NewDelayTicker(delayer Delayer) *DelayTicker
NewDelayTicker 创建并立即启动 Ticker
type Delayer ¶
type Delayer interface {
// NextDelay 计算下次延迟前需要等待的时长,入参为已延迟次数,返回值为等待时长以及是否继续延迟
NextDelay(times int) (delay time.Duration, ok bool)
}
Delayer 延迟器
var DefaultDelayer Delayer = DoubleDelayer(2)
DefaultDelayer 默认延迟器,“试”不过三
func ZeroDelayer ¶
ZeroDelayer 零间隔延迟器,你应该知道自己在做什么、为什么这么做、为什么能这样做
var _ RetryDelayer = ZeroDelayer(2, "trust me!")
type FibonacciDelayer ¶
FibonacciDelayer 斐波那契延迟器,前两项为第一次、第二次延迟时长 之后按照斐波那契规则返回新延迟时长,延迟时长超过第三项时终止
type ForeverDelayer ¶
ForeverDelayer 永久延迟器,每次都返回当前值的延迟间隔
type Pool ¶
type Pool struct {
// 执行请求的最大并行数
Machine int
// 请求池缓冲大小
Size int
// 请求超时
Timeout time.Duration
// 发送请求的会话
Session *Session
// contains filtered or unexported fields
}
Pool 请求池
var DefaultPool *Pool
DefaultPool 默认请求池
func (*Pool) NewTaskWithContext ¶
NewTaskWithContext 携带上下文用请求创建任务
func (*Pool) RunWithContext ¶
RunWithContext 携带上下文启动请求池
type PostMultipartForm ¶
type PostMultipartForm = method.PostMultipartForm
type RandomDelayer ¶
RandomDelayer 随机延迟器,返回给入延迟间隔之间的随机值
type Session ¶
type Session struct {
http.Client
// 基础路径,若 API 路径以 "/" 开头则会拼接在此路径后
BaseURL *url.URL
// 默认请求头,会自动为每个请求添加
Header http.Header
// 自定义变量,当字段标签 default 中的值以 "$" 开头则会尝试在该字典中查找对应值
Variables map[string]any
}
Session 会话
func (*Session) Authorization ¶
Authorization 获取已设置的默认 Authorization 请求头
func (*Session) ContentWithContext ¶
ContentWithContext 携带上下文获取请求结果
func (*Session) CreateRequest ¶
func (s *Session) CreateRequest(ctx context.Context, api API, task method.Task, value reflect.Value) (req *http.Request, err error)
CreateRequest 创建新请求
func (*Session) DoWithContext ¶
DoWithContext 携带上下文发送请求
func (*Session) JSONWithContext ¶
JSONWithContext 携带上下文将请求结果以 JSON 格式反序列化进接口
func (*Session) NewRequest ¶
NewRequest 新建请求
func (*Session) NewRequestWithContext ¶
func (s *Session) NewRequestWithContext(ctx context.Context, api API) (req *http.Request, err error)
NewRequestWithContext 携带上下文新建请求
func (*Session) ResultWithContext ¶
ResultWithContext 携带上下文将请求结果以 JSON 格式反序列化进对象,该对象必须是指针
func (*Session) SetAuthorization ¶
SetAuthorization 设置默认 Authorization 请求头
func (*Session) SetUserAgent ¶
SetUserAgent 设置默认 User-Agent 请求头
func (*Session) TextWithContext ¶
TextWithContext 携带上下文获取请求结果字符串
type SetOnlyCookieJar ¶
SetOnlyCookieJar 是一个不返回 Cookies 的 http.CookieJar ,仅可用于 SetCookies