request method

Future<Response?> request({
  1. required String url,
  2. required String method,
  3. Map<String, dynamic>? body,
  4. Map<String, dynamic>? headers,
  5. Options? options,
})

Implementation

Future<Response?> request({
  required String url,
  required String method,
  Map<String, dynamic>? body,
  Map<String, dynamic>? headers,
  Options? options,
}) async {
  try {
    headers = headers ?? {};
    options = options ?? Options(headers: headers);

    List<String>? savedCookies = await sessionManager?.getSessionCookies();
    if (savedCookies!.isNotEmpty) {
      headers['Cookie'] = savedCookies.join('; ');
    }

    Response response;

    switch (method.toUpperCase()) {
      case 'POST':
        response = await _dio.post(url, data: body, options: options);
        break;
      case 'GET':
        response = await _dio.get(url, options: options);
        break;
      case 'PUT':
        response = await _dio.put(url, data: body, options: options);
        break;
      case 'PATCH':
        response = await _dio.patch(url, data: body, options: options);
        break;
      case 'DELETE':
        response = await _dio.delete(url, data: body, options: options);
        break;
      default:
        throw UnsupportedError("Method not supported: $method");
    }
    _storeResponseCookies(response);
    return response;
  } catch (e) {
    if (kDebugMode) {
      print("Error during network request: $e");
    }
    return null;
  }
}