|
| 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}") |
0 commit comments