httpx

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package httpx 提供各引擎共用的 HTTP Client 默认值(超时等)。

Index

Constants

View Source
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

func NewRateLimitedClient(rps float64, burst int, timeout time.Duration) *http.Client

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

func NewRetryClient(maxRetries int, timeout time.Duration) *http.Client

NewRetryClient creates an *http.Client with automatic retry for 429/5xx.

func OrDefault

func OrDefault(c *http.Client, defaultTimeout time.Duration) *http.Client

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.

func UploadReader added in v0.13.0

func UploadReader(ctx context.Context, hc *http.Client, url, apiKey, fieldName, fileName string, r io.Reader, extra map[string]string, prefix string) ([]byte, error)

UploadReader uploads data from an io.Reader via multipart/form-data.

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.

func (*RateLimitTransport) RoundTrip added in v0.13.0

func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper. It waits for rate limit clearance before forwarding the request to the base transport.

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.

func (*RetryTransport) RoundTrip added in v0.13.0

func (t *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper with automatic retry logic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL