send<ResultType, InnerType> method

  1. @override
Future<ApiResponse<ResultType, InnerType>> send<ResultType, InnerType>(
  1. ApiRequest<ResultType, InnerType> request, [
  2. RequestOptions options = const RequestOptions()
])
override

Implementation

@override
Future<ApiResponse<ResultType, InnerType>> send<ResultType, InnerType>(
  ApiRequest<ResultType, InnerType> request, [
  RequestOptions options = const RequestOptions(),
]) async {
  try {
    options.cancelToken?.throwIfCancelled();

    // Inside the try: building runs the onRequest interceptors, and an
    // interceptor that throws must take the same handled path as a
    // transport failure rather than escaping this provider.
    request = await request.build;

    final http.BaseRequest baseRequest = request.isMultipart
        ? await _buildMultipart(request, options)
        : _buildPlain(request);

    final seconds = options.timeout ?? request.timeout;
    final streamed = await _race(
      _client
          .send(baseRequest)
          .timeout(
            Duration(seconds: seconds),
            onTimeout: () => throw TimeoutException('Connection timed out'),
          ),
      options.cancelToken,
    );

    final body = await _race(
      _readBody(streamed, options.onReceiveProgress),
      options.cancelToken,
    );

    return await ApiResponse<ResultType, InnerType>(
      request: request,
      bodyString: body,
      headers: streamed.headers,
      statusCode: streamed.statusCode,
    ).resolve;
  } catch (e, stackTrace) {
    ApiConfig().log(
      '${request.method} ${request.uri} failed: $e\n$stackTrace',
    );
    rethrow;
  }
}