diff --git a/alephclient/api.py b/alephclient/api.py index e11aa70..8cd19b9 100644 --- a/alephclient/api.py +++ b/alephclient/api.py @@ -226,6 +226,11 @@ def get_entity(self, entity_id: str, publisher: bool = False) -> Dict: entity = self._request("GET", url) return self._patch_entity(entity, publisher) + def delete_entity(self, entity_id: str) -> Dict: + """Delete a single entity by ID.""" + url = self._make_url(f"entities/{entity_id}") + return self._request("DELETE", url) + def get_collection_by_foreign_id(self, foreign_id: str) -> Optional[Dict]: """Get a dict representing a collection based on its foreign ID.""" if foreign_id is None: diff --git a/alephclient/cli.py b/alephclient/cli.py index 8fcc0d1..1631292 100644 --- a/alephclient/cli.py +++ b/alephclient/cli.py @@ -237,6 +237,19 @@ def read_json_stream(stream): raise click.Abort() +@cli.command("delete-entity") +@click.argument("entity_id", required=True) +@click.pass_context +def delete_entity(ctx, entity_id): + """Delete a single entity from standard input""" + api = ctx.obj['api'] + entity_id = entity_id.strip() + try: + api.delete_entity(entity_id) + except Exception as exc: + raise click.ClickException(exc.message) + + @cli.command("write-entities") @click.option("-i", "--infile", type=click.File("r"), default="-") @click.option("-f", "--foreign-id", required=True, help="foreign_id of the collection") diff --git a/alephclient/tests/test_tasks.py b/alephclient/tests/test_tasks.py index 22dab28..6fa837d 100644 --- a/alephclient/tests/test_tasks.py +++ b/alephclient/tests/test_tasks.py @@ -4,7 +4,6 @@ from alephclient.crawldir import crawl_dir from alephclient.api import AlephAPI - class TestTasks(object): def setup_method(self): self.api = AlephAPI(host="http://aleph.test/api/2/", api_key="fake_key") @@ -43,6 +42,27 @@ def test_write_entity(self, mocker): res = self.api.write_entity(collection_id, entity) assert res["id"] == 24 + + def test_delete_entity(self, mocker): + mocker.patch.object(self.api, "write_entity", return_value={"id": 24}) + collection_id = 8 + entity = { + "id": 24, + "schema": "Article", + "properties": { + "title": "", + "author": "", + "publishedAt": "", + "bodyText": "", + }, + } + + res = self.api.write_entity(collection_id, entity) + assert res['id'] == 24 + dres = self.api.delete_entity(eid) + assert dres == {} + + def test_ingest(self, mocker): mocker.patch.object(self.api, "ingest_upload", return_value={"id": 42}) mocker.patch.object(