httpClient

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package httputil provides HTTP client utilities for making API requests. It handles common HTTP operations like request creation, response parsing, and error handling.

Index

Constants

View Source
const (
	// APITypeOpenAI represents the standard OpenAI API
	APITypeOpenAI APIType = "OPEN_AI"

	// APITypeAzure represents the Azure OpenAI API
	APITypeAzure APIType = "AZURE"

	// OpenaiRealtimeAPIURLv1 is the base URL for the OpenAI Realtime API.
	OpenaiRealtimeAPIURLv1 = "wss://api.openai.com/v1/realtime"

	// OpenaiAPIURLv1 is the base URL for the OpenAI API.
	OpenaiAPIURLv1 = "https://api.openai.com/v1"
)
View Source
const (
	DefaultRequestTimeout = 500 * time.Millisecond // Fast timeout for API calls
)

Default configuration constants

Variables

This section is empty.

Functions

func Do

func Do[Q any, R any](ctx context.Context, url string, req *Q, opts ...HTTPOption) (*R, error)

Do performs an HTTP request with the given options. It handles request creation, execution, response parsing, and error handling.

Generic type parameters:

  • Q: The request type to be marshaled to JSON
  • R: The response type to be unmarshaled from JSON

Parameters:

  • ctx: The context for the request
  • url: The URL to send the request to
  • req: The request body to be marshaled to JSON
  • opts: Optional configuration for the request

Returns:

  • *R: A pointer to the unmarshaled response
  • error: An error if the request failed

func GetHeaders

func GetHeaders(config ClientConfig) http.Header

GetHeaders returns the appropriate headers based on API type

Parameters:

  • config: The client configuration

Returns:

  • http.Header: The headers to use for the request

func GetURL

func GetURL(config ClientConfig, model string) string

GetURL constructs the appropriate URL based on API type and model

Parameters:

  • config: The client configuration
  • model: The model to use

Returns:

  • string: The URL to use for the request

Types

type APIType

type APIType string

APIType represents the type of API (Azure or OpenAI)

type ClientConfig

type ClientConfig struct {
	BaseURL    string       // Base URL for the API
	APIType    APIType      // Type of API (OpenAI or Azure)
	APIVersion string       // API version (used for Azure)
	HTTPClient *http.Client // HTTP client to use for requests
	APIBaseURL string       // Base URL for the REST API
	// contains filtered or unexported fields
}

ClientConfig holds the configuration for the HTTP client It contains settings for authentication, API endpoints, and HTTP client configuration.

func DefaultAzureConfig

func DefaultAzureConfig(apiKey, baseURL string) ClientConfig

DefaultAzureConfig creates a default configuration for Azure OpenAI API

Parameters:

  • apiKey: The API key for Azure OpenAI
  • baseURL: The base URL for the Azure OpenAI endpoint

Returns:

  • ClientConfig: A configuration for the Azure OpenAI API

func DefaultConfig

func DefaultConfig(authToken string) ClientConfig

DefaultConfig creates a default configuration with the given auth token for the OpenAI API.

Parameters:

  • authToken: The authentication token for the OpenAI API

Returns:

  • ClientConfig: A configuration for the OpenAI API

func (ClientConfig) String

func (c ClientConfig) String() string

String returns a string representation of the ClientConfig It masks sensitive information like auth tokens for security

type HTTPOption

type HTTPOption func(*option)

HTTPOption is a function that configures an HTTP request option

func WithBasicAuth

func WithBasicAuth(username, password string) HTTPOption

WithBasicAuth sets basic authentication headers for the HTTP request Parameters:

  • username: The username for basic auth
  • password: The password for basic auth

func WithBearerAuth

func WithBearerAuth(token string) HTTPOption

WithBearerAuth sets bearer token authentication for the HTTP request Parameters:

  • token: The bearer token for authentication

func WithClient

func WithClient(client *http.Client) HTTPOption

WithClient sets the HTTP client for the request Parameters:

  • client: The HTTP client to use for the request

func WithHeader

func WithHeader(key, value string) HTTPOption

WithHeader sets a single header for the HTTP request Parameters:

  • key: The header key
  • value: The header value

func WithHeaders

func WithHeaders(headers http.Header) HTTPOption

WithHeaders sets the headers for the HTTP request Parameters:

  • headers: The HTTP headers to use for the request

func WithMethod

func WithMethod(method string) HTTPOption

WithMethod sets the HTTP method for the request Parameters:

  • method: The HTTP method to use (e.g., GET, POST, PUT)

func WithRetryConfig

func WithRetryConfig(config RetryConfig) HTTPOption

WithRetryConfig sets the retry configuration for the HTTP request Parameters:

  • config: The retry configuration to use

func WithTimeout

func WithTimeout(timeout time.Duration) HTTPOption

WithTimeout sets a timeout for the HTTP request Parameters:

  • timeout: The timeout duration for the request

type RetryConfig

type RetryConfig struct {
	MaxRetries           int           // Maximum number of retry attempts
	RetryDelay           time.Duration // Base delay between retries
	MaxDelay             time.Duration // Maximum delay between retries
	RetryableStatusCodes []int         // HTTP status codes that should trigger a retry
}

RetryConfig defines the retry behavior for HTTP requests

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns a sensible default retry configuration

Jump to

Keyboard shortcuts

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