response

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package response provides HTTP response handlers for API calls. This is a Go translation of ai-sdk/packages/provider-utils/src/response-handler.ts

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractResponseHeaders

func ExtractResponseHeaders(resp *http.Response) map[string]string

ExtractResponseHeaders converts http.Header to map[string]string. For headers with multiple values, only the first value is included.

func ParseJSON

func ParseJSON[T any](text string, s *schema.Schema) (T, error)

ParseJSON parses JSON with optional schema validation. Returns the parsed value or an error.

Types

type Handler

type Handler[T any] func(opts HandlerOptions) (*HandlerResult[T], error)

Handler processes an HTTP response and returns a typed result.

func CreateBinaryResponseHandler

func CreateBinaryResponseHandler() Handler[[]byte]

CreateBinaryResponseHandler creates a handler for binary responses. It reads the entire response body and returns it as a byte slice. Returns an error if the response body is empty or cannot be read.

func CreateEventSourceResponseHandler

func CreateEventSourceResponseHandler[T any](s *schema.Schema) Handler[<-chan *ParseResult[T]]

CreateEventSourceResponseHandler creates a handler for SSE (Server-Sent Events) streams. It parses each SSE data line as JSON and sends the results to a channel. The channel is closed when the stream ends or encounters an error.

func CreateJSONErrorResponseHandler

func CreateJSONErrorResponseHandler[T any](config JSONErrorConfig[T]) Handler[*errors.APICallError]

CreateJSONErrorResponseHandler creates a handler for JSON error responses. It attempts to parse the response body as JSON using the provided schema. Falls back to status text if the body is empty or JSON parsing fails.

func CreateJSONResponseHandler

func CreateJSONResponseHandler[T any](s *schema.Schema) Handler[T]

CreateJSONResponseHandler creates a handler that parses JSON responses. The handler validates against the provided schema if non-nil. Returns both the parsed value and rawValue in the result.

func CreateStatusCodeErrorResponseHandler

func CreateStatusCodeErrorResponseHandler() Handler[*errors.APICallError]

CreateStatusCodeErrorResponseHandler creates a generic error handler. It uses the HTTP status text as the error message and includes the response body.

type HandlerOptions

type HandlerOptions struct {
	// URL is the request URL.
	URL string

	// RequestBodyValues contains the request body values for error reporting.
	RequestBodyValues any

	// Response is the HTTP response to process.
	Response *http.Response
}

HandlerOptions contains inputs for a response handler.

type HandlerResult

type HandlerResult[T any] struct {
	// Value is the parsed/processed response value.
	Value T

	// RawValue is the raw parsed value before schema validation (for JSON responses).
	RawValue any

	// ResponseHeaders are the response headers as a flat map.
	ResponseHeaders map[string]string
}

HandlerResult contains the result of processing a response.

type JSONErrorConfig

type JSONErrorConfig[T any] struct {
	// ErrorSchema parses the error JSON body into the provider-specific error type.
	// Returns the parsed error and any parsing error.
	ErrorSchema func(body []byte) (T, error)

	// ErrorToMessage extracts a user-facing message from the parsed error.
	ErrorToMessage func(error T) string

	// IsRetryable determines if the error is retryable based on response and parsed error.
	// If nil, default retryability logic is used based on status code.
	IsRetryable func(resp *http.Response, error *T) bool
}

JSONErrorConfig configures the JSON error response handler.

type ParseResult

type ParseResult[T any] struct {
	// Success indicates if parsing succeeded.
	Success bool

	// Value is the parsed and validated value (only valid if Success is true).
	Value T

	// RawValue is the raw parsed JSON before validation.
	RawValue any

	// Error is the parsing or validation error (only valid if Success is false).
	Error error
}

ParseResult represents the result of parsing JSON.

func SafeParseJSON

func SafeParseJSON[T any](text string, s *schema.Schema) *ParseResult[T]

SafeParseJSON parses JSON with optional schema validation. Returns a ParseResult with Success=true and both Value and RawValue on success, or Success=false and Error on failure.

Jump to

Keyboard shortcuts

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