Skip to content

Commit

Permalink
Make _convert_sync_to_async_url use more readable, fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLindblad committed Jan 28, 2025
1 parent 5c9e7a9 commit ec8f348
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/aryn-sdk/aryn_sdk/partition/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

g_version = "0.1.11"


class PartitionError(Exception):
def __init__(self, message: str, status_code: int) -> None:
super().__init__(message)
Expand Down Expand Up @@ -377,12 +378,12 @@ def partition_file_async_submit(
if async_submit_url:
docparse_url = async_submit_url
elif not aps_url and not docparse_url:
docparse_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, prefix="/submit", include_rest_of_url=True)
docparse_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, "/submit", False)
else:
if aps_url:
aps_url = _convert_sync_to_async_url(aps_url, prefix="/submit", include_rest_of_url=True)
aps_url = _convert_sync_to_async_url(aps_url, "/submit", False)
if docparse_url:
docparse_url = _convert_sync_to_async_url(docparse_url, prefix="/submit", include_rest_of_url=True)
docparse_url = _convert_sync_to_async_url(docparse_url, "/submit", False)

return _partition_file_inner(
file=file,
Expand All @@ -405,13 +406,16 @@ def partition_file_async_submit(
)


def _convert_sync_to_async_url(url: str, *, prefix: str, include_rest_of_url: bool = False) -> str:
def _convert_sync_to_async_url(url: str, prefix: str, truncate: bool) -> str:
parsed_url = urlparse(url)
assert parsed_url.path.startswith("/v1/")
if parsed_url.path.startswith("/v1/async/submit"):
return url
ary = list(parsed_url)
ary[2] = f"/v1/async{prefix}{parsed_url.path[3:] if include_rest_of_url else ''}" # path
if truncate:
ary[2] = f"/v1/async{prefix}" # path
else:
ary[2] = f"/v1/async{prefix}{parsed_url.path[3:]}" # path
return urlunparse(ary)


Expand All @@ -437,7 +441,7 @@ def partition_file_async_result(
`partition_file` had the partitioning been done synchronously.
"""
if not async_result_url:
async_result_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, prefix="/result")
async_result_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, "/result", True)

aryn_config = _process_config(aryn_api_key, aryn_config)

Expand Down Expand Up @@ -475,7 +479,7 @@ def partition_file_async_cancel(
For an example of usage see README.md
"""
if not async_cancel_url:
async_cancel_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, prefix="/cancel")
async_cancel_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, "/cancel", True)

aryn_config = _process_config(aryn_api_key, aryn_config)

Expand Down Expand Up @@ -518,7 +522,7 @@ def partition_file_async_list(
}
"""
if not async_list_url:
async_list_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, prefix="/list")
async_list_url = _convert_sync_to_async_url(ARYN_DOCPARSE_URL, "/list", True)

aryn_config = _process_config(aryn_api_key, aryn_config)

Expand Down

0 comments on commit ec8f348

Please sign in to comment.