send<ResultType, InnerType> method

  1. @override
Future<ApiResponse<ResultType, InnerType>> send<ResultType, InnerType>(
  1. ApiRequest<ResultType, InnerType> request
)
override

Implementation

@override
Future<ApiResponse<ResultType, InnerType>> send<ResultType, InnerType>(
    ApiRequest<ResultType, InnerType> request) async {
  request = request.build;

  http.Response response;
  // print('handling request ${request.baseUrl}');
  try {
    if (request.isMultipart) {
      var res = await handleMultipart(request);
      return ApiResponse<ResultType, InnerType>(
              request: request,
              bodyString: await res.stream.bytesToString(),
              headers: res.headers,
              statusCode: res.statusCode)
          .resolve;
    }
    response = await runRequest(request)
        .timeout(Duration(seconds: request.timeout), onTimeout: () {
      throw (TimeoutException('Connection timed out'));
    });
  } catch (e) {
    if (kDebugMode) print(' error in request ');
    if (kDebugMode) print(e.toString());
    return ApiResponse<ResultType, InnerType>(
            request: request,
            headers: {},
            statusCode: 400,
            error: (e is ApiError)
                ? e
                : formatErrorMessage(e, ApiConfig().defaultErrorMessage))
        .resolve;
  }

  // print(response?.bodyBytes);

  return ApiResponse<ResultType, InnerType>(
          request: request,
          bodyString: response.body,
          headers: response.headers,
          statusCode: response.statusCode)
      .resolve;
}