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 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 DoubleTicker
- type FibonacciTicker
- type ForeverTicker
- type Get
- type NamedReader
- type Pool
- type PostForm
- type PostJSON
- type PostMultipartForm
- type RandomTicker
- type RetryFunc
- type RetryTicker
- type Session
- func (s *Session) Authorization() string
- 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) 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 Ticker
- type Unwrap
Constants ¶
const UserAgent string = "" /* 129-byte string literal not displayed */
UserAgent 默认 UA 请求头
Variables ¶
var DefaultSession = &Session{ Header: http.Header{ "User-Agent": {UserAgent}, }, }
DefaultSession 默认会话
var ErrDuration = errors.New("req: time.Duration of ForeverTicker must be positive")
ErrDuration 重试间隔时间非正
var ErrInvalidKeyPrefix = errors.New("req: key must start with '$'")
ErrInvalidKeyPrefix 无效的自定义变量名
var ErrPoolClosed = errors.New("req: pool has been closed")
ErrPoolClosed 请求池已经关闭
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 FibonacciTicker ¶
FibonacciTicker 斐波那契计时器,前两项为第一次、第二次重试间隔时间 之后按照斐波那契规则返回新间隔时间,重试间隔时间超过第三项时终止
type ForeverTicker ¶
ForeverTicker 永久计时器,每次都返回当前值的重试间隔
type NamedReader ¶
type NamedReader struct {
// contains filtered or unexported fields
}
NamedReader 命名读取器
func NewNamedReader ¶
func NewNamedReader(name string, data []byte) *NamedReader
NewNamedReader 新建命名读取器
func (*NamedReader) Name ¶
func (n *NamedReader) Name() string
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 RandomTicker ¶
RandomTicker 随机计时器,返回给入重试间隔之间的随机值
type RetryTicker ¶
type RetryTicker interface {
// NextRetry 用于计算下次重试前需要等待的时间,入参为已重试次数,返回值为等待时间以及是否继续重试
NextRetry(retried int) (delay time.Duration, ok bool)
}
RetryTicker 重试计时器
var DefaultRetryTicker RetryTicker = DoubleTicker(2)
DefaultRetryTicker 默认计时器,“试”不过三
func ZeroTicker ¶
func ZeroTicker(maxRetries int, whySafe string) RetryTicker
ZeroTicker 零间隔重试器,你应该知道自己在做什么、为什么这么做、为什么能这样做
var _ RetryTicker = ZeroTicker(2, "trust me!")
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