send method

AWSHttpOperation<AWSBaseHttpResponse> send({
  1. AWSHttpClient? client,
  2. FutureOr<void> onCancel()?,
})

Sends the HTTP request.

If client is not provided, a short-lived one is created for this request.

Implementation

AWSHttpOperation send({
  AWSHttpClient? client,
  FutureOr<void> Function()? onCancel,
}) {
  final useClient = client ?? AWSHttpClient();

  // Closes the HTTP client, but only if we created it.
  void closeClient() {
    if (client == null) {
      useClient.close();
    }
  }

  final awsOperation = useClient.send(this, onCancel: closeClient);
  return AWSHttpOperation(
    awsOperation.operation.then(
      (resp) {
        resp.body.tap(
          null,
          // Wait until the body has been read before closing the client,
          // since chunked requests require that the client not be closed
          // until they're complete.
          onDone: closeClient,
        );
        return resp;
      },
      onError: (e, st) {
        closeClient();
        Error.throwWithStackTrace(e, st);
      },
    ),
    requestProgress: awsOperation.requestProgress,
    responseProgress: awsOperation.responseProgress,
    onCancel: () {
      closeClient();
      return onCancel?.call();
    },
  );
}