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

Add more editable fields, fix for the task registry api client caching #468

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
14 changes: 9 additions & 5 deletions amt/clients/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def __init__(self, base_url: str, max_retries: int = 3, timeout: int = 5) -> Non
self.client = httpx.AsyncClient(timeout=timeout, transport=transport)

async def _make_request(self, endpoint: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
response = await self.client.get(f"{self.base_url}/{endpoint}", params=params)
# we use 'Connection: close' for this reason https://github.com/encode/httpx/discussions/2959
response = await self.client.get(
f"{self.base_url}/{endpoint}", params=params, headers=[("Connection", "close")]
)
if response.status_code != 200:
raise AMTNotFound()
return response.json()
Expand All @@ -55,8 +58,9 @@ async def get_task_by_urn(self, task_type: TaskType, urn: str, version: str = "l
return response_data


task_registry_api_client = TaskRegistryAPIClient()


@alru_cache(maxsize=0 if "pytest" in sys.modules else 1000)
async def get_task_by_urn(
client: TaskRegistryAPIClient, task_type: TaskType, urn: str, version: str = "latest"
) -> dict[str, Any]:
return await client.get_task_by_urn(task_type, urn, version)
async def get_task_by_urn(task_type: TaskType, urn: str, version: str = "latest") -> dict[str, Any]:
return await task_registry_api_client.get_task_by_urn(task_type, urn, version)
4 changes: 3 additions & 1 deletion amt/core/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ async def general_exception_handler(request: Request, exc: Exception) -> HTMLRes
request, template_name, {"message": message}, status_code=status_code, headers=response_headers
)
except Exception:
logger.exception("Can not display error template")
logger.warning(
"Can not display error template " + template_name + " as it does not exist, using fallback template"
)
response = templates.TemplateResponse(
request,
fallback_template_name,
Expand Down
2 changes: 1 addition & 1 deletion amt/repositories/task_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def _fetch_tasks_by_urns(self, task_type: TaskType, urns: Sequence[str]) -
Fetches tasks given a list of URN's.
If an URN is not valid, it is ignored.
"""
get_tasks = [get_task_by_urn(self.client, task_type, urn) for urn in urns]
get_tasks = [get_task_by_urn(task_type, urn) for urn in urns]
results = await asyncio.gather(*get_tasks, return_exceptions=True)

tasks: list[dict[str, Any]] = []
Expand Down
5 changes: 2 additions & 3 deletions amt/services/instruments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from collections.abc import Sequence

from amt.clients.clients import TaskRegistryAPIClient, TaskType
from amt.clients.clients import TaskType, task_registry_api_client
from amt.repositories.task_registry import TaskRegistryRepository
from amt.schema.instrument import Instrument

Expand All @@ -24,6 +24,5 @@ async def fetch_instruments(self, urns: str | Sequence[str] | None = None) -> li


def create_instrument_service() -> InstrumentsService:
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
return InstrumentsService(repository)
5 changes: 2 additions & 3 deletions amt/services/measures.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from collections.abc import Sequence

from amt.clients.clients import TaskRegistryAPIClient, TaskType
from amt.clients.clients import TaskType, task_registry_api_client
from amt.repositories.task_registry import TaskRegistryRepository
from amt.schema.measure import Measure

Expand All @@ -24,6 +24,5 @@ async def fetch_measures(self, urns: str | Sequence[str] | None = None) -> list[


def create_measures_service() -> MeasuresService:
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
return MeasuresService(repository)
5 changes: 2 additions & 3 deletions amt/services/requirements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from collections.abc import Sequence

from amt.clients.clients import TaskRegistryAPIClient, TaskType
from amt.clients.clients import TaskType, task_registry_api_client
from amt.repositories.task_registry import TaskRegistryRepository
from amt.schema.requirement import Requirement

Expand All @@ -24,6 +24,5 @@ async def fetch_requirements(self, urns: str | Sequence[str] | None = None) -> l


def create_requirements_service() -> RequirementsService:
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
return RequirementsService(repository)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ filterwarnings = [
]
log_cli = true
log_cli_level = "INFO"
faulthandler_timeout = 40
faulthandler_timeout = 60
markers = [
"slow: marks tests as slow",
"enable_auth: marks tests that require authentication"
Expand Down
1 change: 1 addition & 0 deletions tests/api/routes/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ async def test_update_measure_value(


@pytest.mark.asyncio
@amt_vcr.use_cassette("tests/fixtures/vcr_cassettes/test_update_measure_value_with_people.yml") # type: ignore
async def test_update_measure_value_with_people(
minio_mock: MockMinioClient, client: AsyncClient, mocker: MockFixture, db: DatabaseTestUtils
) -> None:
Expand Down
6 changes: 1 addition & 5 deletions tests/clients/test_clients.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

import pytest
from amt.clients.clients import TaskRegistryAPIClient, TaskType
from amt.clients.clients import TaskType, task_registry_api_client
from amt.core.exceptions import AMTNotFound
from amt.schema.github import RepositoryContent
from pytest_httpx import HTTPXMock
Expand All @@ -10,7 +10,6 @@

@pytest.mark.asyncio
async def test_task_registry_api_client_get_instrument_list(httpx_mock: HTTPXMock):
task_registry_api_client = TaskRegistryAPIClient()
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
Expand All @@ -22,7 +21,6 @@ async def test_task_registry_api_client_get_instrument_list(httpx_mock: HTTPXMoc

@pytest.mark.asyncio
async def test_task_registry_api_client_get_instrument_list_not_succesfull(httpx_mock: HTTPXMock):
task_registry_api_client = TaskRegistryAPIClient()
httpx_mock.add_response(status_code=408, url="https://task-registry.apps.digilab.network/instruments/")

# then
Expand All @@ -33,7 +31,6 @@ async def test_task_registry_api_client_get_instrument_list_not_succesfull(httpx
@pytest.mark.asyncio
async def test_task_registry_api_client_get_instrument(httpx_mock: HTTPXMock):
# given
task_registry_api_client = TaskRegistryAPIClient()
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
Expand All @@ -49,7 +46,6 @@ async def test_task_registry_api_client_get_instrument(httpx_mock: HTTPXMock):

@pytest.mark.asyncio
async def test_task_registry_api_client_get_instrument_not_succesfull(httpx_mock: HTTPXMock):
task_registry_api_client = TaskRegistryAPIClient()
httpx_mock.add_response(
status_code=408,
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test_create_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_e2e_create_algorithm(page: Page) -> None:

button = page.locator("#button-new-algorithm-create")
button.click()
page.wait_for_timeout(30000)
expect(page.get_by_text("My new algorithm").first).to_be_visible(timeout=10000)

expect(page.get_by_text("My new algorithm").first).to_be_visible(timeout=90000)


@pytest.mark.slow
Expand Down
Loading
Loading