Pristine.Core.StreamResponse (Pristine v0.3.0)

Copy Markdown View Source

Response wrapper for streaming HTTP responses.

Contains an enumerable stream of events instead of a complete body. This is used for SSE (Server-Sent Events) and other streaming responses.

Fields

  • :stream - An enumerable that yields events (required)
  • :status - HTTP status code (required)
  • :headers - Response headers as a map (required)
  • :metadata - Additional metadata (optional, defaults to empty map)

Example

%StreamResponse{
  stream: events,
  status: 200,
  headers: %{"content-type" => "text/event-stream"},
  metadata: %{request_id: "req_123"}
}

Usage

The stream can be consumed lazily:

{:ok, response} = StreamTransport.stream(request, context)

response.stream
|> Stream.each(fn event ->
  IO.puts("Got event: #{inspect(event)}")
end)
|> Stream.run()

Or collected into a list:

events = Enum.to_list(response.stream)

Summary

Functions

Cancel the underlying stream when supported by the transport.

Dispatch a single event to a handler module based on its type.

Map a stream of events through a handler module.

Return the latest Last-Event-ID value when available.

Types

t()

@type t() :: %Pristine.Core.StreamResponse{
  headers: map(),
  metadata: map(),
  status: integer(),
  stream: Enumerable.t()
}

Functions

cancel(arg1)

@spec cancel(t()) :: :ok

Cancel the underlying stream when supported by the transport.

dispatch_event(event, handler_module)

@spec dispatch_event(Pristine.Streaming.Event.t(), module()) ::
  {:ok, term()} | {:error, term()} | term()

Dispatch a single event to a handler module based on its type.

dispatch_stream(stream_response, handler_module)

@spec dispatch_stream(t(), module()) :: Enumerable.t()

Map a stream of events through a handler module.

last_event_id(arg1)

@spec last_event_id(t()) :: String.t() | nil

Return the latest Last-Event-ID value when available.