Documentation
¶
Overview ¶
Package httpclient provides an HTTP/REST client with retry, circuit breaker, rate limiting, and comprehensive middleware support for CQI infrastructure. It wraps the resty library with CQI patterns for observability and error handling.
Example usage:
cfg := config.HTTPClientConfig{
BaseURL: "https://api.example.com",
Timeout: 30 * time.Second,
RetryCount: 3,
}
client, err := httpclient.New(ctx, cfg)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Make a GET request
resp, err := client.Get(ctx, "/users/123")
if err != nil {
log.Fatal(err)
}
// Make a POST request with JSON body
user := map[string]string{"name": "John"}
resp, err = client.Post(ctx, "/users").WithJSON(user).Do()
// Use with protobuf
var userProto pb.User
resp, err = client.Get(ctx, "/users/123").IntoProto(&userProto).Do()
Index ¶
- type Client
- func (c *Client) Close() error
- func (c *Client) Delete(ctx context.Context, url string) *Request
- func (c *Client) Get(ctx context.Context, url string) *Request
- func (c *Client) Head(ctx context.Context, url string) *Request
- func (c *Client) NewRequest(ctx context.Context) *Request
- func (c *Client) Options(ctx context.Context, url string) *Request
- func (c *Client) Patch(ctx context.Context, url string) *Request
- func (c *Client) Post(ctx context.Context, url string) *Request
- func (c *Client) Put(ctx context.Context, url string) *Request
- func (c *Client) WithAuthToken(token string) *Client
- func (c *Client) WithBasicAuth(username, password string) *Client
- func (c *Client) WithDefaultHeader(key, value string) *Client
- func (c *Client) WithDefaultHeaders(headers map[string]string) *Client
- func (c *Client) WithLogging(logger *zerolog.Logger) *Client
- func (c *Client) WithMetrics(namespace string) *Client
- func (c *Client) WithTracing(serviceName string) *Client
- type Request
- func (r *Request) Do() (*Response, error)
- func (r *Request) IntoJSON(result interface{}) *Request
- func (r *Request) IntoProto(msg proto.Message) *Request
- func (r *Request) SetMethod(method string) *Request
- func (r *Request) SetURL(url string) *Request
- func (r *Request) WithAuthToken(token string) *Request
- func (r *Request) WithBasicAuth(username, password string) *Request
- func (r *Request) WithBody(body []byte) *Request
- func (r *Request) WithFormData(data map[string]string) *Request
- func (r *Request) WithHeader(key, value string) *Request
- func (r *Request) WithHeaders(headers map[string]string) *Request
- func (r *Request) WithJSON(body interface{}) *Request
- func (r *Request) WithProto(msg proto.Message) *Request
- func (r *Request) WithQuery(key, value string) *Request
- func (r *Request) WithQueryParams(params map[string]string) *Request
- func (r *Request) WithTimeout(timeout time.Duration) *Request
- type Response
- func (r *Response) Body() []byte
- func (r *Response) BodyAsJSON(dest interface{}) error
- func (r *Response) BodyAsProto(msg proto.Message) error
- func (r *Response) BodyAsString() string
- func (r *Response) Error() error
- func (r *Response) Header(key string) string
- func (r *Response) Headers() http.Header
- func (r *Response) IsError() bool
- func (r *Response) IsSuccess() bool
- func (r *Response) Status() string
- func (r *Response) StatusCode() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides HTTP/REST client functionality with retry, circuit breaker, rate limiting, and middleware support.
func New ¶
New creates a new HTTP client with the provided configuration. It initializes connection pooling, retry logic, circuit breaker (if enabled), and rate limiting (if configured).
func (*Client) Close ¶
Close releases all resources associated with the client. It cancels the client context and closes the circuit breaker.
func (*Client) Get ¶
Get creates a new GET request for the specified URL. The URL can be relative (appended to BaseURL) or absolute.
func (*Client) NewRequest ¶
NewRequest creates a new request with the client's configuration.
func (*Client) WithAuthToken ¶
WithAuthToken adds Bearer token authentication to all requests.
func (*Client) WithBasicAuth ¶
WithBasicAuth adds basic authentication to all requests.
func (*Client) WithDefaultHeader ¶
WithDefaultHeader adds a default header to all requests.
func (*Client) WithDefaultHeaders ¶
WithDefaultHeaders adds multiple default headers to all requests.
func (*Client) WithLogging ¶
WithLogging adds logging middleware to the HTTP client. It logs request start, response, duration, and status codes.
func (*Client) WithMetrics ¶
WithMetrics adds metrics middleware to the HTTP client. It records request count and duration for all HTTP requests.
func (*Client) WithTracing ¶
WithTracing adds distributed tracing middleware to the HTTP client. It creates spans for each request with appropriate attributes.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request represents an HTTP request with a fluent builder API. It wraps resty.Request with CQI-specific features like protobuf support and error mapping.
func (*Request) Do ¶
Do executes the request and returns the response. It enforces rate limiting, maps errors to CQI error types, and handles retries automatically.
func (*Request) IntoJSON ¶
IntoJSON tells the request to unmarshal the response body into the provided struct.
func (*Request) IntoProto ¶
IntoProto tells the request to unmarshal the response body as a protobuf message.
func (*Request) SetURL ¶
SetURL sets the URL for the request. Can be relative (appended to BaseURL) or absolute.
func (*Request) WithAuthToken ¶
WithAuthToken sets the Bearer authentication token.
func (*Request) WithBasicAuth ¶
WithBasicAuth sets basic authentication credentials.
func (*Request) WithFormData ¶
WithFormData sets the request body as form data.
func (*Request) WithHeader ¶
WithHeader sets a single header on the request.
func (*Request) WithHeaders ¶
WithHeaders sets multiple headers on the request.
func (*Request) WithJSON ¶
WithJSON sets the request body as JSON. The body is automatically serialized to JSON.
func (*Request) WithProto ¶
WithProto sets the request body as a protobuf message. The message is serialized to wire format before sending.
func (*Request) WithQueryParams ¶
WithQueryParams adds multiple query parameters to the request.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response wraps the resty response with CQI-specific error mapping and protobuf deserialization support.
func (*Response) BodyAsJSON ¶
BodyAsJSON unmarshals the response body into the provided struct.
func (*Response) BodyAsProto ¶
BodyAsProto unmarshals the response body as a protobuf message.
func (*Response) BodyAsString ¶
BodyAsString returns the response body as a string.
func (*Response) StatusCode ¶
StatusCode returns the HTTP status code.