httpclient

package
v1.3.10 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 6 Imported by: 0

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 New

func New(opts Options) *Client

New returns a Client configured with opts.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns the configured base URL (used by callers that need to build URLs outside of DoJSON, e.g. download endpoints).

func (*Client) DoJSON

func (c *Client) DoJSON(ctx context.Context, method, path string, result interface{}) (err error)

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

func (c *Client) HTTPClient() *http.Client

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

func (c *Client) IsAuthenticated() bool

IsAuthenticated reports whether the client has a non-empty API key.

func (*Client) SetAPIKey

func (c *Client) SetAPIKey(key string)

SetAPIKey updates the API key used for subsequent requests.

func (*Client) SetBaseURL

func (c *Client) SetBaseURL(u string)

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.

Jump to

Keyboard shortcuts

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