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 1 commit
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
9 changes: 5 additions & 4 deletions amt/clients/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,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)
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)
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
20 changes: 7 additions & 13 deletions tests/repositories/test_task_registry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from amt.clients.clients import TaskRegistryAPIClient, TaskType
from amt.clients.clients import TaskType, task_registry_api_client
from amt.core.exceptions import AMTInstrumentError
from amt.repositories.task_registry import TaskRegistryRepository
from pytest_httpx import HTTPXMock
Expand All @@ -10,8 +10,7 @@
@pytest.mark.asyncio
async def test_fetch_tasks_all():
# given
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)

# when
instrument_result = await repository.fetch_tasks(TaskType.INSTRUMENTS)
Expand All @@ -28,8 +27,7 @@ async def test_fetch_tasks_all():
@pytest.mark.asyncio
async def test_fetch_task_with_urn():
# given
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
instrument_urn = "urn:nl:aivt:tr:iama:1.0"
requirement_urn = "urn:nl:ak:ver:aia-08"
measure_urn = "urn:nl:ak:mtr:dat-02"
Expand Down Expand Up @@ -57,8 +55,7 @@ async def test_fetch_task_with_urn():
@pytest.mark.asyncio
async def test_fetch_task_with_urns():
# given
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
urns = ["urn:nl:aivt:tr:iama:1.0", "urn:nl:aivt:tr:aiia:1.0"]

# when
Expand All @@ -76,8 +73,7 @@ async def test_fetch_task_with_urns():
@pytest.mark.asyncio
async def test_fetch_task_with_invalid_urn():
# given
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
urn = "invalid"

# when
Expand All @@ -91,8 +87,7 @@ async def test_fetch_task_with_invalid_urn():
@pytest.mark.asyncio
async def test_fetch_task_with_valid_and_invalid_urn():
# given
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
urns = ["urn:nl:aivt:tr:iama:1.0", "invalid"]

# when
Expand All @@ -107,8 +102,7 @@ async def test_fetch_task_with_valid_and_invalid_urn():
@pytest.mark.asyncio
async def test_fetch_tasks_invalid_response(httpx_mock: HTTPXMock):
# given
client = TaskRegistryAPIClient()
repository = TaskRegistryRepository(client)
repository = TaskRegistryRepository(task_registry_api_client)
urn = "urn:nl:aivt:tr:td:1.0"

httpx_mock.add_response(
Expand Down
Loading