callService static method

Future<Response> callService({
  1. required APIRequestInfoObj requestInfo,
})

Makes an API call based on the provided request information.

This method determines the type of API call (multipart or regular) based on the request information. It also handles various exceptions like no internet connectivity, HTTP errors, and request timeouts.

Implementation

static Future<http.Response> callService({
  required APIRequestInfoObj requestInfo,
}) async {
  try {
    // Check for Internet connectivity before making the API call.
    await _checkConnectivity();

    // Print the API request details for debugging purposes.
    _printApiDetail(requestInfo);

    // Determine the type of API call (multipart or regular) and make the call.
    return requestInfo.docList.isEmpty
        ? await _callAPI(requestInfo: requestInfo)
            .timeout(Duration(seconds: requestInfo.timeSecond))
        : await _callMultipartAPI(requestInfo: requestInfo)
            .timeout(Duration(seconds: requestInfo.timeSecond));
  } on SocketException catch (e) {
    // Handle exceptions related to no internet connectivity.
    throw AppException(
      message: e.message,
      type: ExceptionType.noInternet,
    );
  } on HttpException catch (e) {
    // Handle generic HTTP exceptions.
    throw AppException(
      message: e.message,
      type: ExceptionType.httpException,
    );
  } on FormatException catch (e) {
    // Handle exceptions related to response format errors.
    throw AppException(
      message: e.source?.toString(),
      type: ExceptionType.formatException,
    );
  } on TimeoutException {
    // Handle request timeout exceptions.
    throw AppException(
      title: ApiErrorMessage.requestTimeoutTitle,
      message: ApiErrorMessage.requestTimeoutMessage,
      type: ExceptionType.timeOut,
    );
  } catch (error) {
    // Rethrow any other unhandled exceptions.
    rethrow;
  }
}