Skip to content

Commit fbd6f82

Browse files
committed
cleanup permalink
1 parent 01b1266 commit fbd6f82

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

services/web/server/src/simcore_service_webserver/projects/_permalink_api.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import logging
3-
from collections.abc import Callable, Coroutine
4-
from typing import Any, cast
3+
from typing import Protocol, cast
54

65
from aiohttp import web
76
from models_library.api_schemas_webserver.permalinks import ProjectPermalink
@@ -13,21 +12,24 @@
1312
_PROJECT_PERMALINK = f"{__name__}"
1413
_logger = logging.getLogger(__name__)
1514

16-
_CreateLinkCallable = Callable[
17-
[web.Request, ProjectID], Coroutine[Any, Any, ProjectPermalink]
18-
]
1915

16+
class CreateLinkCoroutine(Protocol):
17+
async def __call__(
18+
self, request: web.Request, project_uuid: ProjectID
19+
) -> ProjectPermalink:
20+
...
2021

21-
def register_factory(app: web.Application, factory_coro: _CreateLinkCallable):
22+
23+
def register_factory(app: web.Application, factory_coro: CreateLinkCoroutine):
2224
if _create := app.get(_PROJECT_PERMALINK):
2325
msg = f"Permalink factory can only be set once: registered {_create}"
2426
raise PermalinkFactoryError(msg)
2527
app[_PROJECT_PERMALINK] = factory_coro
2628

2729

28-
def _get_factory(app: web.Application) -> _CreateLinkCallable:
30+
def _get_factory(app: web.Application) -> CreateLinkCoroutine:
2931
if _create := app.get(_PROJECT_PERMALINK):
30-
return cast(_CreateLinkCallable, _create)
32+
return cast(CreateLinkCoroutine, _create)
3133

3234
msg = "Undefined permalink factory. Check plugin initialization."
3335
raise PermalinkFactoryError(msg)
@@ -37,17 +39,18 @@ def _get_factory(app: web.Application) -> _CreateLinkCallable:
3739

3840

3941
async def _create_permalink(
40-
request: web.Request, project_id: ProjectID
42+
request: web.Request, project_uuid: ProjectID
4143
) -> ProjectPermalink:
42-
create = _get_factory(request.app)
44+
create_coro: CreateLinkCoroutine = _get_factory(request.app)
4345

4446
try:
4547
permalink: ProjectPermalink = await asyncio.wait_for(
46-
create(request, project_id), timeout=_PERMALINK_CREATE_TIMEOUT_S
48+
create_coro(request=request, project_uuid=project_uuid),
49+
timeout=_PERMALINK_CREATE_TIMEOUT_S,
4750
)
4851
return permalink
49-
except asyncio.TimeoutError as err:
50-
msg = f"Permalink factory callback '{create}' timed out after {_PERMALINK_CREATE_TIMEOUT_S} secs"
52+
except TimeoutError as err:
53+
msg = f"Permalink factory callback '{create_coro}' timed out after {_PERMALINK_CREATE_TIMEOUT_S} secs"
5154
raise PermalinkFactoryError(msg) from err
5255

5356

@@ -61,7 +64,7 @@ async def update_or_pop_permalink_in_project(
6164
If fails, it pops it from project (so it is not set in the pydantic model. SEE ProjectGet.permalink)
6265
"""
6366
try:
64-
permalink = await _create_permalink(request, project_id=project["uuid"])
67+
permalink = await _create_permalink(request, project_uuid=project["uuid"])
6568

6669
assert permalink # nosec
6770
project["permalink"] = permalink
@@ -74,4 +77,14 @@ async def update_or_pop_permalink_in_project(
7477
return None
7578

7679

80+
async def aggregate_permalink_in_project(
81+
request: web.Request, project: ProjectDict
82+
) -> ProjectDict:
83+
"""
84+
Adapter to use in parallel aggregation of fields in a project dataset
85+
"""
86+
await update_or_pop_permalink_in_project(request, project)
87+
return project
88+
89+
7790
__all__: tuple[str, ...] = ("ProjectPermalink",)

0 commit comments

Comments
 (0)