http

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package http is a context-aware JSON HTTP client with retries.

Index

Constants

HTTP method constants re-exported from fasthttp for use with HTTP.FireAndForget and similar helpers.

Variables

View Source
var ErrCircuitOpen = errors.New("http: circuit breaker open")

ErrCircuitOpen is returned by request methods when the circuit breaker is open and the request is rejected without being sent.

Functions

This section is empty.

Types

type Error added in v0.6.0

type Error struct {
	Code int
	Body []byte
}

Error is returned when the server responds with a non-2xx status code.

func (*Error) Error added in v0.6.0

func (e *Error) Error() string

type FileField added in v0.6.0

type FileField struct {
	Field    string    // form field name
	Filename string    // file name reported to the server
	Content  io.Reader // file contents
}

FileField is one file part of a multipart request.

type HTTP added in v0.6.0

type HTTP struct {
	BaseURL string
	Client  *fasthttp.Client
	// contains filtered or unexported fields
}

HTTP is a small JSON HTTP client built on fasthttp.

func New

func New(baseURL string, opts ...Option) *HTTP

New creates a new HTTP client.

func (*HTTP) Delete added in v0.6.0

func (h *HTTP) Delete(ctx context.Context, path string, out any) (int, error)

Delete sends a DELETE request to path and JSON-decodes the response into out.

func (*HTTP) FireAndForget added in v0.6.0

func (h *HTTP) FireAndForget(ctx context.Context, method, path string, body any)

FireAndForget sends a request in the background, logging any error if a logger was configured via WithLogger. The caller's context values are kept but its cancellation/deadline are dropped so the request outlives the caller.

func (*HTTP) Get added in v0.6.0

func (h *HTTP) Get(ctx context.Context, path string, out any) (int, error)

Get sends a GET request to path and JSON-decodes the response into out.

func (*HTTP) Patch added in v0.6.0

func (h *HTTP) Patch(ctx context.Context, path string, body, out any) (int, error)

Patch sends a PATCH request with a JSON-encoded body and decodes the response into out.

func (*HTTP) Post added in v0.6.0

func (h *HTTP) Post(ctx context.Context, path string, body, out any) (int, error)

Post sends a POST request with a JSON-encoded body and decodes the response into out.

func (*HTTP) PostForm added in v0.6.0

func (h *HTTP) PostForm(ctx context.Context, path string, values url.Values, out any) (int, error)

PostForm sends an application/x-www-form-urlencoded POST and JSON-decodes the response into out.

func (*HTTP) PostMultipart added in v0.6.0

func (h *HTTP) PostMultipart(ctx context.Context, path string, fields map[string]string, files []FileField, out any) (int, error)

PostMultipart sends a multipart/form-data POST with the given fields and files and JSON-decodes the response into out.

func (*HTTP) Put added in v0.6.0

func (h *HTTP) Put(ctx context.Context, path string, body, out any) (int, error)

Put sends a PUT request with a JSON-encoded body and decodes the response into out.

func (*HTTP) SetHeader added in v0.6.0

func (h *HTTP) SetHeader(key, value string)

SetHeader sets a default header sent with every request.

type Option

type Option func(*HTTP)

Option configures an HTTP client.

func WithCircuitBreaker added in v1.2.0

func WithCircuitBreaker(maxFailures int, openTimeout time.Duration) Option

WithCircuitBreaker enables a circuit breaker: after maxFailures consecutive failures (transport errors or 5xx responses) the client rejects requests with ErrCircuitOpen until openTimeout elapses, then allows a single probe.

func WithClient

func WithClient(c *fasthttp.Client) Option

WithClient replaces the underlying fasthttp client.

func WithContextHeader added in v1.4.0

func WithContextHeader(header string, extract func(context.Context) string) Option

WithContextHeader sets a per-request header from the call's context: before each request, extract is called with the context and, when it returns a non-empty string, that value is sent as header. Useful for propagating a request id, trace correlation, or tenant downstream.

func WithHeader

func WithHeader(key, value string) Option

WithHeader adds a default header sent with every request.

func WithLogger

func WithLogger(l *logger.Logger) Option

WithLogger attaches a logger used to report FireAndForget errors.

func WithRetry

func WithRetry(n int, backoff time.Duration) Option

WithRetry configures the number of additional retry attempts and the backoff duration between them. Only 5xx errors and transport failures are retried.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the per-request timeout used when no context deadline is present.

Jump to

Keyboard shortcuts

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