httpx

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedMessageType = errors.New("unsupported message type")
)

Functions

func Get

func Get(ctx context.Context, url string, opts ...Option) (*http.Response, error)

func GetJSON

func GetJSON[T any](ctx context.Context, url string, opts ...Option) (*T, error)

func GetJSONWithConfig

func GetJSONWithConfig[T any](ctx context.Context, conf *Config, url string, opts ...Option) (*T, error)

func GetWithConfig

func GetWithConfig(ctx context.Context, conf *Config, url string, opts ...Option) (*http.Response, error)

func NewErrorStatusCode

func NewErrorStatusCode(code int, message string) error

func NewErrorStatusCode429

func NewErrorStatusCode429(message string, retryAfter int) error

func Post

func Post(ctx context.Context, url string, body io.Reader, opts ...Option) (*http.Response, error)

func PostJson

func PostJson[T any](ctx context.Context, url string, body any, opts ...Option) (*T, error)

func PostJsonSse

func PostJsonSse[T any](ctx context.Context, url string, body any, opts ...Option) (chan *JsonSseEvent[T], error)

func PostJsonSseWithConfig

func PostJsonSseWithConfig[T any](ctx context.Context, conf *Config, url string, body any, opts ...Option) (chan *JsonSseEvent[T], error)

func PostJsonWithConfig

func PostJsonWithConfig[T any](ctx context.Context, conf *Config, url string, body any, opts ...Option) (*T, error)

func PostWithConfig

func PostWithConfig(ctx context.Context, conf *Config, url string, body io.Reader, opts ...Option) (*http.Response, error)

Types

type Config

type Config struct {
	Client  *http.Client
	BaseUrl string
	Header  http.Header
	Retry   *Retry
}

func NewConfig

func NewConfig(opts ...Option) *Config

func (*Config) Clone

func (c *Config) Clone() *Config

func (*Config) Request

func (c *Config) Request(ctx context.Context, method, url string, body io.Reader, opts ...Option) (*http.Response, error)

type ErrorStatusCode

type ErrorStatusCode struct {
	Code       int
	Message    string
	RetryAfter int
}

func (*ErrorStatusCode) Error

func (e *ErrorStatusCode) Error() string

func (*ErrorStatusCode) RetryAfterDuration

func (e *ErrorStatusCode) RetryAfterDuration() time.Duration

func (*ErrorStatusCode) StatusCode

func (e *ErrorStatusCode) StatusCode() int

type JsonSseEvent

type JsonSseEvent[T any] struct {
	Err  error
	Data *T
}

type Option

type Option func(*Config)

func WithBaseUrl

func WithBaseUrl(url string) Option

func WithClient

func WithClient(client *http.Client) Option

func WithHeader

func WithHeader(k, v string) Option

func WithRetry

func WithRetry(retryInitInterval, retryMaxInterval, retryMax time.Duration, multiplier float64) Option

type Retry

type Retry struct {
	InitInterval time.Duration
	MaxInterval  time.Duration
	Multiplier   float64
	Max          time.Duration
}

type SseEvent

type SseEvent struct {
	ID    string
	Event string
	Data  string
	Retry time.Duration
}

type SseResponse

type SseResponse struct {
	// contains filtered or unexported fields
}

func NewSseResponse

func NewSseResponse(r io.Reader) *SseResponse

func (*SseResponse) Recv

func (s *SseResponse) Recv() (*SseEvent, error)

Recv 读取并返回下一个解析好的泛型对象

type WSClient

type WSClient struct {
	// contains filtered or unexported fields
}

func WSConnect

func WSConnect(ctx context.Context, url string, options WSClientOptions) (*WSClient, error)

func (*WSClient) Close

func (w *WSClient) Close()

func (*WSClient) Ping

func (w *WSClient) Ping(ctx context.Context)

func (*WSClient) Recv

func (w *WSClient) Recv(ctx context.Context) (WSMessageType, []byte, error)

func (*WSClient) Send

func (w *WSClient) Send(ctx context.Context, messageType WSMessageType, data []byte) error

func (*WSClient) SendJsonMessage

func (w *WSClient) SendJsonMessage(ctx context.Context, msg any) error

func (*WSClient) SetReadLimit

func (w *WSClient) SetReadLimit(n int64)

type WSClientOptions

type WSClientOptions struct {
	Header     http.Header
	HTTPClient *http.Client
}

type WSEventHandler

type WSEventHandler func(ctx context.Context, messageType WSMessageType, data []byte) error

type WSHandler

type WSHandler func(messageType WSMessageType, data []byte)

type WSMessageType

type WSMessageType int
const (
	WSMessageUnknown WSMessageType = iota + 1
	WSMessageText
	WSMessageBinary
)

Jump to

Keyboard shortcuts

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