Documentation
¶
Overview ¶
Package client provides a small, dependency-free HTTP client for talking to JSON REST APIs. It is the single transport layer shared by every service wrapper in this server.
The client is intentionally generic: callers describe a request (method, path, query, body) and supply a destination for the decoded JSON response. Authentication is pluggable via the Authorizer interface.
Index ¶
- type APIError
- type APIKeyHeaderAuthorizer
- type Authorizer
- type BearerAuthorizer
- type Client
- func (c *Client) Delete(ctx context.Context, path string, query url.Values, out any) error
- func (c *Client) Do(ctx context.Context, req Request) (*Response, error)
- func (c *Client) GetJSON(ctx context.Context, path string, query url.Values, out any) error
- func (c *Client) PatchJSON(ctx context.Context, path string, query url.Values, body, out any) error
- func (c *Client) PostJSON(ctx context.Context, path string, query url.Values, body, out any) error
- func (c *Client) PutJSON(ctx context.Context, path string, query url.Values, body, out any) error
- type Option
- type RawBody
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIError ¶
type APIError struct {
Method string // HTTP method of the failed request
URL string // request URL (path + query)
StatusCode int // HTTP status code
Message string // human-readable message extracted from the body
Body string // raw response body (truncated)
}
APIError is a structured error returned for any non-2xx HTTP response. It preserves the HTTP status and, when available, the service-provided message so callers (and ultimately the LLM) get an actionable explanation rather than an opaque status code.
type APIKeyHeaderAuthorizer ¶
type APIKeyHeaderAuthorizer struct {
// contains filtered or unexported fields
}
APIKeyHeaderAuthorizer authenticates by sending an API key in a fixed request header (e.g. "X-Api-Key: <key>"), the scheme used by most coinbase providers.
func NewAPIKeyHeaderAuthorizer ¶
func NewAPIKeyHeaderAuthorizer(header, key string) *APIKeyHeaderAuthorizer
NewAPIKeyHeaderAuthorizer builds an authorizer that sets header to key.
func (*APIKeyHeaderAuthorizer) Authorize ¶
func (a *APIKeyHeaderAuthorizer) Authorize(r *http.Request)
Authorize sets the configured API-key header.
type Authorizer ¶
type Authorizer interface {
// Authorize mutates the request to carry authentication.
Authorize(*http.Request)
}
Authorizer applies authentication credentials to an outgoing request.
type BearerAuthorizer ¶
type BearerAuthorizer struct {
// contains filtered or unexported fields
}
BearerAuthorizer authenticates using an OAuth-style bearer token.
func NewBearerAuthorizer ¶
func NewBearerAuthorizer(token string) *BearerAuthorizer
NewBearerAuthorizer builds a BearerAuthorizer for the given token.
func (*BearerAuthorizer) Authorize ¶
func (a *BearerAuthorizer) Authorize(r *http.Request)
Authorize sets the Authorization header for bearer auth.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a reusable JSON REST client bound to a base URL and an Authorizer. A Client is safe for concurrent use by multiple goroutines.
func New ¶
func New(baseURL string, auth Authorizer, opts ...Option) (*Client, error)
New creates a Client for the given base URL and Authorizer.
func (*Client) Do ¶
Do executes a request, decoding a successful JSON response into req.Out (when set) and returning a typed *APIError for non-2xx responses.
func (*Client) PatchJSON ¶
PatchJSON performs a PATCH request with a JSON body and decodes the response.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithHTTPClient ¶
WithHTTPClient sets a custom *http.Client (e.g. for testing or proxies).
func WithHeader ¶
WithHeader adds a header applied to every request (e.g. an API version pin).
func WithUserAgent ¶
WithUserAgent sets the User-Agent header sent on every request.
type RawBody ¶
RawBody captures an undecoded response body. Pass a *RawBody as Request.Out to receive the raw bytes (and content type) instead of JSON decoding.
type Request ¶
type Request struct {
// Method is the HTTP method (GET, POST, PATCH, PUT, DELETE).
Method string
// Path is appended to the client's base URL. A leading slash is optional.
Path string
// Query holds URL query parameters.
Query url.Values
// Body, when non-nil, is JSON-encoded and sent as the request body.
// If Body already implements io.Reader it is sent verbatim.
Body any
// ContentType overrides the request Content-Type. Defaults to
// "application/json" when a body is present.
ContentType string
// Header holds extra request headers (applied after defaults, so they win).
Header http.Header
// Out, when non-nil, receives the JSON-decoded response body.
Out any
}
Request describes a single REST call.