sseResponse static method

StreamedApiResponse sseResponse(
  1. List<String> jsonEvents, {
  2. int statusCode = 200,
  3. String doneSignal = '[DONE]',
})

Create a StreamedApiResponse that emits SSE events from JSON strings.

client.nextStreamedResponse = RecordingClient.sseResponse([
  '{"id":"1","content":"Hello"}',
  '{"id":"2","content":" world"}',
]);

Implementation

static StreamedApiResponse sseResponse(
  List<String> jsonEvents, {
  int statusCode = 200,
  String doneSignal = '[DONE]',
}) {
  final chunks = <List<int>>[];
  for (final event in jsonEvents) {
    chunks.add(utf8.encode('data: $event\n\n'));
  }
  chunks.add(utf8.encode('data: $doneSignal\n\n'));
  return StreamedApiResponse(
    statusCode: statusCode,
    headers: const {'content-type': 'text/event-stream'},
    byteStream: Stream.fromIterable(chunks),
  );
}