Documentation
¶
Index ¶
- Variables
- func SetBaseURL(baseURL string)
- func SetDefaultClient(client *Client)
- func SetHeader(key, value string)
- func SetHeaders(headers map[string]string)
- func SetTimeout(timeout time.Duration)
- type CircuitBreaker
- type CircuitBreakerConfig
- type Client
- func (c *Client) AddCookie(cookie *http.Cookie)
- func (c *Client) AddInterceptor(interceptor Interceptor)
- func (c *Client) AddMiddleware(middleware Middleware)
- func (c *Client) Delete(url string) (*Response, error)
- func (c *Client) DisableDebug()
- func (c *Client) EnableDebug()
- func (c *Client) Get(url string) (*Response, error)
- func (c *Client) NewRequest(method, url string) *Request
- func (c *Client) Patch(url string, body io.Reader) (*Response, error)
- func (c *Client) PatchJSON(url string, data interface{}) (*Response, error)
- func (c *Client) Post(url string, body io.Reader) (*Response, error)
- func (c *Client) PostJSON(url string, data interface{}) (*Response, error)
- func (c *Client) Put(url string, body io.Reader) (*Response, error)
- func (c *Client) PutJSON(url string, data interface{}) (*Response, error)
- func (c *Client) SetBaseURL(baseURL string)
- func (c *Client) SetDebug(debug *DebugConfig)
- func (c *Client) SetHeader(key, value string)
- func (c *Client) SetHeaders(headers map[string]string)
- func (c *Client) SetTimeout(timeout time.Duration)
- type ClientOptions
- type DebugConfig
- type Interceptor
- type Logger
- type Metrics
- type Middleware
- type PoolConfig
- type RateLimiter
- type Request
- func (r *Request) Body(body io.Reader) *Request
- func (r *Request) Context(ctx context.Context) *Request
- func (r *Request) Cookie(cookie *http.Cookie) *Request
- func (r *Request) Do() (*Response, error)
- func (r *Request) Form(data url.Values) *Request
- func (r *Request) Header(key, value string) *Request
- func (r *Request) Headers(headers map[string]string) *Request
- func (r *Request) JSON(data interface{}) *Request
- func (r *Request) Retries(retries int) *Request
- func (r *Request) Timeout(timeout time.Duration) *Request
- func (r *Request) WithCtx(ctx context.Context) *Request
- type Response
- func Delete(url string) (*Response, error)
- func Get(url string) (*Response, error)
- func Patch(url string, body io.Reader) (*Response, error)
- func PatchJSON(url string, data interface{}) (*Response, error)
- func Post(url string, body io.Reader) (*Response, error)
- func PostJSON(url string, data interface{}) (*Response, error)
- func Put(url string, body io.Reader) (*Response, error)
- func PutJSON(url string, data interface{}) (*Response, error)
- func (r *Response) Bytes() []byte
- func (r *Response) Error() string
- func (r *Response) IsClientError() bool
- func (r *Response) IsError() bool
- func (r *Response) IsInformational() bool
- func (r *Response) IsOK() bool
- func (r *Response) IsRedirect() bool
- func (r *Response) IsServerError() bool
- func (r *Response) IsSuccess() bool
- func (r *Response) JSON(v interface{}) error
- func (r *Response) String() string
- type RetryConfig
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrCircuitHalfOpenLimited = errors.New("circuit breaker half-open request limit reached")
View Source
var ErrCircuitOpen = errors.New("circuit breaker is open")
Functions ¶
func SetBaseURL ¶
func SetBaseURL(baseURL string)
func SetDefaultClient ¶
func SetDefaultClient(client *Client)
func SetHeaders ¶
func SetTimeout ¶
Types ¶
type CircuitBreaker ¶
CircuitBreaker 熔断器接口
type CircuitBreakerConfig ¶
type CircuitBreakerConfig struct {
MaxRequests uint32 // 半开状态最大请求数
Interval time.Duration // 统计时间窗口
Timeout time.Duration // 熔断超时时间
FailureThreshold uint32 // 失败阈值
SuccessThreshold uint32 // 成功阈值
}
CircuitBreakerConfig 熔断器配置
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client HTTP客户端
func GetDefaultClient ¶
func GetDefaultClient() *Client
func NewClientWithOptions ¶
func NewClientWithOptions(opts ClientOptions) *Client
NewClientWithOptions 根据选项创建HTTP客户端
func (*Client) AddInterceptor ¶
func (c *Client) AddInterceptor(interceptor Interceptor)
AddInterceptor 添加拦截器
func (*Client) AddMiddleware ¶
func (c *Client) AddMiddleware(middleware Middleware)
AddMiddleware 添加中间件
func (*Client) NewRequest ¶
NewRequest 创建新的请求构建器
func (*Client) SetHeaders ¶
SetHeaders 批量设置请求头
type ClientOptions ¶
type ClientOptions struct {
Timeout time.Duration // 超时时间
BaseURL string // 基础URL
Headers map[string]string // 默认请求头
UserAgent string // 用户代理
Cookies []*http.Cookie // 默认Cookie
Retry *RetryConfig // 重试配置
CircuitBreaker *CircuitBreakerConfig // 熔断器配置
Pool *PoolConfig // 连接池配置
TLS *tls.Config // TLS配置
Proxy func(*http.Request) (*url.URL, error) // 代理函数
Interceptors []Interceptor // 拦截器
Middlewares []Middleware // 中间件
Logger Logger // 日志记录器
Metrics Metrics // 指标收集器
RateLimiter RateLimiter // 限流器
Debug *DebugConfig // Debug配置
}
ClientOptions HTTP客户端选项
type DebugConfig ¶
type DebugConfig struct {
Enabled bool // 是否启用Debug
LogRequestHeaders bool // 是否记录请求头
LogRequestBody bool // 是否记录请求体
LogResponseHeaders bool // 是否记录响应头
LogResponseBody bool // 是否记录响应体
MaxBodySize int // 最大记录的Body大小(字节),0表示不限制
SensitiveHeaders []string // 敏感请求头列表,将被脱敏
}
DebugConfig Debug配置
type Interceptor ¶
type Interceptor func(req *http.Request, next func(*http.Request) (*http.Response, error)) (*http.Response, error)
Interceptor HTTP拦截器
type Logger ¶
type Logger interface {
Debug(msg string, fields ...interface{})
Info(msg string, fields ...interface{})
Warn(msg string, fields ...interface{})
Error(msg string, fields ...interface{})
}
Logger 日志接口
type Metrics ¶
type Metrics interface {
IncCounter(name string, labels map[string]string)
AddHistogram(name string, value float64, labels map[string]string)
SetGauge(name string, value float64, labels map[string]string)
}
Metrics 指标接口
type Middleware ¶
type Middleware func(next http.RoundTripper) http.RoundTripper
Middleware HTTP中间件函数类型
type PoolConfig ¶
type PoolConfig struct {
MaxIdleConns int // 最大空闲连接数
MaxIdleConnsPerHost int // 每个主机最大空闲连接数
MaxConnsPerHost int // 每个主机最大连接数
IdleConnTimeout time.Duration // 空闲连接超时时间
DisableKeepAlives bool // 禁用keep-alive
DisableCompression bool // 禁用压缩
}
PoolConfig 连接池配置
type RateLimiter ¶
RateLimiter 限流器接口
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request HTTP请求构建器
Click to show internal directories.
Click to hide internal directories.