client

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 9 Imported by: 0

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

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.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

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) Delete

func (c *Client) Delete(ctx context.Context, path string, query url.Values, out any) error

Delete performs a DELETE request, optionally decoding a response body.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req Request) (*Response, error)

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) GetJSON

func (c *Client) GetJSON(ctx context.Context, path string, query url.Values, out any) error

GetJSON performs a GET request and decodes the response into out.

func (*Client) PatchJSON

func (c *Client) PatchJSON(ctx context.Context, path string, query url.Values, body, out any) error

PatchJSON performs a PATCH request with a JSON body and decodes the response.

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, path string, query url.Values, body, out any) error

PostJSON performs a POST request with a JSON body and decodes the response.

func (*Client) PutJSON

func (c *Client) PutJSON(ctx context.Context, path string, query url.Values, body, out any) error

PutJSON performs a PUT request with a JSON body and decodes the response.

type Option

type Option func(*Client)

Option configures a Client.

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

WithHTTPClient sets a custom *http.Client (e.g. for testing or proxies).

func WithHeader

func WithHeader(key, value string) Option

WithHeader adds a header applied to every request (e.g. an API version pin).

func WithUserAgent

func WithUserAgent(ua string) Option

WithUserAgent sets the User-Agent header sent on every request.

type RawBody

type RawBody struct {
	Bytes       []byte
	ContentType string
}

RawBody captures an undecoded response body. Pass a *RawBody as Request.Out to receive the raw bytes (and content type) instead of JSON decoding.

func (*RawBody) String

func (r *RawBody) String() string

String returns the raw body as a string.

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.

type Response

type Response struct {
	StatusCode int
	Header     http.Header
}

Response carries metadata about a completed request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL