Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider new created status when checking if annotation is already imported #60

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions rossum_api/elis_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def __init__(
"""
Parameters
----------
base_url
base API URL including the "/api" and version ("/v1") in the url path. For example
"https://elis.rossum.ai/api/v1"
deserializer
pass a custom deserialization callable if different model classes should be returned
"""
Expand Down Expand Up @@ -97,19 +100,19 @@ async def import_document(
) -> List[int]:
"""https://elis.rossum.ai/api/docs/#import-a-document.

arguments
Parameters
---------
files
2-tuple containing current filepath and name to be used by Elis for the uploaded file
metadata
metadata will be set to newly created annotation object
values
may be used to initialize datapoint values by setting the value of rir_field_names in the schema
files
2-tuple containing current filepath and name to be used by Elis for the uploaded file
metadata
metadata will be set to newly created annotation object
values
may be used to initialize datapoint values by setting the value of rir_field_names in the schema

Returns
-------
annotation_ids
list of IDs of created annotations, respects the order of `files` argument
annotation_ids
list of IDs of created annotations, respects the order of `files` argument
"""
tasks = [
asyncio.create_task(self._upload(file, queue_id, filename, values, metadata))
Expand Down Expand Up @@ -309,7 +312,7 @@ async def poll_annotation_until_imported(
) -> Annotation:
"""A shortcut for waiting until annotation is imported."""
return await self.poll_annotation(
annotation_id, lambda a: a.status != "importing", **poll_kwargs
annotation_id, lambda a: a.status not in ("importing", "created"), **poll_kwargs
)

async def upload_and_wait_until_imported(
Expand Down Expand Up @@ -376,11 +379,13 @@ async def create_new_document(
) -> Document:
"""https://elis.rossum.ai/api/docs/#create-document"""
metadata = metadata or {}
files = {
files: httpx._types.RequestFiles = {
"content": (file_name, file_data),
"metadata": ("", json.dumps(metadata).encode("utf-8")),
"parent": ("", parent),
}
if parent:
files["parent"] = ("", parent)

document = await self._http_client.request_json(
"POST", url=Resource.Document.value, files=files
)
Expand Down Expand Up @@ -497,7 +502,7 @@ async def request(self, method: str, *args, **kwargs) -> httpx.Response:
async def get_token(self, refresh: bool = False) -> str:
"""Returns the current token. Authentication is done automatically if needed.

Arguments:
Parameters
----------
refresh
force refreshing the token
Expand Down
29 changes: 19 additions & 10 deletions rossum_api/elis_api_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def __init__(
http_client: Optional[APIClient] = None,
deserializer: Optional[Deserializer] = None,
):
"""
Parameters
----------
base_url
base API URL including the "/api" and version ("/v1") in the url path. For example
"https://elis.rossum.ai/api/v1"
deserializer
pass a custom deserialization callable if different model classes should be returned
"""
self.elis_api_client = ElisAPIClient(
username, password, token, base_url, http_client, deserializer
)
Expand Down Expand Up @@ -114,19 +123,19 @@ def import_document(
) -> List[int]:
"""https://elis.rossum.ai/api/docs/#import-a-document.

arguments
Parameters
---------
files
2-tuple containing current filepath and name to be used by Elis for the uploaded file
metadata
metadata will be set to newly created annotation object
values
may be used to initialize datapoint values by setting the value of rir_field_names in the schema
files
2-tuple containing current filepath and name to be used by Elis for the uploaded file
metadata
metadata will be set to newly created annotation object
values
may be used to initialize datapoint values by setting the value of rir_field_names in the schema

Returns
-------
annotation_ids
list of IDs of created annotations, respects the order of `files` argument
annotation_ids
list of IDs of created annotations, respects the order of `files` argument
"""
return self.event_loop.run_until_complete(
self.elis_api_client.import_document(queue_id, files, values, metadata)
Expand Down Expand Up @@ -441,7 +450,7 @@ def request(self, method: str, *args, **kwargs) -> httpx.Response:
def get_token(self, refresh: bool = False) -> str:
"""Returns the current token. Authentication is done automatically if needed.

Arguments:
Parameters
----------
refresh
force refreshing the token
Expand Down
4 changes: 3 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ async def main_with_async_client():
)

print("Polling until annotation is ready to review...")
annotation = await client.poll_annotation(annotation_id, lambda a: a.status != "importing")
annotation = await client.poll_annotation(
annotation_id, lambda a: a.status not in ("importing", "created")
)
print(f"Annotation ready to review: {annotation}")

# Cleanup
Expand Down
4 changes: 2 additions & 2 deletions tests/elis_api_client/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ async def test_retrieve_annotation_with_sideloads(self, elis_client, dummy_annot

async def test_poll_annotation(self, elis_client, dummy_annotation):
def is_imported(annotation):
return annotation.status != "importing"
return annotation.status not in ("importing", "created")

client, http_client = elis_client
in_progress_annotation = {**dummy_annotation, "status": "importing"}
Expand Down Expand Up @@ -464,7 +464,7 @@ def test_retrieve_annotation_with_sideloads(self, elis_client_sync, dummy_annota

def test_poll_annotation(self, elis_client_sync, dummy_annotation):
def is_imported(annotation):
return annotation.status != "importing"
return annotation.status not in ("importing", "created")

client, http_client = elis_client_sync
in_progress_annotation = {**dummy_annotation, "status": "importing"}
Expand Down
Loading