Skip to content

Commit

Permalink
Delete entity method and cli (#44)
Browse files Browse the repository at this point in the history
* Add delete-entity method to AlephAPI class

Cases arise where we need to delete specific entities in a dataset.
While the REST API supports it, this will make it a bit easier using
CLI tools and AlephAPI class in code.

* remove test crud

* Fix test crud
  • Loading branch information
brrttwrks authored May 13, 2024
1 parent 6b6efc5 commit 65c0f6a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions alephclient/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions alephclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
22 changes: 21 additions & 1 deletion alephclient/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 65c0f6a

Please sign in to comment.