send method

  1. @override
AWSHttpOperation<AWSBaseHttpResponse> send(
  1. AWSBaseHttpRequest request, {
  2. FutureOr<void> onCancel()?,
})

Sends request using the underlying HTTP protocol and returns the streaming response.

Implementation

@override
AWSHttpOperation send(
  AWSBaseHttpRequest request, {
  FutureOr<void> Function()? onCancel,
}) {
  final requestProgress = StreamController<int>.broadcast();
  final responseProgress = StreamController<int>.broadcast();
  var isCancelled = false;
  return AWSHttpOperation(
    CancelableOperation.fromFuture(
      Future(() async {
        final readRequest = await request.read();
        requestProgress.add(readRequest.bodyBytes.length);
        unawaited(requestProgress.close());
        final response = await _handler(
          readRequest,
          () => isCancelled,
        );
        if (response is AWSHttpResponse) {
          responseProgress.add(response.bodyBytes.length);
          unawaited(responseProgress.close());
          return response;
        }
        return AWSStreamedHttpResponse(
          statusCode: response.statusCode,
          headers: response.headers,
          body: response.body.tap(
            (event) => responseProgress.add(event.length),
            onDone: responseProgress.close,
          ),
        );
      }),
      onCancel: () {
        isCancelled = true;
        requestProgress.close();
        responseProgress.close();
        return onCancel?.call();
      },
    ),
    requestProgress: requestProgress.stream,
    responseProgress: responseProgress.stream,
    onCancel: onCancel,
  );
}