Documentation
¶
Overview ¶
Package httpclient provides a production-ready HTTP client with automatic retries, exponential backoff, circuit breaker, and structured logging via slog.
Use the HTTPClient interface to accept either a real Client or a MockClient in your code, making external HTTP calls easy to test.
Index ¶
- type CircuitBreaker
- type CircuitState
- type Client
- func (c *Client) Delete(ctx context.Context, path string) (*Response, error)
- func (c *Client) Get(ctx context.Context, path string) (*Response, error)
- func (c *Client) Patch(ctx context.Context, path string, body any) (*Response, error)
- func (c *Client) Post(ctx context.Context, path string, body any) (*Response, error)
- func (c *Client) Put(ctx context.Context, path string, body any) (*Response, error)
- func (c *Client) Request() *RequestBuilder
- func (c *Client) SetBearerToken(token string) *Client
- func (c *Client) SetHeader(key, value string) *Client
- func (c *Client) SetHeaders(headers map[string]string) *Client
- type HTTPClient
- type HTTPError
- type MockCall
- type MockClient
- func (mc *MockClient) Delete(ctx context.Context, path string) (*Response, error)
- func (mc *MockClient) Get(ctx context.Context, path string) (*Response, error)
- func (mc *MockClient) GetCallCount() int
- func (mc *MockClient) GetCalls() []MockCall
- func (mc *MockClient) OnDelete(path string, statusCode int, body []byte) *MockClient
- func (mc *MockClient) OnError(method, path string, err error) *MockClient
- func (mc *MockClient) OnGet(path string, statusCode int, body []byte) *MockClient
- func (mc *MockClient) OnPatch(path string, statusCode int, body []byte) *MockClient
- func (mc *MockClient) OnPost(path string, statusCode int, body []byte) *MockClient
- func (mc *MockClient) OnPut(path string, statusCode int, body []byte) *MockClient
- func (mc *MockClient) Patch(ctx context.Context, path string, body any) (*Response, error)
- func (mc *MockClient) Post(ctx context.Context, path string, body any) (*Response, error)
- func (mc *MockClient) Put(ctx context.Context, path string, body any) (*Response, error)
- func (mc *MockClient) Reset()
- type Option
- func WithCircuitBreaker(threshold int, timeout time.Duration) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMaxRetries(n int) Option
- func WithMaxRetryDelay(d time.Duration) Option
- func WithRetryDelay(d time.Duration) Option
- func WithTimeout(d time.Duration) Option
- func WithTransport(t http.RoundTripper) Option
- type RequestBuilder
- func (rb *RequestBuilder) BearerToken(token string) *RequestBuilder
- func (rb *RequestBuilder) Body(body any) *RequestBuilder
- func (rb *RequestBuilder) Delete(ctx context.Context) (*Response, error)
- func (rb *RequestBuilder) Get(ctx context.Context) (*Response, error)
- func (rb *RequestBuilder) Header(key, value string) *RequestBuilder
- func (rb *RequestBuilder) Headers(headers map[string]string) *RequestBuilder
- func (rb *RequestBuilder) Method(method string) *RequestBuilder
- func (rb *RequestBuilder) Param(key, value string) *RequestBuilder
- func (rb *RequestBuilder) Params(params map[string]string) *RequestBuilder
- func (rb *RequestBuilder) Path(path string) *RequestBuilder
- func (rb *RequestBuilder) Post(ctx context.Context) (*Response, error)
- func (rb *RequestBuilder) Put(ctx context.Context) (*Response, error)
- func (rb *RequestBuilder) Send(ctx context.Context) (*Response, error)
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern
func NewCircuitBreaker ¶
func NewCircuitBreaker(threshold int, timeout time.Duration) *CircuitBreaker
NewCircuitBreaker creates a new circuit breaker
func (*CircuitBreaker) Call ¶
func (cb *CircuitBreaker) Call(fn func() error) error
Call executes the function if circuit is closed
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() CircuitState
State returns current circuit state
type CircuitState ¶
type CircuitState int
CircuitState represents the state of a circuit breaker
const ( StateClosed CircuitState = iota StateOpen StateHalfOpen )
Circuit breaker states.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client with retries, circuit breaker, and structured logging.
func (*Client) Request ¶
func (c *Client) Request() *RequestBuilder
Request creates a new request builder.
func (*Client) SetBearerToken ¶
SetBearerToken sets Authorization header with Bearer token.
type HTTPClient ¶
type HTTPClient interface {
Get(ctx context.Context, path string) (*Response, error)
Post(ctx context.Context, path string, body any) (*Response, error)
Put(ctx context.Context, path string, body any) (*Response, error)
Patch(ctx context.Context, path string, body any) (*Response, error)
Delete(ctx context.Context, path string) (*Response, error)
}
HTTPClient is the interface implemented by Client and MockClient.
type HTTPError ¶
HTTPError represents an HTTP error response
func (*HTTPError) IsClientError ¶
IsClientError returns true if status is 4xx
func (*HTTPError) IsServerError ¶
IsServerError returns true if status is 5xx
type MockClient ¶
type MockClient struct {
// contains filtered or unexported fields
}
MockClient is a concurrency-safe mock HTTP client for testing.
func (*MockClient) GetCallCount ¶
func (mc *MockClient) GetCallCount() int
GetCallCount returns the number of calls.
func (*MockClient) GetCalls ¶
func (mc *MockClient) GetCalls() []MockCall
GetCalls returns all recorded calls.
func (*MockClient) OnDelete ¶
func (mc *MockClient) OnDelete(path string, statusCode int, body []byte) *MockClient
OnDelete sets mock response for DELETE request.
func (*MockClient) OnError ¶
func (mc *MockClient) OnError(method, path string, err error) *MockClient
OnError sets error for a request.
func (*MockClient) OnGet ¶
func (mc *MockClient) OnGet(path string, statusCode int, body []byte) *MockClient
OnGet sets mock response for GET request.
func (*MockClient) OnPatch ¶
func (mc *MockClient) OnPatch(path string, statusCode int, body []byte) *MockClient
OnPatch sets mock response for PATCH request.
func (*MockClient) OnPost ¶
func (mc *MockClient) OnPost(path string, statusCode int, body []byte) *MockClient
OnPost sets mock response for POST request.
func (*MockClient) OnPut ¶
func (mc *MockClient) OnPut(path string, statusCode int, body []byte) *MockClient
OnPut sets mock response for PUT request.
type Option ¶
type Option func(*Client)
Option configures the Client.
func WithCircuitBreaker ¶
WithCircuitBreaker enables the circuit breaker with the given threshold and timeout.
func WithLogger ¶
WithLogger sets the structured logger.
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retry attempts.
func WithMaxRetryDelay ¶
WithMaxRetryDelay sets the maximum delay between retries.
func WithRetryDelay ¶
WithRetryDelay sets the initial delay between retries.
func WithTimeout ¶
WithTimeout sets the HTTP request timeout.
func WithTransport ¶
func WithTransport(t http.RoundTripper) Option
WithTransport sets the HTTP transport.
type RequestBuilder ¶
type RequestBuilder struct {
// contains filtered or unexported fields
}
RequestBuilder builds HTTP requests with fluent API
func (*RequestBuilder) BearerToken ¶
func (rb *RequestBuilder) BearerToken(token string) *RequestBuilder
BearerToken sets Authorization header
func (*RequestBuilder) Body ¶
func (rb *RequestBuilder) Body(body any) *RequestBuilder
Body sets the request body
func (*RequestBuilder) Delete ¶
func (rb *RequestBuilder) Delete(ctx context.Context) (*Response, error)
Delete is a shorthand for Method("DELETE").Send()
func (*RequestBuilder) Get ¶
func (rb *RequestBuilder) Get(ctx context.Context) (*Response, error)
Get is a shorthand for Method("GET").Send()
func (*RequestBuilder) Header ¶
func (rb *RequestBuilder) Header(key, value string) *RequestBuilder
Header sets a request header
func (*RequestBuilder) Headers ¶
func (rb *RequestBuilder) Headers(headers map[string]string) *RequestBuilder
Headers sets multiple headers
func (*RequestBuilder) Method ¶
func (rb *RequestBuilder) Method(method string) *RequestBuilder
Method sets the HTTP method
func (*RequestBuilder) Param ¶
func (rb *RequestBuilder) Param(key, value string) *RequestBuilder
Param sets a query parameter
func (*RequestBuilder) Params ¶
func (rb *RequestBuilder) Params(params map[string]string) *RequestBuilder
Params sets multiple query parameters
func (*RequestBuilder) Path ¶
func (rb *RequestBuilder) Path(path string) *RequestBuilder
Path sets the request path
func (*RequestBuilder) Post ¶
func (rb *RequestBuilder) Post(ctx context.Context) (*Response, error)
Post is a shorthand for Method("POST").Send()
type Response ¶
type Response struct {
StatusCode int
Status string
Headers http.Header
Body []byte
Duration time.Duration
}
Response represents an HTTP response
func (*Response) IsClientError ¶
IsClientError returns true if status code is 4xx
func (*Response) IsServerError ¶
IsServerError returns true if status code is 5xx