internal

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: 10 Imported by: 0

Documentation

Overview

Package internal provides shared utilities for the goai package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultShouldRetry

func DefaultShouldRetry(err error, attempt int) bool

DefaultShouldRetry is the default retry predicate. It retries on transient errors (rate limits, server errors).

func IsRetryableStatus

func IsRetryableStatus(statusCode int) bool

IsRetryable checks if an HTTP status code is retryable.

func ParseRetryAfter

func ParseRetryAfter(value string) time.Duration

ParseRetryAfter parses the Retry-After header value. Returns the duration to wait, or 0 if not parseable.

func WithRetry

func WithRetry(ctx context.Context, maxAttempts int, op func() error) error

WithRetry is a convenience function for simple retry operations.

func WithRetryResult

func WithRetryResult[T any](ctx context.Context, maxAttempts int, op func() (T, error)) (T, error)

WithRetryResult is a convenience function for operations that return results.

Types

type HTTPClient

type HTTPClient struct {
	// Client is the underlying HTTP client.
	Client *http.Client

	// BaseURL is the base URL for all requests.
	BaseURL string

	// DefaultHeaders are headers sent with every request.
	DefaultHeaders map[string]string

	// MaxRetries is the maximum number of retry attempts.
	MaxRetries int

	// RetryBackoff is the initial backoff duration for retries.
	RetryBackoff time.Duration
}

HTTPClient wraps http.Client with goai-specific functionality.

func NewHTTPClient

func NewHTTPClient(baseURL string) *HTTPClient

NewHTTPClient creates a new HTTP client with sensible defaults.

func (*HTTPClient) Do

func (c *HTTPClient) Do(ctx context.Context, opts RequestOptions) (*Response, error)

Do executes an HTTP request.

func (*HTTPClient) Get

func (c *HTTPClient) Get(ctx context.Context, path string, headers map[string]string) (*Response, error)

Get performs a GET request.

func (*HTTPClient) Post

func (c *HTTPClient) Post(ctx context.Context, path string, body any, headers map[string]string) (*Response, error)

Post performs a POST request with JSON body.

func (*HTTPClient) PostStream

func (c *HTTPClient) PostStream(ctx context.Context, path string, body any, headers map[string]string) (*Response, error)

PostStream performs a POST request and returns a streaming response.

func (*HTTPClient) SetAuthAPIKey

func (c *HTTPClient) SetAuthAPIKey(headerName, apiKey string)

SetAuthAPIKey sets an API key header.

func (*HTTPClient) SetAuthBearer

func (c *HTTPClient) SetAuthBearer(token string)

SetAuthBearer sets the Authorization header with a Bearer token.

type MultipartWriter

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

MultipartWriter helps build multipart/form-data requests.

func NewMultipartWriter

func NewMultipartWriter() *MultipartWriter

NewMultipartWriter creates a new multipart writer.

func (*MultipartWriter) Bytes

func (w *MultipartWriter) Bytes() []byte

Bytes returns the multipart body as bytes.

func (*MultipartWriter) Close

func (w *MultipartWriter) Close()

Close finalizes the multipart body.

func (*MultipartWriter) ContentType

func (w *MultipartWriter) ContentType() string

ContentType returns the Content-Type header value.

func (*MultipartWriter) Reader

func (w *MultipartWriter) Reader() io.Reader

Reader returns the multipart body reader.

func (*MultipartWriter) WriteField

func (w *MultipartWriter) WriteField(name, value string)

WriteField writes a text field.

func (*MultipartWriter) WriteFile

func (w *MultipartWriter) WriteFile(name, filename, contentType string, data []byte)

WriteFile writes a file field.

type RequestOptions

type RequestOptions struct {
	// Method is the HTTP method (GET, POST, etc.).
	Method string

	// Path is the URL path (appended to BaseURL).
	Path string

	// Headers are additional headers for this request.
	Headers map[string]string

	// Body is the request body (will be JSON-encoded if not nil).
	Body any

	// RawBody is the raw request body (used instead of Body if set).
	RawBody io.Reader

	// ContentType is the Content-Type header (defaults to "application/json").
	ContentType string

	// Stream indicates this is a streaming request.
	Stream bool
}

RequestOptions contains options for an HTTP request.

type Response

type Response struct {
	// StatusCode is the HTTP status code.
	StatusCode int

	// Headers are the response headers.
	Headers http.Header

	// Body is the response body.
	Body []byte

	// Reader provides streaming access to the response body.
	Reader io.ReadCloser
}

Response contains the HTTP response.

func (*Response) JSON

func (r *Response) JSON(v any) error

JSON decodes the response body as JSON into v.

type Retrier

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

Retrier executes operations with retry logic.

func NewRetrier

func NewRetrier(config RetryConfig) *Retrier

NewRetrier creates a new retrier with the given configuration.

func (*Retrier) Do

func (r *Retrier) Do(ctx context.Context, op func() error) error

Do executes the operation with retry logic.

func (*Retrier) DoWithResult

func (r *Retrier) DoWithResult(ctx context.Context, op func() (any, error)) (any, error)

DoWithResult executes an operation that returns a result with retry logic.

type RetryConfig

type RetryConfig struct {
	// MaxAttempts is the maximum number of attempts (including the first).
	MaxAttempts int

	// InitialDelay is the initial delay before the first retry.
	InitialDelay time.Duration

	// MaxDelay is the maximum delay between retries.
	MaxDelay time.Duration

	// Multiplier is the factor by which the delay increases after each retry.
	Multiplier float64

	// Jitter adds randomness to the delay (0 to 1).
	Jitter float64

	// ShouldRetry determines if an error should trigger a retry.
	ShouldRetry func(err error, attempt int) bool
}

RetryConfig contains configuration for retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns a sensible default retry configuration.

type SSEEvent

type SSEEvent struct {
	// Event is the event type (from "event:" field).
	Event string

	// Data is the event data (from "data:" field).
	Data string

	// ID is the event ID (from "id:" field).
	ID string

	// Retry is the retry interval in milliseconds (from "retry:" field).
	Retry int
}

SSEEvent represents a Server-Sent Event.

type SSEParser

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

SSEParser provides a simpler line-by-line SSE parsing interface.

func NewSSEParser

func NewSSEParser() *SSEParser

NewSSEParser creates a new SSE parser with default prefixes.

func (*SSEParser) GetEventType

func (p *SSEParser) GetEventType(line string) string

GetEventType extracts the event type from an event line.

func (*SSEParser) IsDone

func (p *SSEParser) IsDone(data string) bool

IsDone returns true if the data is the SSE done marker.

func (*SSEParser) IsEventLine

func (p *SSEParser) IsEventLine(line string) bool

IsEventLine returns true if the line is an event type line.

func (*SSEParser) ParseLine

func (p *SSEParser) ParseLine(line string) (string, bool)

ParseLine parses a single SSE line. Returns the data content if the line is a data line, empty string otherwise. Returns true for the second value if this is a data line.

type SSEReader

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

SSEReader reads Server-Sent Events from a stream.

func NewSSEReader

func NewSSEReader(r io.Reader) *SSEReader

NewSSEReader creates a new SSE reader.

func (*SSEReader) Err

func (r *SSEReader) Err() error

Err returns any error that occurred during reading.

func (*SSEReader) Events

func (r *SSEReader) Events() <-chan *SSEEvent

Events returns a channel that yields SSE events. The channel is closed when the stream ends or an error occurs.

func (*SSEReader) Next

func (r *SSEReader) Next() *SSEEvent

Next reads the next SSE event. Returns nil when the stream ends or an error occurs.

Jump to

Keyboard shortcuts

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