send<T> method

Future<HtpioResponse<T>> send<T>(
  1. HtpioRequest<T> request
)

Implementation

Future<HtpioResponse<T>> send<T>(HtpioRequest<T> request) async {
  try {
    _debug.log('Sending request to ${request.url}');

    // Process middlewares before the request
    for (final middleware in _middlewares) {
      await middleware.beforeRequest(request);
    }

    // Process interceptors before the request
    HtpioRequest<T> processedRequest = request;
    for (final interceptor in _interceptors) {
      processedRequest = await interceptor.onRequest(processedRequest) as HtpioRequest<T>;
    }

    // Execute the actual HTTP request
    final response = await processedRequest.execute();

    // Process interceptors after the response
    HtpioResponse processedResponse = response;
    for (final interceptor in _interceptors.reversed) {
      processedResponse = await interceptor.onResponse(processedResponse);
    }

    // Process middlewares after the response
    for (final middleware in _middlewares.reversed) {
      await middleware.afterResponse(processedResponse);
    }

    return processedResponse as HtpioResponse<T>;
  } catch (error) {
    final wrapped = HtpioError.from(error);
    _debug.log('Error: ${wrapped.message}');
    rethrow;
  }
}