http

package
v1.0.61 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package http provides common HTTP utilities for provider implementations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleHTTPStatusError

func HandleHTTPStatusError(resp *http.Response, provider types.ProviderType, operation string, customMessage string) error

HandleHTTPStatusError checks if an HTTP response indicates an error and returns a standardized ProviderError if so.

If the response status code is not OK (200), it reads the response body and returns a categorized error based on the status code.

Parameters:

  • resp: The HTTP response to check
  • provider: The provider type for error reporting
  • operation: The operation name for error reporting
  • customMessage: Optional custom message prefix (can be empty)

Returns an error if the status code indicates an error, nil otherwise.

Usage:

resp, err := client.Do(req)
if err != nil {
    return err
}
defer httpcommon.SafeClose(resp)
if err := httpcommon.HandleHTTPStatusError(resp, types.ProviderTypeOpenAI, "chat_completion", ""); err != nil {
    return nil, err
}
Example
resp, err := http.Get("https://example.com")
if err != nil {
	panic(err)
}
defer SafeClose(resp)

if err := HandleHTTPStatusError(resp, types.ProviderTypeOpenAI, "fetch_models", ""); err != nil {
	panic(err)
}

func ReadAndUnmarshalResponse

func ReadAndUnmarshalResponse(resp *http.Response, target interface{}, provider types.ProviderType, operation string) error

ReadAndUnmarshalResponse reads the entire response body and unmarshals it into the target struct with standardized error handling.

This combines the common pattern of reading the body and then unmarshaling it into a single function call with proper error handling.

Parameters:

  • resp: The HTTP response to read from
  • target: The target struct to unmarshal into (must be a pointer)
  • provider: The provider type for error reporting
  • operation: The operation name for error reporting

Returns an error if reading or unmarshaling fails.

Usage:

var response ChatCompletionResponse
if err := httpcommon.ReadAndUnmarshalResponse(resp, &response, types.ProviderTypeOpenAI, "chat_completion"); err != nil {
    return nil, err
}
defer httpcommon.SafeClose(resp)

func SafeClose

func SafeClose(resp *http.Response)

SafeClose safely closes an HTTP response body. It logs but doesn't fail on close errors, as response body close errors are typically not actionable in the context of API clients.

Usage:

resp, err := client.Do(req)
if err != nil {
    return err
}
defer httpcommon.SafeClose(resp)
Example
resp, err := http.Get("https://example.com")
if err != nil {
	panic(err)
}
defer SafeClose(resp)
// Process response...

func SafeCloseWithError

func SafeCloseWithError(resp *http.Response) error

SafeCloseWithError closes an HTTP response body and returns an error if closing fails. Use this when you need to handle close errors explicitly.

Usage:

resp, err := client.Do(req)
if err != nil {
    return err
}
defer func() {
    if err := httpcommon.SafeCloseWithError(resp); err != nil {
        log.Printf("warning: failed to close response body: %v", err)
    }
}()

func UnmarshalJSONBytes

func UnmarshalJSONBytes(data []byte, target interface{}, provider types.ProviderType, operation string) error

UnmarshalJSONBytes unmarshals JSON bytes into a target struct with standardized error handling for provider errors.

This is useful when you've already read the response body into a byte slice and need to unmarshal it with consistent error handling.

Parameters:

  • data: The JSON data to unmarshal
  • target: The target struct to unmarshal into (must be a pointer)
  • provider: The provider type for error reporting
  • operation: The operation name for error reporting

Returns an error if unmarshaling fails.

Usage:

body, err := io.ReadAll(resp.Body)
if err != nil {
    return err
}
var response ChatCompletionResponse
if err := httpcommon.UnmarshalJSONBytes(body, &response, types.ProviderTypeOpenAI, "chat_completion"); err != nil {
    return nil, err
}

func UnmarshalJSONResponse

func UnmarshalJSONResponse(body io.Reader, target interface{}, provider types.ProviderType, operation string) error

UnmarshalJSONResponse unmarshals an HTTP response body into a target struct with standardized error handling for provider errors.

It handles JSON decoding errors and wraps them in an InvalidRequestError with the provider type and operation name for consistent error reporting.

Parameters:

  • body: The response body to read and unmarshal
  • target: The target struct to unmarshal into (must be a pointer)
  • provider: The provider type for error reporting
  • operation: The operation name for error reporting (e.g., "chat_completion", "fetch_models")

Returns an error if unmarshaling fails.

Usage:

var response ChatCompletionResponse
if err := httpcommon.UnmarshalJSONResponse(resp.Body, &response, types.ProviderTypeOpenAI, "chat_completion"); err != nil {
    return nil, err
}
Example
type ChatResponse struct {
	Message string `json:"message"`
}

resp, err := http.Get("https://example.com")
if err != nil {
	panic(err)
}
defer SafeClose(resp)

var result ChatResponse
if err := UnmarshalJSONResponse(resp.Body, &result, types.ProviderTypeOpenAI, "chat"); err != nil {
	panic(err)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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