send<BodyType, InnerType> method

Future<Response<BodyType>> send<BodyType, InnerType>(
  1. Request request, {
  2. ConvertRequest? requestConverter,
  3. ConvertResponse? responseConverter,
})

Sends a pre-build Request, applying all provided Interceptors and Converters.

BodyType should be the expected type of the response body (e.g., String or CustomObject).

If BodyType is a List or a BuiltList, InnerType should be type of the generic parameter (e.g., convertResponse<List<CustomObject>, CustomObject>(response)).

Response<List<CustomObject>> res = await send<List<CustomObject>, CustomObject>(request);

Implementation

Future<Response<BodyType>> send<BodyType, InnerType>(
  Request request, {
  ConvertRequest? requestConverter,
  ConvertResponse? responseConverter,
}) async {
  var req = await _handleRequestConverter(request, requestConverter);
  req = await _interceptRequest(req);
  _requestController.add(req);

  final streamRes = await httpClient.send(await req.toBaseRequest());
  if (isTypeOf<BodyType, Stream<List<int>>>()) {
    return Response(streamRes, (streamRes.stream) as BodyType);
  }

  final response = await http.Response.fromStream(streamRes);
  dynamic res = Response(response, response.body);

  if (authenticator != null) {
    var updatedRequest = await authenticator!.authenticate(req, res, request);

    if (updatedRequest != null) {
      res = await send<BodyType, InnerType>(
        updatedRequest,
        requestConverter: requestConverter,
        responseConverter: responseConverter,
      );
      // To prevent double call with typed response
      if (_responseIsSuccessful(res.statusCode)) {
        return _processResponse(res);
      } else {
        res = await _handleErrorResponse<BodyType, InnerType>(res);
        return _processResponse(res);
      }
    }
  }

  if (_responseIsSuccessful(res.statusCode)) {
    res = await _handleSuccessResponse<BodyType, InnerType>(
      res,
      responseConverter,
    );
  } else {
    res = await _handleErrorResponse<BodyType, InnerType>(res);
  }

  return _processResponse(res);
}