deleteAll method

Future<RestResponse> deleteAll(
  1. String apiEndPoint,
  2. {Map<String, String> headers,
  3. List<Map<String, dynamic>> body,
  4. Map<String, String> queryParameters,
  5. Encoding encoding,
  6. String basePath,
  7. String protocol,
  8. String hosting,
  9. int hostPort}
)

Implementation

Future<RestResponse> deleteAll(
  String apiEndPoint, {
  Map<String, String> headers,
  List<Map<String, dynamic>> body,
  Map<String, String> queryParameters,
  Encoding encoding,
  String basePath,
  String protocol,
  String hosting,
  int hostPort,
}) async {
  var url = uri(
    apiEndPoint,
    queryParameters: queryParameters,
    host: hosting,
    basePath: basePath,
    protocol: protocol,
    port: hostPort,
  );

  headers ??= headersDefault;

  var request = HttpRequest();
  request.open('delete', url.toString());
  request.setRequestHeader('Content-Type', 'application/json');
  request.send(json.encode(body));

  await request.onLoadEnd.first;
  //await request.onReadyStateChange.first;

  var jsonDecoded = jsonDecode(request.responseText);
  if (request.status == 200) {
    return RestResponse(
        message: 'Sucesso',
        status: RestStatus.SUCCESS,
        data: jsonDecode(request.responseText),
        statusCode: request.status);
  }
  var message = '${request.responseText}';
  var exception = '${request.responseText}';
  //exibe mensagem se der erro não autorizado
  if (request.status == 401) {
    if (jsonDecoded is Map) {
      if (jsonDecoded.containsKey('message')) {
        message = jsonDecoded['message'];
      }
      if (jsonDecoded.containsKey('exception')) {
        exception = jsonDecoded['exception'];
      }
    }

    return RestResponse(message: message, status: RestStatus.UNAUTHORIZED, statusCode: request.status);
  }
  //

  if (jsonDecoded is Map) {
    if (jsonDecoded.containsKey('message')) {
      message = jsonDecoded['message'];
    }
    if (jsonDecoded.containsKey('exception')) {
      exception = jsonDecoded['exception'];
    }
  }

  return RestResponse(message: message, exception: exception, status: RestStatus.DANGER, statusCode: request.status);
}