Codex.HTTPClient behaviour (Codex SDK v0.7.2)

Copy Markdown View Source

HTTP client abstraction for making HTTP requests.

This module provides a behaviour and default implementation for HTTP operations, allowing easy mocking in tests while using a real HTTP client in production.

Configuration

The HTTP client implementation can be configured in config.exs:

config :codex_sdk, :http_client_impl, Codex.HTTPClient.Req

For testing, use the mock implementation:

config :codex_sdk, :http_client_impl, Codex.HTTPClient.Mock

Usage

# GET request
{:ok, response} = Codex.HTTPClient.get("https://api.example.com/data", [{"Authorization", "Bearer token"}])
response.status  # => 200
response.body    # => "{...}"

# POST request
{:ok, response} = Codex.HTTPClient.post("https://api.example.com/data", body, [{"Content-Type", "application/json"}])

Summary

Functions

Performs an HTTP GET request.

Performs an HTTP POST request.

Types

headers()

@type headers() :: [{String.t(), String.t()}]

response()

@type response() :: %{status: integer(), body: binary() | map()}

Callbacks

get(url, headers)

@callback get(url :: String.t(), headers :: headers()) ::
  {:ok, response()} | {:error, term()}

post(url, body, headers)

@callback post(url :: String.t(), body :: String.t(), headers :: headers()) ::
  {:ok, response()} | {:error, term()}

Functions

get(url, headers \\ [])

@spec get(String.t(), headers()) :: {:ok, response()} | {:error, term()}

Performs an HTTP GET request.

Parameters

  • url - The URL to request
  • headers - List of header tuples (optional, defaults to [])

Returns

  • {:ok, %{status: integer(), body: binary()}} on success
  • {:error, reason} on failure

post(url, body, headers \\ [])

@spec post(String.t(), String.t(), headers()) :: {:ok, response()} | {:error, term()}

Performs an HTTP POST request.

Parameters

  • url - The URL to request
  • body - The request body as a string (usually JSON)
  • headers - List of header tuples (optional, defaults to [])

Returns

  • {:ok, %{status: integer(), body: binary()}} on success
  • {:error, reason} on failure