call method

Future call(
  1. String path, {
  2. dynamic xmlData,
  3. Map<String, dynamic>? params,
  4. ApiMethod method = ApiMethod.GET,
})

call method make the ApiService callable You can call the method as: api('/path') method default is GET

Implementation

Future<dynamic> call(
  String path, {
  dynamic xmlData,
  Map<String, dynamic>? params,
  ApiMethod method = ApiMethod.GET,
}) async {
  try {
    Response response;

    switch (method) {
      case ApiMethod.GET:
        response = await _dio.get(path);
        break;
      case ApiMethod.POST:
        response = await _dio.post(path, data: xmlData ?? params);
        break;
      case ApiMethod.PUT:
        response = await _dio.put(path, data: params);
        break;
      case ApiMethod.DELETE:
        response = await _dio.delete(
          path,
        );
        break;
    }

    if (response.statusCode == 200) {
      return response.data ?? true;
    }
  } on DioError catch (e) {
    throw 'error ${e.response?.statusCode}';
  } catch (e) {
    throw 'generic error';
  } finally {}

  return null;
}