Skip to content

Commit 63bc45c

Browse files
committed
cleanup
1 parent da0f6ea commit 63bc45c

File tree

6 files changed

+58
-9
lines changed

6 files changed

+58
-9
lines changed

packages/models-library/src/models_library/api_schemas_rpc_async_jobs/async_jobs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Any, TypeAlias
33
from uuid import UUID
44

5+
from models_library.users import UserID
56
from pydantic import BaseModel, model_validator
67
from typing_extensions import Self
78

@@ -41,3 +42,9 @@ class AsyncJobGet(BaseModel):
4142
class AsyncJobAbort(BaseModel):
4243
result: bool
4344
job_id: AsyncJobId
45+
46+
47+
class AsyncJobAccessData(BaseModel):
48+
"""Data for controlling access to an async job"""
49+
50+
user_id: UserID | None

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/async_jobs/async_jobs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from models_library.api_schemas_rpc_async_jobs.async_jobs import (
44
AsyncJobAbort,
5+
AsyncJobAccessData,
56
AsyncJobId,
67
AsyncJobResult,
78
AsyncJobStatus,
@@ -20,12 +21,14 @@ async def abort(
2021
rabbitmq_rpc_client: RabbitMQRPCClient,
2122
*,
2223
rpc_namespace: RPCNamespace,
23-
job_id: AsyncJobId
24+
job_id: AsyncJobId,
25+
access_data: AsyncJobAccessData | None
2426
) -> AsyncJobAbort:
2527
result = await rabbitmq_rpc_client.request(
2628
rpc_namespace,
2729
_RPC_METHOD_NAME_ADAPTER.validate_python("abort"),
2830
job_id=job_id,
31+
access_data=access_data,
2932
timeout_s=_DEFAULT_TIMEOUT_S,
3033
)
3134
assert isinstance(result, AsyncJobAbort)
@@ -36,12 +39,14 @@ async def get_status(
3639
rabbitmq_rpc_client: RabbitMQRPCClient,
3740
*,
3841
rpc_namespace: RPCNamespace,
39-
job_id: AsyncJobId
42+
job_id: AsyncJobId,
43+
access_data: AsyncJobAccessData | None
4044
) -> AsyncJobStatus:
4145
result = await rabbitmq_rpc_client.request(
4246
rpc_namespace,
4347
_RPC_METHOD_NAME_ADAPTER.validate_python("get_status"),
4448
job_id=job_id,
49+
access_data=access_data,
4550
timeout_s=_DEFAULT_TIMEOUT_S,
4651
)
4752
assert isinstance(result, AsyncJobStatus)
@@ -52,12 +57,14 @@ async def get_result(
5257
rabbitmq_rpc_client: RabbitMQRPCClient,
5358
*,
5459
rpc_namespace: RPCNamespace,
55-
job_id: AsyncJobId
60+
job_id: AsyncJobId,
61+
access_data: AsyncJobAccessData | None
5662
) -> AsyncJobResult:
5763
result = await rabbitmq_rpc_client.request(
5864
rpc_namespace,
5965
_RPC_METHOD_NAME_ADAPTER.validate_python("get_result"),
6066
job_id=job_id,
67+
access_data=access_data,
6168
timeout_s=_DEFAULT_TIMEOUT_S,
6269
)
6370
assert isinstance(result, AsyncJobResult)

services/storage/src/simcore_service_storage/api/rpc/_async_jobs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from fastapi import FastAPI
44
from models_library.api_schemas_rpc_async_jobs.async_jobs import (
55
AsyncJobAbort,
6+
AsyncJobAccessData,
67
AsyncJobId,
78
AsyncJobResult,
89
AsyncJobStatus,
@@ -18,13 +19,17 @@
1819

1920

2021
@router.expose()
21-
async def abort(app: FastAPI, job_id: AsyncJobId) -> AsyncJobAbort:
22+
async def abort(
23+
app: FastAPI, job_id: AsyncJobId, access_data: AsyncJobAccessData | None
24+
) -> AsyncJobAbort:
2225
assert app # nosec
2326
return AsyncJobAbort(result=True, job_id=job_id)
2427

2528

2629
@router.expose(reraise_if_error_type=(StatusError,))
27-
async def get_status(app: FastAPI, job_id: AsyncJobId) -> AsyncJobStatus:
30+
async def get_status(
31+
app: FastAPI, job_id: AsyncJobId, access_data: AsyncJobAccessData | None
32+
) -> AsyncJobStatus:
2833
assert app # nosec
2934
progress_report = ProgressReport(actual_value=0.5, total=1.0, attempt=1)
3035
return AsyncJobStatus(
@@ -37,7 +42,9 @@ async def get_status(app: FastAPI, job_id: AsyncJobId) -> AsyncJobStatus:
3742

3843

3944
@router.expose(reraise_if_error_type=(ResultError,))
40-
async def get_result(app: FastAPI, job_id: AsyncJobId) -> AsyncJobResult:
45+
async def get_result(
46+
app: FastAPI, job_id: AsyncJobId, access_data: AsyncJobAccessData | None
47+
) -> AsyncJobResult:
4148
assert app # nosec
4249
assert job_id # nosec
4350
return AsyncJobResult(result="Here's your result.", error=None)

services/storage/tests/unit/test_data_export.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ async def test_start_data_export(rpc_client: RabbitMQRPCClient, faker: Faker):
8888
async def test_abort_data_export(rpc_client: RabbitMQRPCClient, faker: Faker):
8989
_job_id = AsyncJobId(faker.uuid4())
9090
result = await async_jobs.abort(
91-
rpc_client, rpc_namespace=STORAGE_RPC_NAMESPACE, job_id=_job_id
91+
rpc_client,
92+
rpc_namespace=STORAGE_RPC_NAMESPACE,
93+
job_id=_job_id,
94+
access_data=None,
9295
)
9396
assert isinstance(result, AsyncJobAbort)
9497
assert result.job_id == _job_id
@@ -97,7 +100,10 @@ async def test_abort_data_export(rpc_client: RabbitMQRPCClient, faker: Faker):
97100
async def test_get_data_export_status(rpc_client: RabbitMQRPCClient, faker: Faker):
98101
_job_id = AsyncJobId(faker.uuid4())
99102
result = await async_jobs.get_status(
100-
rpc_client, rpc_namespace=STORAGE_RPC_NAMESPACE, job_id=_job_id
103+
rpc_client,
104+
rpc_namespace=STORAGE_RPC_NAMESPACE,
105+
job_id=_job_id,
106+
access_data=None,
101107
)
102108
assert isinstance(result, AsyncJobStatus)
103109
assert result.job_id == _job_id
@@ -106,7 +112,10 @@ async def test_get_data_export_status(rpc_client: RabbitMQRPCClient, faker: Fake
106112
async def test_get_data_export_result(rpc_client: RabbitMQRPCClient, faker: Faker):
107113
_job_id = AsyncJobId(faker.uuid4())
108114
result = await async_jobs.get_result(
109-
rpc_client, rpc_namespace=STORAGE_RPC_NAMESPACE, job_id=_job_id
115+
rpc_client,
116+
rpc_namespace=STORAGE_RPC_NAMESPACE,
117+
job_id=_job_id,
118+
access_data=None,
110119
)
111120
assert isinstance(result, AsyncJobResult)
112121

services/web/server/src/simcore_service_webserver/storage/_rest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from urllib.parse import quote, unquote
1010

1111
from aiohttp import ClientTimeout, web
12+
from models_library.api_schemas_rpc_async_jobs.async_jobs import AsyncJobAccessData
1213
from models_library.api_schemas_storage import STORAGE_RPC_NAMESPACE
1314
from models_library.api_schemas_storage.storage_schemas import (
1415
FileUploadCompleteResponse,
@@ -447,13 +448,18 @@ class _RequestContext(RequestParameters):
447448
@permission_required("storage.files.*")
448449
@handle_data_export_exceptions
449450
async def get_async_job_status(request: web.Request) -> web.Response:
451+
class _RequestContext(RequestParameters):
452+
user_id: UserID = Field(..., alias=RQT_USERID_KEY) # type: ignore[literal-required]
453+
454+
_req_ctx = _RequestContext.model_validate(request)
450455
rabbitmq_rpc_client = get_rabbitmq_rpc_client(request.app)
451456

452457
async_job_get = parse_request_path_parameters_as(StorageAsyncJobGet, request)
453458
async_job_rpc_status = await get_status(
454459
rabbitmq_rpc_client=rabbitmq_rpc_client,
455460
rpc_namespace=STORAGE_RPC_NAMESPACE,
456461
job_id=async_job_get.job_id,
462+
access_data=AsyncJobAccessData(user_id=_req_ctx.user_id),
457463
)
458464
return create_data_response(
459465
StorageAsyncJobStatus.from_rpc_schema(async_job_rpc_status),
@@ -469,12 +475,18 @@ async def get_async_job_status(request: web.Request) -> web.Response:
469475
@permission_required("storage.files.*")
470476
@handle_data_export_exceptions
471477
async def abort_async_job(request: web.Request) -> web.Response:
478+
class _RequestContext(RequestParameters):
479+
user_id: UserID = Field(..., alias=RQT_USERID_KEY) # type: ignore[literal-required]
480+
481+
_req_ctx = _RequestContext.model_validate(request)
482+
472483
rabbitmq_rpc_client = get_rabbitmq_rpc_client(request.app)
473484
async_job_get = parse_request_path_parameters_as(StorageAsyncJobGet, request)
474485
async_job_rpc_abort = await abort(
475486
rabbitmq_rpc_client=rabbitmq_rpc_client,
476487
rpc_namespace=STORAGE_RPC_NAMESPACE,
477488
job_id=async_job_get.job_id,
489+
access_data=AsyncJobAccessData(user_id=_req_ctx.user_id),
478490
)
479491
return web.Response(
480492
status=status.HTTP_200_OK
@@ -491,12 +503,18 @@ async def abort_async_job(request: web.Request) -> web.Response:
491503
@permission_required("storage.files.*")
492504
@handle_data_export_exceptions
493505
async def get_async_job_result(request: web.Request) -> web.Response:
506+
class _RequestContext(RequestParameters):
507+
user_id: UserID = Field(..., alias=RQT_USERID_KEY) # type: ignore[literal-required]
508+
509+
_req_ctx = _RequestContext.model_validate(request)
510+
494511
rabbitmq_rpc_client = get_rabbitmq_rpc_client(request.app)
495512
async_job_get = parse_request_path_parameters_as(StorageAsyncJobGet, request)
496513
async_job_rpc_result = await get_result(
497514
rabbitmq_rpc_client=rabbitmq_rpc_client,
498515
rpc_namespace=STORAGE_RPC_NAMESPACE,
499516
job_id=async_job_get.job_id,
517+
access_data=AsyncJobAccessData(user_id=_req_ctx.user_id),
500518
)
501519
return create_data_response(
502520
StorageAsyncJobResult.from_rpc_schema(async_job_rpc_result),

services/web/server/tests/unit/with_dbs/03/test_storage_rpc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def _(method: str, result_or_exception: Any):
5252
def side_effect(*args, **kwargs):
5353
if isinstance(result_or_exception, Exception):
5454
raise result_or_exception
55+
5556
return result_or_exception
5657

5758
mocker.patch(

0 commit comments

Comments
 (0)