Documentation
¶
Overview ¶
Package httpc provides HTTP helper functions for provider implementations.
Index ¶
- func DoJSONRequest(ctx context.Context, cfg RequestConfig, parseError ErrorParser) (*http.Response, error)
- func MustMarshalJSON(v any) []byte
- func MustNewRequest(ctx context.Context, method, url string, body []byte) *http.Request
- func ParseDataURL(url string) (mediaType, data string, ok bool)
- type ErrorParser
- type RequestConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoJSONRequest ¶ added in v0.5.9
func DoJSONRequest(ctx context.Context, cfg RequestConfig, parseError ErrorParser) (*http.Response, error)
DoJSONRequest builds, sends, and validates a JSON POST request. It extracts "_headers" from the body map, marshals JSON, sets Content-Type and Authorization Bearer (if token is non-empty), applies provider and per-request headers, checks the HTTP status code, and returns the response or a parsed error.
func MustMarshalJSON ¶
MustMarshalJSON marshals v to JSON. It panics on failure, which should only occur with unmarshalable types (chan, func, cyclic structs) - a programming error, not a runtime condition. All call sites pass provider.GenerateParams or similar well-typed structs that are always marshalable.
func MustNewRequest ¶
MustNewRequest creates an HTTP request. It panics on failure, which should only occur with an invalid HTTP method or a nil context - both programming errors detectable at development time, not runtime conditions. All call sites pass a valid context and a constant HTTP method string.
func ParseDataURL ¶
ParseDataURL extracts media type and base64 data from a data URL. Format: data:<mediaType>;base64,<data>
Types ¶
type ErrorParser ¶ added in v0.5.9
ErrorParser is a function that converts an HTTP error response into a Go error. It receives the provider ID, status code, response body, and response headers.
type RequestConfig ¶ added in v0.5.9
type RequestConfig struct {
URL string // Full URL to POST to.
Token string // Bearer token (empty = no Authorization header).
Body map[string]any // JSON body; "_headers" key is extracted and applied.
Headers map[string]string // Provider-level extra headers (e.g. WithHeaders).
HTTPClient *http.Client // Custom client (nil = http.DefaultClient).
ProviderID string // Provider name for error messages.
}
RequestConfig holds common HTTP request parameters for DoJSONRequest.