postChatStream<T> static method

Stream<T> postChatStream<T>({
  1. required String fullPath,
  2. required String apiKey,
  3. required T onSuccess(
    1. ChatStreamResponse
    ),
  4. required T onError(
    1. String
    ),
  5. required Map<String, dynamic> body,
})

Implementation

static Stream<T> postChatStream<T>({
  required String fullPath,
  required String apiKey,
  required T Function(ChatStreamResponse) onSuccess,
  required T Function(String) onError,
  required Map<String, dynamic> body,
}) {
  final controller = StreamController<T>();
  final uri = Uri.parse(fullPath);
  final headers = {
    'apiKey': apiKey,
    'Content-Type': 'application/json',
  };

  _streamingHttpClient()
      .send(http.Request('POST', uri)
        ..headers.addAll(headers)
        ..body = jsonEncode(body))
      .then((response) {
    readStream(response.stream, controller, onSuccess, onError);
  }).catchError((error) {
    onError(error.toString());
  });

  return controller.stream;
}