Documentation
¶
Overview ¶
Package httpclient is a context-aware JSON HTTP client with retries.
Index ¶
- Constants
- Variables
- type Error
- type FileField
- type HTTP
- func (h *HTTP) Delete(ctx context.Context, path string, out any) (int, error)
- func (h *HTTP) FireAndForget(ctx context.Context, method, path string, body any)
- func (h *HTTP) Get(ctx context.Context, path string, out any) (int, error)
- func (h *HTTP) Patch(ctx context.Context, path string, body, out any) (int, error)
- func (h *HTTP) Post(ctx context.Context, path string, body, out any) (int, error)
- func (h *HTTP) PostForm(ctx context.Context, path string, values url.Values, out any) (int, error)
- func (h *HTTP) PostMultipart(ctx context.Context, path string, fields map[string]string, files []FileField, ...) (int, error)
- func (h *HTTP) Put(ctx context.Context, path string, body, out any) (int, error)
- func (h *HTTP) SetHeader(key, value string)
- type Option
- func WithCircuitBreaker(maxFailures int, openTimeout time.Duration) Option
- func WithClient(c *fasthttp.Client) Option
- func WithContextHeader(header string, extract func(context.Context) string) Option
- func WithHeader(key, value string) Option
- func WithLogger(l *logger.Logger) Option
- func WithRetry(n int, backoff time.Duration) Option
- func WithTimeout(d time.Duration) Option
Constants ¶
const ( Post = fasthttp.MethodPost Get = fasthttp.MethodGet Put = fasthttp.MethodPut Patch = fasthttp.MethodPatch Delete = fasthttp.MethodDelete Options = fasthttp.MethodOptions Head = fasthttp.MethodHead Connect = fasthttp.MethodConnect Trace = fasthttp.MethodTrace )
HTTP method constants re-exported from fasthttp for use with HTTP.FireAndForget and similar helpers.
Variables ¶
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 FileField ¶
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 ¶
type HTTP struct {
BaseURL string
Client *fasthttp.Client
// contains filtered or unexported fields
}
HTTP is a small JSON HTTP client built on fasthttp.
func (*HTTP) FireAndForget ¶
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) Patch ¶
Patch sends a PATCH request with a JSON-encoded body and decodes the response into out.
func (*HTTP) Post ¶
Post sends a POST request with a JSON-encoded body and decodes the response into out.
func (*HTTP) PostForm ¶
PostForm sends an application/x-www-form-urlencoded POST and JSON-decodes the response into out.
func (*HTTP) PostMultipart ¶
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.
type Option ¶
type Option func(*HTTP)
Option configures an HTTP client.
func WithCircuitBreaker ¶
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 ¶
WithClient replaces the underlying fasthttp client.
func WithContextHeader ¶
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 ¶
WithHeader adds a default header sent with every request.
func WithLogger ¶
WithLogger attaches a logger used to report FireAndForget errors.
func WithRetry ¶
WithRetry configures the number of additional retry attempts and the backoff duration between them. Only 5xx errors and transport failures are retried.
func WithTimeout ¶
WithTimeout sets the per-request timeout used when no context deadline is present.