Gemini.Client.HTTPStreaming (GeminiEx v0.0.2)

View Source

HTTP client for streaming Server-Sent Events (SSE) from Gemini API.

Provides proper streaming support with:

  • Incremental SSE parsing
  • Connection management
  • Error handling and retries
  • Backpressure support

Summary

Functions

Start an SSE stream with a callback function.

Start an SSE stream that sends events to a GenServer process.

Types

stream_callback()

@type stream_callback() :: (stream_event() -> :ok | :stop)

stream_event()

@type stream_event() :: %{
  type: :data | :error | :complete,
  data: map() | nil,
  error: term() | nil
}

Functions

stream_sse(url, headers, body, callback, opts \\ [])

@spec stream_sse(
  String.t(),
  [{String.t(), String.t()}],
  map(),
  stream_callback(),
  keyword()
) ::
  {:ok, :completed} | {:error, term()}

Start an SSE stream with a callback function.

Parameters

  • url - Full URL for the streaming endpoint
  • headers - HTTP headers including authentication
  • body - Request body (will be JSON encoded)
  • callback - Function called for each event
  • opts - Options including timeout, retry settings

Examples

callback = fn
  %{type: :data, data: data} ->
    IO.puts("Received data")
    :ok
  %{type: :complete} ->
    IO.puts("Stream complete")
    :ok
  %{type: :error, error: _error} ->
    IO.puts("Stream error")
    :stop
end

HTTPStreaming.stream_sse(url, headers, body, callback)

stream_to_process(url, headers, body, stream_id, target_pid, opts \\ [])

@spec stream_to_process(
  String.t(),
  [{String.t(), String.t()}],
  map(),
  String.t(),
  pid(),
  keyword()
) :: {:ok, pid()} | {:error, term()}

Start an SSE stream that sends events to a GenServer process.

Events are sent as messages: {:stream_event, stream_id, event}