-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Extract URL domain logic into a reusable module
- Loading branch information
Showing
5 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from rossum_api.models import Resource | ||
|
||
DEFAULT_BASE_URL = "https://elis.rossum.ai/api/v1" | ||
|
||
|
||
def parse_resource_id_from_url(url: str) -> int: | ||
# Annotation content resource is special, we need to stirp /content suffix | ||
return int(url.replace("/content", "").split("/")[-1]) | ||
|
||
|
||
def parse_annotation_id_from_datapoint_url(url: str) -> int: | ||
# URL format: .../annotation/<annotation ID>/content/<datapoint ID> | ||
# Remove the /content/<datapoint ID> from the URL and then pass it to the generic function. | ||
return parse_resource_id_from_url(re.sub(r"/content/.*", "", url)) | ||
|
||
|
||
def build_url(resource: Resource, id_: int) -> str: | ||
return f"{resource.value}/{id_}" | ||
|
||
|
||
def build_export_url(resource: Resource, id_: int) -> str: | ||
return f"{build_url(resource, id_)}/export" | ||
|
||
|
||
def build_upload_url(resource: Resource, id_: int) -> str: | ||
return f"{build_url(resource, id_)}/upload" | ||
|
||
|
||
def build_full_login_url(base_url: str) -> str: | ||
return f"{base_url}/auth/login" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from rossum_api.domain_logic.resources import Resource | ||
from rossum_api.domain_logic.urls import ( | ||
build_export_url, | ||
build_full_login_url, | ||
build_upload_url, | ||
build_url, | ||
parse_annotation_id_from_datapoint_url, | ||
parse_resource_id_from_url, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"url, expected_id", | ||
[ | ||
("https://elis.rossum.ai/api/v1/queues/8199", 8199), | ||
("https://elis.rossum.ai/api/v1/annotations/314521/content", 314521), | ||
], | ||
) | ||
def test_parse_resource_id_from_url(url, expected_id): | ||
assert parse_resource_id_from_url(url) == expected_id | ||
|
||
|
||
def test_parse_annotation_id_from_datapoint_url(): | ||
assert ( | ||
parse_annotation_id_from_datapoint_url( | ||
"https://elis.rossum.ai/api/v1/annotations/314521/content/1123123" | ||
) | ||
== 314521 | ||
) | ||
|
||
|
||
def test_build_url(): | ||
assert build_url(Resource.Queue, 123) == "queues/123" | ||
|
||
|
||
def test_build_full_login_url(): | ||
assert ( | ||
build_full_login_url("https://elis.rossum.ai/api/v1") | ||
== "https://elis.rossum.ai/api/v1/auth/login" | ||
) | ||
|
||
|
||
def test_build_upload_url(): | ||
assert build_upload_url(Resource.Queue, 123) == "queues/123/upload" | ||
|
||
|
||
def test_build_export_url(): | ||
assert build_export_url(Resource.Queue, 123) == "queues/123/export" |