Skip to content

Commit 3be3e49

Browse files
committed
gets items
1 parent fb6e14c commit 3be3e49

File tree

2 files changed

+78
-33
lines changed

2 files changed

+78
-33
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import re
2+
from typing import Annotated, Any, Literal
3+
from urllib.parse import urlparse
4+
5+
import httpx
6+
import pytest
7+
from pydantic import BaseModel, BeforeValidator, Field, HttpUrl, ValidationError
8+
from servicelib.aiohttp import status
9+
from settings_library.base import BaseCustomSettings
10+
11+
12+
class ItisVipSettings(BaseCustomSettings):
13+
ITIS_VIP_API_URL: str
14+
ITIS_VIP_CATEGORIES: list[str]
15+
16+
def get_urls(self) -> list[HttpUrl]:
17+
return [
18+
HttpUrl(self.ITIS_VIP_API_URL.format(category=category))
19+
for category in self.ITIS_VIP_CATEGORIES
20+
]
21+
22+
23+
def _feature_descriptor_to_dict(descriptor: str) -> dict[str, Any]:
24+
# NOTE: this is manually added in the server side so be more robust to errors
25+
pattern = r"(\w+): ([^,]+)"
26+
matches = re.findall(pattern, descriptor.strip("{}"))
27+
return dict(matches)
28+
29+
30+
class AvailableDownload(BaseModel):
31+
id: Annotated[int, Field(alias="ID")]
32+
description: Annotated[str, Field(alias="Description")]
33+
thumbnail: Annotated[str, Field(alias="Thumbnail")]
34+
features: Annotated[
35+
dict[str, Any],
36+
BeforeValidator(_feature_descriptor_to_dict),
37+
Field(alias="Features"),
38+
]
39+
doi: Annotated[str, Field(alias="DOI")]
40+
license_key: Annotated[str | None, Field(alias="LicenseKey")]
41+
license_version: Annotated[str | None, Field(alias="LicenseVersion")]
42+
protection: Annotated[Literal["Code", "PayPal"], Field(alias="Protection")]
43+
available_from_url: Annotated[HttpUrl | None, Field(alias="AvailableFromURL")]
44+
45+
46+
class ResponseData(BaseModel):
47+
msg: int | None = None # still not used
48+
available_downloads: Annotated[
49+
list[AvailableDownload], Field(alias="availableDownloads")
50+
]
51+
52+
53+
async def get_downloadable_items(
54+
client: httpx.AsyncClient, url: HttpUrl
55+
) -> list[AvailableDownload]:
56+
response = await client.post(f"{url}")
57+
assert response.status_code == status.HTTP_200_OK
58+
validated_data = ResponseData.model_validate(**response.json())
59+
return validated_data.available_downloads
60+
61+
62+
async def fetch_vip_downloadables(settings: ItisVipSettings):
63+
urls, categories = settings.get_urls(), settings.ITIS_VIP_CATEGORIES
64+
65+
base_url = f"{urls[0].scheme}://{urlparse(f'{urls[0]}').netloc}"
66+
67+
async with httpx.AsyncClient() as client:
68+
for url in urls:
69+
response = await client.post(url)
70+
assert response.status_code == status.HTTP_200_OK
71+
response_json = response.json()
72+
73+
try:
74+
validated_data = ResponseData(**response_json)
75+
except ValidationError as e:
76+
pytest.fail(f"Response validation failed: {e}")

services/web/server/tests/unit/with_dbs/04/licenses/test_licensed_itis_vip_api.py renamed to services/web/server/tests/unit/with_dbs/04/licenses/test_itis_vip_service.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
# pylint: disable=too-many-arguments
55
# pylint: disable=too-many-statements
66

7-
import re
87
from collections.abc import Iterator
9-
from typing import Annotated, Any, Literal
108

119
import pytest
1210
import respx
1311
from faker import Faker
1412
from httpx import AsyncClient
15-
from pydantic import BaseModel, BeforeValidator, Field, HttpUrl, ValidationError
13+
from pydantic import ValidationError
1614
from servicelib.aiohttp import status
15+
from simcore_service_webserver.licenses._itis_vip_service import ResponseData
1716

1817

1918
@pytest.fixture
@@ -45,36 +44,6 @@ def mock_itis_vip_downloadables_api(faker: Faker) -> Iterator[respx.MockRouter]:
4544
yield mock
4645

4746

48-
def _feature_descriptor_to_dict(descriptor: str) -> dict[str, Any]:
49-
# NOTE: this is manually added in the server side so be more robust to errors
50-
pattern = r"(\w+): ([^,]+)"
51-
matches = re.findall(pattern, descriptor.strip("{}"))
52-
return dict(matches)
53-
54-
55-
class AvailableDownload(BaseModel):
56-
id: Annotated[int, Field(alias="ID")]
57-
description: Annotated[str, Field(alias="Description")]
58-
thumbnail: Annotated[str, Field(alias="Thumbnail")]
59-
features: Annotated[
60-
dict[str, Any],
61-
BeforeValidator(_feature_descriptor_to_dict),
62-
Field(alias="Features"),
63-
]
64-
doi: Annotated[str, Field(alias="DOI")]
65-
license_key: Annotated[str | None, Field(alias="LicenseKey")]
66-
license_version: Annotated[str | None, Field(alias="LicenseVersion")]
67-
protection: Annotated[Literal["Code", "PayPal"], Field(alias="Protection")]
68-
available_from_url: Annotated[HttpUrl | None, Field(alias="AvailableFromURL")]
69-
70-
71-
class ResponseData(BaseModel):
72-
msg: int = -1
73-
available_downloads: Annotated[
74-
list[AvailableDownload], Field(alias="availableDownloads")
75-
]
76-
77-
7847
async def test_fetch_itis_vip_api(
7948
mock_itis_vip_downloadables_api: respx.MockRouter,
8049
):

0 commit comments

Comments
 (0)