send method

  1. @override
Future<StreamedResponse> send(
  1. BaseRequest request
)

Sends an HTTP request and asynchronously returns the response.

Implementers should call BaseRequest.finalize to get the body of the request as a ByteStream. They shouldn't make any assumptions about the state of the stream; it could have data written to it asynchronously at a later point, or it could already be closed when it's returned. Any internal HTTP errors should be wrapped as ClientExceptions.

Implementation

@override
Future<StreamedResponse> send(BaseRequest request) async {
  if (request is! Request) return inner.send(request);
  final repeatable = _isRepeatable(request);
  var retries = 0;
  var inProgress = 0;
  while (true) {
    StreamedResponse response;
    try {
      response = await inner.send(_copy(request));
    } catch (error) {
      if (!repeatable || !_isNetworkError(error) || retries >= options.maxRetries) rethrow;
      retries++;
      await _wait(retries + inProgress, 'network error: ${error.runtimeType}', _backoff(retries, null));
      continue;
    }

    final status = response.statusCode;
    if (repeatable && (status == 202 || status == 409)) {
      final bytes = await response.stream.toBytes();
      if (_errorCode(bytes) == idempotencyKeyInProgressCode && inProgress < options.inProgressRetries) {
        inProgress++;
        final wait = parseRetryAfter(response.headers['retry-after']) ?? _inProgressDefaultWait;
        await _wait(retries + inProgress, '$status in progress ($idempotencyKeyInProgressCode)', _cap(wait));
        continue;
      }
      return _buffered(response, bytes);
    }

    if (status == 429 || (repeatable && _retryableStatuses.contains(status))) {
      if (retries >= options.maxRetries) return response;
      retries++;
      final delay = _backoff(retries, parseRetryAfter(response.headers['retry-after']));
      await _discard(response);
      await _wait(retries + inProgress, 'status $status', delay);
      continue;
    }

    return response;
  }
}