httpc

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package httpc provides HTTP helper functions for provider implementations.

Index

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

func MustMarshalJSON(v any) []byte

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

func MustNewRequest(ctx context.Context, method, url string, body []byte) *http.Request

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

func ParseDataURL(url string) (mediaType, data string, ok bool)

ParseDataURL extracts media type and base64 data from a data URL. Format: data:<mediaType>;base64,<data>

Types

type ErrorParser added in v0.5.9

type ErrorParser func(providerID string, statusCode int, body []byte, headers http.Header) error

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.

Jump to

Keyboard shortcuts

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