Documentation
¶
Overview ¶
Package httpclient 提供基于 github.com/sunerpy/requests 的 HTTP 客户端连接池管理
Index ¶
- Constants
- Variables
- func AcquireSession() requests.Session
- func Close() error
- func IsHTTP2Enabled() bool
- func NewDelete(url string) *requests.RequestBuilder
- func NewGet(url string) *requests.RequestBuilder
- func NewPatch(url string) *requests.RequestBuilder
- func NewPost(url string) *requests.RequestBuilder
- func NewPut(url string) *requests.RequestBuilder
- func NewRequestBuilder(method requests.Method, url string) *requests.RequestBuilder
- func ReleaseSession(sess requests.Session)
- func SetHTTP2Enabled(enabled bool)
- type Handler
- type Hooks
- type Method
- type Middleware
- type MiddlewareChain
- type Pool
- type PoolConfig
- type RequestBuilder
- type RequestOption
- type Response
- func Delete(url string, opts ...RequestOption) (Response, error)
- func DeleteWithContext(ctx context.Context, url string, opts ...RequestOption) (Response, error)
- func Get(url string, opts ...RequestOption) (Response, error)
- func GetWithContext(ctx context.Context, url string, opts ...RequestOption) (Response, error)
- func Patch(url string, body any, opts ...RequestOption) (Response, error)
- func PatchWithContext(ctx context.Context, url string, body any, opts ...RequestOption) (Response, error)
- func Post(url string, body any, opts ...RequestOption) (Response, error)
- func PostWithContext(ctx context.Context, url string, body any, opts ...RequestOption) (Response, error)
- func Put(url string, body any, opts ...RequestOption) (Response, error)
- func PutWithContext(ctx context.Context, url string, body any, opts ...RequestOption) (Response, error)
- type Result
- func DeleteJSON[T any](url string, opts ...RequestOption) (Result[T], error)
- func GetJSON[T any](url string, opts ...RequestOption) (Result[T], error)
- func PatchJSON[T any](url string, data any, opts ...RequestOption) (Result[T], error)
- func PostJSON[T any](url string, data any, opts ...RequestOption) (Result[T], error)
- func PutJSON[T any](url string, data any, opts ...RequestOption) (Result[T], error)
- type RetryExecutor
Constants ¶
const ( MethodGet = requests.MethodGet MethodPost = requests.MethodPost MethodPut = requests.MethodPut MethodDelete = requests.MethodDelete MethodPatch = requests.MethodPatch MethodHead = requests.MethodHead MethodOptions = requests.MethodOptions )
HTTP 方法常量
Variables ¶
var ( WithTimeout = requests.WithTimeout WithHeader = requests.WithHeader WithHeaders = requests.WithHeaders WithQuery = requests.WithQuery WithQueryParams = requests.WithQueryParams WithBasicAuth = requests.WithBasicAuth WithBearerToken = requests.WithBearerToken WithContext = requests.WithContext WithContentType = requests.WithContentType WithAccept = requests.WithAccept )
导出常用请求选项
var ( NewMiddlewareChain = requests.NewMiddlewareChain NewHooks = requests.NewHooks )
导出中间件和钩子构造函数
var ( NoRetryPolicy = requests.NoRetryPolicy LinearRetryPolicy = requests.LinearRetryPolicy ExponentialRetryPolicy = requests.ExponentialRetryPolicy RetryOn5xx = requests.RetryOn5xx RetryOnNetworkError = requests.RetryOnNetworkError RetryOnStatusCodes = requests.RetryOnStatusCodes CombineRetryConditions = requests.CombineRetryConditions )
导出重试策略函数
var ( IsTimeout = requests.IsTimeout IsConnectionError = requests.IsConnectionError IsResponseError = requests.IsResponseError IsTemporary = requests.IsTemporary )
导出错误检查函数
Functions ¶
func AcquireSession ¶
AcquireSession 从对象池获取 Session(高性能场景) 使用完毕后需调用 ReleaseSession
func NewRequestBuilder ¶
func NewRequestBuilder(method requests.Method, url string) *requests.RequestBuilder
NewRequestBuilder 创建请求构建器
Types ¶
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool HTTP 客户端连接池,基于 requests 库
type PoolConfig ¶
type PoolConfig struct {
// MaxIdleConns 最大空闲连接数
MaxIdleConns int
// MaxIdleConnsPerHost 每个主机的最大空闲连接数
MaxIdleConnsPerHost int
// IdleTimeout 空闲连接超时时间
IdleTimeout time.Duration
// Timeout 请求超时时间
Timeout time.Duration
// ConnectTimeout 连接超时时间
ConnectTimeout time.Duration
// EnableKeepAlive 是否启用 Keep-Alive
EnableKeepAlive bool
// EnableHTTP2 是否启用 HTTP/2
EnableHTTP2 bool
// MaxRetries 最大重试次数
MaxRetries int
// RetryDelay 初始重试延迟
RetryDelay time.Duration
// MaxRetryDelay 最大重试延迟
MaxRetryDelay time.Duration
}
PoolConfig 连接池配置
type Response ¶
type Response interface {
StatusCode() int
Bytes() []byte
Text() string
IsSuccess() bool
IsError() bool
}
Response 响应接口 - 封装 requests 库的响应
func DeleteWithContext ¶
DeleteWithContext 发送带上下文的 DELETE 请求
func GetWithContext ¶
GetWithContext 发送带上下文的 GET 请求
func Patch ¶
func Patch(url string, body any, opts ...RequestOption) (Response, error)
Patch 发送 PATCH 请求
func PatchWithContext ¶
func PatchWithContext(ctx context.Context, url string, body any, opts ...RequestOption) (Response, error)
PatchWithContext 发送带上下文的 PATCH 请求
func Post ¶
func Post(url string, body any, opts ...RequestOption) (Response, error)
Post 发送 POST 请求
func PostWithContext ¶
func PostWithContext(ctx context.Context, url string, body any, opts ...RequestOption) (Response, error)
PostWithContext 发送带上下文的 POST 请求
func PutWithContext ¶
func PutWithContext(ctx context.Context, url string, body any, opts ...RequestOption) (Response, error)
PutWithContext 发送带上下文的 PUT 请求
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result 泛型结果类型
func DeleteJSON ¶
func DeleteJSON[T any](url string, opts ...RequestOption) (Result[T], error)
DeleteJSON 发送 DELETE 请求并解析 JSON 响应
func GetJSON ¶
func GetJSON[T any](url string, opts ...RequestOption) (Result[T], error)
GetJSON 发送 GET 请求并解析 JSON 响应
type RetryExecutor ¶
type RetryExecutor struct {
// contains filtered or unexported fields
}
RetryExecutor 重试执行器
func NewRetryExecutor ¶
func NewRetryExecutor(maxRetries int, retryDelay, maxRetryDelay time.Duration) *RetryExecutor
NewRetryExecutor 创建重试执行器
func NewRetryExecutorFromConfig ¶
func NewRetryExecutorFromConfig(config PoolConfig) *RetryExecutor
NewRetryExecutorFromConfig 从配置创建重试执行器
func (*RetryExecutor) GetAttemptCount ¶
func (r *RetryExecutor) GetAttemptCount() int
GetAttemptCount 获取最大尝试次数(初始 + 重试)
func (*RetryExecutor) GetMaxRetries ¶
func (r *RetryExecutor) GetMaxRetries() int
GetMaxRetries 获取最大重试次数