Documentation
¶
Overview ¶
Package httpclient is a thin JSON HTTP client used by mod-source SDKs (NexusMods, CurseForge, ...). It centralises auth-header injection, status-code mapping (401 -> domain.ErrAuthRequired), JSON decode, and limited body reads on errors. Source-specific behaviour (extra status codes, body parsing) plugs in via the optional ErrorMapper.
Index ¶
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 is a small JSON HTTP client wrapping net/http for use by mod-source SDKs. Construct via New; configure via Options.
func (*Client) BaseURL ¶
BaseURL returns the configured base URL (used by callers that need to build URLs outside of DoJSON, e.g. download endpoints).
func (*Client) DoJSON ¶
DoJSON performs an HTTP request against baseURL+path and JSON-decodes the response body into result. Auth header is set when an APIKey is configured. Non-2xx responses are first offered to ErrorMapper; if ErrorMapper returns nil (or is unset), 401 is mapped to domain.ErrAuthRequired and other statuses are surfaced as "API error (status N): <body>".
func (*Client) HTTPClient ¶
HTTPClient returns the underlying *http.Client (used by callers that need to issue raw downloads or non-JSON requests with the same transport).
func (*Client) IsAuthenticated ¶
IsAuthenticated reports whether the client has a non-empty API key.
func (*Client) SetBaseURL ¶
SetBaseURL replaces the configured base URL. Used by tests that wire a httptest server in front of the real client.
type Options ¶
type Options struct {
HTTPClient *http.Client
BaseURL string
APIKey string
// AuthHeader is the request header used to forward APIKey, e.g. "apikey"
// (NexusMods) or "x-api-key" (CurseForge).
AuthHeader string
// AuthLabel is the human-readable source name interpolated into the
// "<label> API key required" error returned on 401.
AuthLabel string
// ErrorMapper, when set, is consulted before the default non-2xx mapping.
// Return nil to defer to the default; return a non-nil error to short-
// circuit (e.g. translate 404 to a domain error).
ErrorMapper func(status int, body []byte, requestPath string) error
}
Options configures a Client. AuthHeader and AuthLabel are required; the rest have sensible zero-value defaults.