Documentation
¶
Overview ¶
Package httpx 提供各引擎共用的 HTTP Client 默认值(超时等)。
Index ¶
- Constants
- func DoJSON(ctx context.Context, hc *http.Client, method, url, apiKey string, body []byte, ...) ([]byte, error)
- func NewRateLimitedClient(rps float64, burst int, timeout time.Duration) *http.Client
- func NewRetryClient(maxRetries int, timeout time.Duration) *http.Client
- func OrDefault(c *http.Client, defaultTimeout time.Duration) *http.Client
- func UploadFile(ctx context.Context, hc *http.Client, url, apiKey, fieldName, filePath string, ...) ([]byte, error)
- func UploadReader(ctx context.Context, hc *http.Client, url, apiKey, fieldName, fileName string, ...) ([]byte, error)
- type RateLimitTransport
- type RetryTransport
Constants ¶
const DefaultTimeout = 3 * time.Minute
DefaultTimeout 为未显式配置 Timeout 时的默认请求超时(含连接+TLS+首包+整体)。
Variables ¶
This section is empty.
Functions ¶
func DoJSON ¶ added in v0.9.0
func DoJSON(ctx context.Context, hc *http.Client, method, url, apiKey string, body []byte, prefix string) ([]byte, error)
DoJSON sends a JSON request and returns the response body. It sets Authorization (Bearer) and Content-Type headers automatically. Non-2xx responses are converted to *aigoerr.Error.
func NewRateLimitedClient ¶ added in v0.13.0
NewRateLimitedClient creates an *http.Client with a token-bucket rate limiter. rps is requests per second; burst is the maximum burst size.
func NewRetryClient ¶ added in v0.13.0
NewRetryClient creates an *http.Client with automatic retry for 429/5xx.
func OrDefault ¶
OrDefault 返回可用的 *http.Client:nil 或 Timeout==0 时使用 defaultTimeout。 若 c 已有 Timeout>0,原样返回 c。
func UploadFile ¶ added in v0.13.0
func UploadFile(ctx context.Context, hc *http.Client, url, apiKey, fieldName, filePath string, extra map[string]string, prefix string) ([]byte, error)
UploadFile uploads a file via multipart/form-data. fieldName is the form field name for the file; extra adds additional string fields.
Types ¶
type RateLimitTransport ¶ added in v0.13.0
type RateLimitTransport struct {
Base http.RoundTripper
Limiter *rate.Limiter
}
RateLimitTransport wraps an http.RoundTripper with a token-bucket rate limiter.
type RetryTransport ¶ added in v0.13.0
type RetryTransport struct {
Base http.RoundTripper
MaxRetries int // default: 3
BaseDelay time.Duration // default: 1s
MaxDelay time.Duration // default: 30s
}
RetryTransport wraps an http.RoundTripper and retries on 429/5xx responses. Only GET requests (and other idempotent methods) are retried by default.