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
@type stream_callback() :: (stream_event() -> :ok | :stop)
Functions
@spec stream_sse( String.t(), [{String.t(), String.t()}], map() | nil, stream_callback(), keyword() ) :: {:ok, :completed} | {:error, term()}
Start an SSE stream with a callback function.
Parameters
url- Full URL for the streaming endpointheaders- HTTP headers including authenticationbody- Request body (will be JSON encoded)callback- Function called for each eventopts- Options including timeout, retry settings:timeout- Receive timeout per attempt (default:Gemini.Config.timeout/0):max_retries- Number of retry attempts (default: 3):max_backoff_ms- Max backoff between retries (default: 10_000):connect_timeout- Finch connect timeout (default: 5_000)
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)
@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}