diff --git a/.gitignore b/.gitignore index c2dc7bd..2dd119c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ venv/ .gitignore .DS_Store .auth.json -node_modules \ No newline at end of file +node_modules +api-tools/delete-entities/onfleet_delete_mcp.py \ No newline at end of file diff --git a/api-tools/delete-entities/README.md b/api-tools/delete-entities/README.md index 062f234..1519308 100644 --- a/api-tools/delete-entities/README.md +++ b/api-tools/delete-entities/README.md @@ -1,71 +1,133 @@ -# Delete Multiple Entities +# Delete Multiple Entities -Delete Multiple Entities with a loop, this could be tasks or drivers, any allowed deletions. +Delete multiple Onfleet entities (workers, tasks, admins, teams, or webhooks) in bulk via an interactive web UI or directly via the command-line script. + +This tool is part of the [Onfleet Developer open source repository](https://github.com/onfleet/developer) — a collection of resources and tools to augment and accelerate custom integrations with the Onfleet API. + +> **Note:** All scripts require Python 3. ## Table of Contents -- [Installation](#installation) - [Disclaimer](#disclaimer) -- [Usage](#usage) -- [Configuration](#configuration) -- [Template File](#template-file) -- [Output](#output) +- [Files](#files) +- [Web UI (recommended)](#web-ui-recommended) + - [Installation](#installation) + - [Running locally](#running-locally) + - [Usage](#usage) +- [Command-line script](#command-line-script) - [Dependencies](#dependencies) +- [Related resources](#related-resources) -## Installation +--- -1. Clone the repository: +## Disclaimer -``` +**This tool does not have a rollback function. Any deletion cannot be reversed.** +Please double-check your inputs before executing. Onfleet is not responsible for any loss of data resulting from the use of this tool. -git clone https://github.com/onfleet/developer.git +--- +## Files -``` -2. Install the required dependencies: +| File | Description | +|------|-------------| +| `app.py` | Flask web server — serves the UI and proxies delete requests to the Onfleet API | +| `templates/index.html` | Browser-based UI | +| `deletion.py` | Original command-line script for manual use | -``` - -pip install requests -pip install datetime +--- -``` -## Disclaimer +## Web UI (recommended) -This script does not have a roll back function. Any deletion using this script cannot be reversed. Please check before you execute this script. Onfleet is not responsible for any loss of data by using this script. +### Installation -## Usage +1. Clone the repository and navigate to this folder: -1. Open the `deletion.py` file and fill in the necessary line with your info. - -2. Replace `ENTITY` with the entity you are accessing on line 6. Replace `USERNAME` with your actual Onfleet API Key on line 9. Include an array of Onfleet IDs you wish to delete `` +```bash +git clone https://github.com/onfleet/developer.git +cd developer/api-tools/delete-entities +``` -3. Run the program: +2. (Optional but recommended) Create and activate a virtual environment: +```bash +python3 -m venv venv +source venv/bin/activate # macOS/Linux +venv\Scripts\activate # Windows ``` -python deletion.py +3. Install dependencies: +```bash +pip install -r requirements.txt ``` -4. Check the console output for any errors encountered during deletion. +### Running locally -## Configuration +```bash +python app.py +``` -To configure the program, you need to provide your Onfleet API key. Replace `USERNAME` on line 9 of the program with your actual API key. If needed, [here](https://support.onfleet.com/hc/en-us/articles/360045763292-API#h_01FTGN2E1AGNAA4DB3Q2RPVWD9) is more information on how to create an API key in Onfleet +Then open your browser and go to: -## Template File +``` +http://127.0.0.1:5000 +``` -## Output +> **Debug mode:** Debug mode is **off by default**. Never enable it on a shared or internet-facing machine — it exposes an interactive Python debugger that allows arbitrary code execution. To turn it on temporarily during local development only: +> +> ```bash +> # macOS/Linux +> FLASK_DEBUG=1 python app.py +> +> # Windows (Command Prompt) +> set FLASK_DEBUG=1 && python app.py +> +> # Windows (PowerShell) +> $env:FLASK_DEBUG="1"; python app.py +> ``` +> +> Leave `FLASK_DEBUG` unset (or set to `0`) for normal use. + +### Usage + +1. **API Key** — Enter your Onfleet API key. For help creating one, see the [Onfleet API key guide](https://support.onfleet.com/hc/en-us/articles/360045763292-API#h_01FTGN2E1AGNAA4DB3Q2RPVWD9). +2. **Entity Type** — Select the type of entity to delete: Workers, Tasks, Admins, Teams, or Webhooks. +3. **IDs to Delete** — Paste one entity ID per line. +4. Click **Delete** — all fields are validated before submission. A confirmation dialog will appear reminding you that deletions are permanent. +5. Results stream in live as each deletion is processed. Any errors are shown inline without stopping the remaining deletions. +6. A final summary shows total succeeded and failed counts. + +--- + +## Command-line script + +If you prefer to run without the UI, edit `deletion.py` directly: + +1. Set `ENTITY` to the entity type (e.g. `tasks`, `workers`). +2. Set `USERNAME` to your Onfleet API key. +3. Populate `id_list` with the IDs to delete. +4. Run: + +```bash +pip install requests +python deletion.py +``` -ID that has been deleted or encountered any errors from the Onfleet API response. +--- ## Dependencies -The program relies on the following dependencies: +| Package | Purpose | +|---------|---------| +| `flask` | Local web server and UI | +| `requests` | HTTP requests to the Onfleet API | -- `requests`: Python's built-in module for simple HTTP requests. -- `datetime`: Python's built-in module for working with dates and times. +--- +## Related resources -Make sure you have these dependencies installed before running the program. \ No newline at end of file +- [Onfleet API documentation](https://docs.onfleet.com/reference#introduction) +- [Onfleet API key setup](https://support.onfleet.com/hc/en-us/articles/360045763292-API#h_01FTGN2E1AGNAA4DB3Q2RPVWD9) +- `pyonfleet` — Onfleet Python API wrapper: [repository](https://github.com/onfleet/pyonfleet) · [PyPI](https://pypi.org/project/pyonfleet/) +- `node-onfleet` — Onfleet Node.js API wrapper: [repository](https://github.com/onfleet/node-onfleet) diff --git a/api-tools/delete-entities/app.py b/api-tools/delete-entities/app.py new file mode 100644 index 0000000..1b1be1f --- /dev/null +++ b/api-tools/delete-entities/app.py @@ -0,0 +1,61 @@ +import html +import json +import os +import time + +import requests +from flask import Flask, Response, render_template, request, stream_with_context + +app = Flask(__name__) + +API_URL = "https://onfleet.com/api/v2" +VALID_ENTITIES = {"workers", "tasks", "admins", "teams", "webhooks"} + + +@app.route("/") +def index(): + return render_template("index.html") + + +@app.route("/delete", methods=["POST"]) +def delete_entities(): + data = request.get_json() + api_key = (data.get("api_key") or "").strip() + entity = (data.get("entity") or "").strip() + ids = [i.strip() for i in (data.get("ids") or []) if i.strip()] + + if not api_key: + return {"error": "API key is required."}, 400 + if entity not in VALID_ENTITIES: + return {"error": f"Invalid entity '{html.escape(entity)}'."}, 400 + if not ids: + return {"error": "No valid IDs provided."}, 400 + + def generate(): + for entity_id in ids: + try: + url = f"{API_URL}/{entity}/{entity_id}" + resp = requests.delete(url, auth=(api_key, ""), timeout=10) + resp.raise_for_status() + result = {"id": entity_id, "status": "success", "message": "Deleted successfully"} + except requests.exceptions.HTTPError as e: + status_code = e.response.status_code if e.response is not None else "?" + result = { + "id": entity_id, + "status": "error", + "message": f"HTTP {status_code}: {e.response.text if e.response is not None else str(e)}", + } + except requests.exceptions.RequestException as e: + result = {"id": entity_id, "status": "error", "message": str(e)} + + yield f"data: {json.dumps(result)}\n\n" + time.sleep(1) + + yield f"data: {json.dumps({'done': True})}\n\n" + + return Response(stream_with_context(generate()), mimetype="text/event-stream") + + +if __name__ == "__main__": + debug = os.environ.get("FLASK_DEBUG", "0") == "1" + app.run(debug=debug, use_reloader=False) diff --git a/api-tools/delete-entities/requirements.txt b/api-tools/delete-entities/requirements.txt new file mode 100644 index 0000000..30692b7 --- /dev/null +++ b/api-tools/delete-entities/requirements.txt @@ -0,0 +1,2 @@ +flask +requests diff --git a/api-tools/delete-entities/templates/index.html b/api-tools/delete-entities/templates/index.html new file mode 100644 index 0000000..32bf80f --- /dev/null +++ b/api-tools/delete-entities/templates/index.html @@ -0,0 +1,435 @@ + + + + + + Onfleet — Delete Entities + + + +
+

Delete Onfleet Entities

+ +
+ + +
API key is required.
+
+ +
+ + +
Please select an entity type.
+
+ +
+ + +
Enter one ID per line.
+
Please enter at least one valid ID.
+
+ + + +
+

Results

+
+
+
+
+ + + + + + +