Skip to content

Commit dbde203

Browse files
author
Andrei Neagu
committed
using new types
1 parent b100eec commit dbde203

File tree

4 files changed

+26
-8
lines changed
  • packages
    • models-library/src/models_library/api_schemas_webserver
    • service-library/src/servicelib/rabbitmq/rpc_interfaces/storage
  • services

4 files changed

+26
-8
lines changed

packages/models-library/src/models_library/api_schemas_webserver/storage.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
2-
from typing import Annotated
2+
from typing import Annotated, Any
33

4-
from pydantic import BaseModel, Field
4+
from pydantic import BaseModel, BeforeValidator, Field
55

66
from ..api_schemas_storage.storage_schemas import (
77
DEFAULT_NUMBER_OF_PATHS_PER_PAGE,
@@ -37,5 +37,18 @@ class BatchDeletePathsBodyParams(InputSchema):
3737
paths: set[Path]
3838

3939

40+
def _ensure_valid_path(value: Any) -> str:
41+
try:
42+
Path(value)
43+
except Exception as e:
44+
msg = f"Provided {value=} is nto a valid path"
45+
raise ValueError(msg) from e
46+
47+
return value
48+
49+
50+
PathToExport = Annotated[str, BeforeValidator(_ensure_valid_path)]
51+
52+
4053
class DataExportPost(InputSchema):
41-
paths: list[Path]
54+
paths: list[PathToExport]

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/storage/simcore_s3.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from pathlib import Path
2-
31
from models_library.api_schemas_rpc_async_jobs.async_jobs import (
42
AsyncJobGet,
53
AsyncJobNameData,
64
)
75
from models_library.api_schemas_storage import STORAGE_RPC_NAMESPACE
86
from models_library.api_schemas_storage.storage_schemas import FoldersBody
7+
from models_library.api_schemas_webserver.storage import PathToExport
98
from models_library.products import ProductName
109
from models_library.rabbitmq_basic_types import RPCMethodName
1110
from models_library.users import UserID
@@ -38,7 +37,7 @@ async def start_data_export(
3837
*,
3938
user_id: UserID,
4039
product_name: ProductName,
41-
paths_to_export: list[Path],
40+
paths_to_export: list[PathToExport],
4241
) -> tuple[AsyncJobGet, AsyncJobNameData]:
4342
job_id_data = AsyncJobNameData(user_id=user_id, product_name=product_name)
4443
async_job_rpc_get = await submit(

services/storage/src/simcore_service_storage/api/_worker_tasks/_simcore_s3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from models_library.progress_bar import ProgressReport
1010
from models_library.projects_nodes_io import StorageFileID
1111
from models_library.users import UserID
12+
from pydantic import TypeAdapter
1213
from servicelib.logging_utils import log_context
1314
from servicelib.progress_bar import ProgressBarData
1415
from simcore_service_storage.exceptions.errors import FileAccessRightError
@@ -78,6 +79,11 @@ async def export_data(
7879
)
7980
assert isinstance(dsm, SimcoreS3DataManager) # nosec
8081

82+
paths_to_export = [
83+
TypeAdapter(StorageFileID).validate_python(path_to_export)
84+
for path_to_export in paths_to_export
85+
]
86+
8187
try:
8288
for path_to_export in paths_to_export:
8389
await dsm.can_read_file(user_id=user_id, file_id=path_to_export)

services/web/server/tests/unit/with_dbs/01/storage/test_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ async def test_data_export(
464464
)
465465

466466
_body = DataExportPost(
467-
paths=[Path(f"{faker.uuid4()}/{faker.uuid4()}/{faker.file_name()}")]
467+
paths=[f"{faker.uuid4()}/{faker.uuid4()}/{faker.file_name()}"]
468468
)
469469
response = await client.post(
470470
f"/{API_VERSION}/storage/locations/0/data-export", data=_body.model_dump_json()
@@ -671,7 +671,7 @@ async def test_get_async_job_links(
671671
)
672672

673673
_body = DataExportPost(
674-
paths=[Path(f"{faker.uuid4()}/{faker.uuid4()}/{faker.file_name()}")]
674+
paths=[f"{faker.uuid4()}/{faker.uuid4()}/{faker.file_name()}"]
675675
)
676676
response = await client.post(
677677
f"/{API_VERSION}/storage/locations/0/data-export", data=_body.model_dump_json()

0 commit comments

Comments
 (0)