prepareResponse method

Response prepareResponse(
  1. Response res, {
  2. ResponseType? responseType,
})
inherited

Implementation

Response<dynamic> prepareResponse(http.Response res,
    {ResponseType? responseType}) {
  responseType ??= ResponseType.json;
  if (res.statusCode >= 400) {
    if ((res.headers['content-type'] ?? '').contains('application/json')) {
      final response = json.decode(res.body);
      throw TombaException(
        response['errors']['message'] as String?,
        res.statusCode,
        response,
      );
    } else {
      throw TombaException(res.body);
    }
  }
  dynamic data;
  if ((res.headers['content-type'] ?? '').contains('application/json')) {
    if (responseType == ResponseType.json) {
      data = json.decode(res.body);
    } else if (responseType == ResponseType.bytes) {
      data = res.bodyBytes;
    } else {
      data = res.body;
    }
  } else {
    if (responseType == ResponseType.bytes) {
      data = res.bodyBytes;
    } else {
      data = res.body;
    }
  }
  final rateLimit = RateLimit.fromHeaders(res.headers);
  return Response<dynamic>(data: data, rateLimit: rateLimit);
}