delete method

Future<PlexApiResult> delete(
  1. String endpoint, {
  2. Map<String, dynamic>? queryParams,
  3. Map<String, String>? headers,
  4. dynamic body,
  5. bool useFullUrl = false,
})

Makes a DELETE request to the specified endpoint

endpoint - The API endpoint (will be appended to the base URL) queryParams - Optional query parameters to append to the URL headers - Optional headers to include in the request body - Optional request body (will be serialized to JSON) useFullUrl - If true, endpoint is treated as a complete URL

Returns a Future<PlexApiResult> containing the response

Implementation

Future<PlexApiResult> delete(
  String endpoint, {
  Map<String, dynamic>? queryParams,
  Map<String, String>? headers,
  dynamic body,
  bool useFullUrl = false,
}) async {
  final url = useFullUrl ? endpoint : endpoint;

  // Create headers with standard content type if not specified
  final Map<String, String> requestHeaders = headers ?? {};
  if (!requestHeaders.containsKey("Content-Type")) {
    requestHeaders["Content-Type"] = "application/json";
  }

  final uri = Uri.parse(url);
  http.Response response;

  try {
    if (await PlexNetworking.instance.isNetworkAvailable() == false) {
      return PlexApiResult(false, 5001, 'Network not available', null);
    }

    // Apply standard headers from callback if available
    if (PlexNetworking.instance.addHeaders != null) {
      final standardHeaders = await PlexNetworking.instance.addHeaders!();
      requestHeaders.addAll(standardHeaders);
    }

    if (kDebugMode) print("DELETE Started: ${uri.toString()}");
    final startTime = DateTime.now();

    if (body != null) {
      response = await http.delete(
        uri,
        headers: requestHeaders,
        body: jsonEncode(body),
      );
    } else {
      response = await http.delete(
        uri,
        headers: requestHeaders,
      );
    }

    final diffInMillis = DateTime.now().difference(startTime).inMilliseconds;
    if (kDebugMode) print("DELETE Completed: ${response.statusCode}: ${uri.toString()} in ${diffInMillis}ms");

    // Parse response
    if (response.statusCode >= 200 && response.statusCode < 300) {
      dynamic responseData;
      try {
        responseData = jsonDecode(response.body);
      } catch (e) {
        responseData = response.body;
      }
      return PlexApiResult(true, response.statusCode, 'Success', responseData);
    } else {
      return PlexApiResult(false, response.statusCode, response.body.isEmpty ? response.reasonPhrase ?? 'Error' : response.body, null);
    }
  } catch (e) {
    if (kDebugMode) print("DELETE Error: ${e.toString()}");
    if (e is SocketException) {
      return PlexApiResult(false, 5002, 'Network Available But Unable To Connect With Server', null);
    }
    return PlexApiResult(false, 400, e.toString(), null);
  }
}