feat(httpclient): initial stable release v0.9.0
Resilient HTTP client with circuit breaking, exponential-backoff retry, X-Request-ID propagation, and a generic typed JSON helper.
What's included:
- Client interface with Do(req) method; New(logger, cfg) and NewWithDefaults(logger) constructors
- Config struct with env-tag support for timeout, dial timeout, retry, and circuit breaker parameters
- Retry via avast/retry-go/v4 with BackOffDelay; triggers only on network errors and HTTP 5xx
- Circuit breaker via sony/gobreaker wrapping the full retry loop; open circuit → xerrors.ErrUnavailable
- X-Request-ID header propagated automatically from context via logz.GetRequestID on every attempt
- DoJSON[T](ctx, client, req) generic helper for typed JSON request/response with xerrors error mapping
- MapStatusToError(code, msg) exported function mapping HTTP status codes to xerrors types
Tested-via: todo-api POC integration
Reviewed-against: docs/adr/
2026-03-19 13:04:37 +00:00
|
|
|
package httpclient
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-11 19:50:16 -06:00
|
|
|
"bytes"
|
feat(httpclient): initial stable release v0.9.0
Resilient HTTP client with circuit breaking, exponential-backoff retry, X-Request-ID propagation, and a generic typed JSON helper.
What's included:
- Client interface with Do(req) method; New(logger, cfg) and NewWithDefaults(logger) constructors
- Config struct with env-tag support for timeout, dial timeout, retry, and circuit breaker parameters
- Retry via avast/retry-go/v4 with BackOffDelay; triggers only on network errors and HTTP 5xx
- Circuit breaker via sony/gobreaker wrapping the full retry loop; open circuit → xerrors.ErrUnavailable
- X-Request-ID header propagated automatically from context via logz.GetRequestID on every attempt
- DoJSON[T](ctx, client, req) generic helper for typed JSON request/response with xerrors error mapping
- MapStatusToError(code, msg) exported function mapping HTTP status codes to xerrors types
Tested-via: todo-api POC integration
Reviewed-against: docs/adr/
2026-03-19 13:04:37 +00:00
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"code.nochebuena.dev/go/xerrors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DoJSON executes req and decodes the JSON response body into T.
|
|
|
|
|
// Returns a xerrors-typed error for HTTP 4xx/5xx responses.
|
|
|
|
|
func DoJSON[T any](ctx context.Context, client Client, req *http.Request) (*T, error) {
|
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
|
return nil, MapStatusToError(resp.StatusCode, "external API error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, xerrors.New(xerrors.ErrInternal, "failed to read response body").WithError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data T
|
|
|
|
|
if err := json.Unmarshal(body, &data); err != nil {
|
|
|
|
|
return nil, xerrors.New(xerrors.ErrInternal, "failed to decode JSON response").WithError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 19:50:16 -06:00
|
|
|
// DoJSONRequest serialises body as JSON, POST/PUT/PATCHes it to rawURL, and
|
|
|
|
|
// decodes the response into Resp. For requests without a body, use DoJSON instead.
|
|
|
|
|
func DoJSONRequest[Req, Resp any](ctx context.Context, client Client, method, rawURL string, body Req) (*Resp, error) {
|
|
|
|
|
data, err := json.Marshal(body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, xerrors.New(xerrors.ErrInternal, "failed to encode request body").WithError(err)
|
|
|
|
|
}
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, rawURL, bytes.NewReader(data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, xerrors.New(xerrors.ErrInternal, "failed to create request").WithError(err)
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
return DoJSON[Resp](ctx, client, req)
|
|
|
|
|
}
|
|
|
|
|
|
feat(httpclient): initial stable release v0.9.0
Resilient HTTP client with circuit breaking, exponential-backoff retry, X-Request-ID propagation, and a generic typed JSON helper.
What's included:
- Client interface with Do(req) method; New(logger, cfg) and NewWithDefaults(logger) constructors
- Config struct with env-tag support for timeout, dial timeout, retry, and circuit breaker parameters
- Retry via avast/retry-go/v4 with BackOffDelay; triggers only on network errors and HTTP 5xx
- Circuit breaker via sony/gobreaker wrapping the full retry loop; open circuit → xerrors.ErrUnavailable
- X-Request-ID header propagated automatically from context via logz.GetRequestID on every attempt
- DoJSON[T](ctx, client, req) generic helper for typed JSON request/response with xerrors error mapping
- MapStatusToError(code, msg) exported function mapping HTTP status codes to xerrors types
Tested-via: todo-api POC integration
Reviewed-against: docs/adr/
2026-03-19 13:04:37 +00:00
|
|
|
// MapStatusToError maps an HTTP status code to the matching xerrors type.
|
|
|
|
|
func MapStatusToError(code int, msg string) error {
|
|
|
|
|
switch code {
|
|
|
|
|
case http.StatusNotFound:
|
|
|
|
|
return xerrors.New(xerrors.ErrNotFound, msg)
|
|
|
|
|
case http.StatusBadRequest:
|
|
|
|
|
return xerrors.New(xerrors.ErrInvalidInput, msg)
|
|
|
|
|
case http.StatusUnauthorized:
|
|
|
|
|
return xerrors.New(xerrors.ErrUnauthorized, msg)
|
|
|
|
|
case http.StatusForbidden:
|
|
|
|
|
return xerrors.New(xerrors.ErrPermissionDenied, msg)
|
|
|
|
|
case http.StatusConflict:
|
|
|
|
|
return xerrors.New(xerrors.ErrAlreadyExists, msg)
|
|
|
|
|
case http.StatusTooManyRequests:
|
|
|
|
|
return xerrors.New(xerrors.ErrUnavailable, msg)
|
|
|
|
|
default:
|
|
|
|
|
return xerrors.New(xerrors.ErrInternal, msg)
|
|
|
|
|
}
|
|
|
|
|
}
|