Documentation
¶
Overview ¶
Package internal provides shared utilities for the goai package.
Index ¶
- func DefaultShouldRetry(err error, attempt int) bool
- func IsRetryableStatus(statusCode int) bool
- func ParseRetryAfter(value string) time.Duration
- func WithRetry(ctx context.Context, maxAttempts int, op func() error) error
- func WithRetryResult[T any](ctx context.Context, maxAttempts int, op func() (T, error)) (T, error)
- type HTTPClient
- func (c *HTTPClient) Do(ctx context.Context, opts RequestOptions) (*Response, error)
- func (c *HTTPClient) Get(ctx context.Context, path string, headers map[string]string) (*Response, error)
- func (c *HTTPClient) Post(ctx context.Context, path string, body any, headers map[string]string) (*Response, error)
- func (c *HTTPClient) PostStream(ctx context.Context, path string, body any, headers map[string]string) (*Response, error)
- func (c *HTTPClient) SetAuthAPIKey(headerName, apiKey string)
- func (c *HTTPClient) SetAuthBearer(token string)
- type MultipartWriter
- func (w *MultipartWriter) Bytes() []byte
- func (w *MultipartWriter) Close()
- func (w *MultipartWriter) ContentType() string
- func (w *MultipartWriter) Reader() io.Reader
- func (w *MultipartWriter) WriteField(name, value string)
- func (w *MultipartWriter) WriteFile(name, filename, contentType string, data []byte)
- type RequestOptions
- type Response
- type Retrier
- type RetryConfig
- type SSEEvent
- type SSEParser
- type SSEReader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultShouldRetry ¶
DefaultShouldRetry is the default retry predicate. It retries on transient errors (rate limits, server errors).
func IsRetryableStatus ¶
IsRetryable checks if an HTTP status code is retryable.
func ParseRetryAfter ¶
ParseRetryAfter parses the Retry-After header value. Returns the duration to wait, or 0 if not parseable.
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.
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.
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 ¶
GetEventType extracts the event type from an event line.
func (*SSEParser) IsEventLine ¶
IsEventLine returns true if the line is an event type line.
type SSEReader ¶
type SSEReader struct {
// contains filtered or unexported fields
}
SSEReader reads Server-Sent Events from a stream.
func NewSSEReader ¶
NewSSEReader creates a new SSE reader.