httpjson

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package httpjson is the shared HTTP-JSON plumbing for lore's provider adapters (the OpenAI-compatible embed/chat client and the Cohere-style reranker): a POST of a JSON body, decode of a 2xx JSON response, bearer or api-key auth, and automatic retry of transient 429/503 responses honoring Retry-After. It carries no provider-specific knowledge.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth int

Auth selects how the API key is presented. AuthBearer (Authorization: Bearer <key>) is the OpenAI/Cohere default; AuthAPIKey (api-key: <key>) is Azure OpenAI's scheme.

const (
	AuthBearer Auth = iota
	AuthAPIKey
)

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the shared HTTP plumbing for a JSON-over-HTTP endpoint.

func NewClient

func NewClient(baseURL, apiKey string, auth Auth, httpClient *http.Client) *Client

NewClient builds a Client. A nil httpClient uses http.DefaultClient.

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, in, out any) error

Post marshals in as JSON to baseURL+path, then decodes a 2xx response into out. It retries transient rate-limit/availability responses (429, 503), honoring the Retry-After header and backing off, up to maxRetries; other non-2xx responses become an error carrying the status and a body snippet. The context governs the request lifetime and cancels any backoff.

func (*Client) PostStream

func (c *Client) PostStream(ctx context.Context, path string, in any) (io.ReadCloser, error)

PostStream marshals in as JSON and returns the raw body of a 2xx response for the caller to stream (e.g. server-sent events). It applies the same auth and transient-retry policy as Post — but only up to the point a response status is known; once streaming begins the body is the caller's, who MUST Close it. A non-2xx response is drained and returned as an error carrying a body snippet.

type StatusError added in v1.0.0

type StatusError struct {
	Path string // request path, e.g. "/embeddings"
	Code int    // HTTP status code
	Body string // trimmed response-body snippet
}

StatusError is a non-2xx response from the endpoint. It carries the status code separately from the message so callers can recognize a class of failure — notably an authentication rejection (401/403) — and replace the raw upstream body with actionable guidance instead of surfacing a provider-specific JSON blob. Error() keeps the original one-line form for logs and pipes.

func (*StatusError) Error added in v1.0.0

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

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