|
1 | 1 | import asyncio |
2 | 2 | import logging |
3 | | -from typing import cast |
| 3 | +from typing import Annotated, cast |
4 | 4 |
|
5 | | -from aiohttp import web |
6 | | -from aiohttp.web import RouteTableDef |
7 | | -from common_library.json_serialization import json_dumps |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, Request |
8 | 6 | from models_library.api_schemas_storage import FileLocation |
| 7 | +from models_library.generics import Envelope |
9 | 8 | from models_library.projects_nodes_io import StorageFileID |
| 9 | +from servicelib.aiohttp import status |
10 | 10 | from servicelib.aiohttp.application_keys import ( |
11 | 11 | APP_CONFIG_KEY, |
12 | 12 | APP_FIRE_AND_FORGET_TASKS_KEY, |
13 | 13 | ) |
14 | | -from servicelib.aiohttp.requests_validation import ( |
15 | | - parse_request_path_parameters_as, |
16 | | - parse_request_query_parameters_as, |
17 | | -) |
18 | 14 | from servicelib.utils import fire_and_forget_task |
19 | 15 |
|
20 | 16 | # Exclusive for simcore-s3 storage ----------------------- |
21 | | -from ..._meta import API_VTAG |
22 | 17 | from ...core.settings import ApplicationSettings |
23 | 18 | from ...dsm import get_dsm_provider |
24 | 19 | from ...models import ( |
25 | | - LocationPathParams, |
| 20 | + LocationID, |
26 | 21 | StorageQueryParamsBase, |
27 | 22 | SyncMetadataQueryParams, |
28 | 23 | SyncMetadataResponse, |
|
31 | 26 |
|
32 | 27 | _logger = logging.getLogger(__name__) |
33 | 28 |
|
34 | | -routes = RouteTableDef() |
| 29 | +router = APIRouter( |
| 30 | + tags=["locations"], |
| 31 | +) |
35 | 32 |
|
36 | 33 |
|
37 | 34 | # HANDLERS --------------------------------------------------- |
38 | | -@routes.get(f"/{API_VTAG}/locations", name="list_storage_locations") |
39 | | -async def list_storage_locations(request: web.Request) -> web.Response: |
40 | | - query_params: StorageQueryParamsBase = parse_request_query_parameters_as( |
41 | | - StorageQueryParamsBase, request |
42 | | - ) |
43 | | - _logger.debug( |
44 | | - "received call to list_storage_locations with %s", |
45 | | - f"{query_params=}", |
46 | | - ) |
| 35 | +@router.get( |
| 36 | + "/locations", |
| 37 | + status_code=status.HTTP_200_OK, |
| 38 | + response_model=Envelope[list[FileLocation]], |
| 39 | +) |
| 40 | +async def list_storage_locations( |
| 41 | + query_params: Annotated[StorageQueryParamsBase, Depends()], request: Request |
| 42 | +): |
47 | 43 | dsm_provider = get_dsm_provider(request.app) |
48 | 44 | location_ids = dsm_provider.locations() |
49 | 45 | locs: list[FileLocation] = [] |
50 | 46 | for loc_id in location_ids: |
51 | 47 | dsm = dsm_provider.get(loc_id) |
52 | 48 | if await dsm.authorized(query_params.user_id): |
53 | 49 | locs.append(FileLocation(name=dsm.location_name, id=dsm.location_id)) |
54 | | - |
55 | | - return web.json_response({"error": None, "data": locs}, dumps=json_dumps) |
| 50 | + return Envelope[list[FileLocation]](data=locs) |
56 | 51 |
|
57 | 52 |
|
58 | | -@routes.post( |
59 | | - f"/{API_VTAG}/locations/{{location_id}}:sync", name="synchronise_meta_data_table" |
| 53 | +@router.post( |
| 54 | + "/locations/{location_id}:sync", response_model=Envelope[SyncMetadataResponse] |
60 | 55 | ) |
61 | | -async def synchronise_meta_data_table(request: web.Request) -> web.Response: |
62 | | - query_params: SyncMetadataQueryParams = parse_request_query_parameters_as( |
63 | | - SyncMetadataQueryParams, request |
64 | | - ) |
65 | | - path_params = parse_request_path_parameters_as(LocationPathParams, request) |
66 | | - _logger.debug( |
67 | | - "received call to synchronise_meta_data_table with %s", |
68 | | - f"{path_params=}, {query_params=}", |
69 | | - ) |
70 | | - |
| 56 | +async def synchronise_meta_data_table( |
| 57 | + query_params: Annotated[SyncMetadataQueryParams, Depends()], |
| 58 | + location_id: LocationID, |
| 59 | + request: Request, |
| 60 | +): |
| 61 | + if not location_id == SimcoreS3DataManager.get_location_id(): |
| 62 | + raise HTTPException( |
| 63 | + status.HTTP_400_BAD_REQUEST, |
| 64 | + detail="invalid call: cannot be called for other than simcore", |
| 65 | + ) |
71 | 66 | dsm = cast( |
72 | 67 | SimcoreS3DataManager, |
73 | 68 | get_dsm_provider(request.app).get(SimcoreS3DataManager.get_location_id()), |
@@ -101,10 +96,4 @@ async def _go(): |
101 | 96 | fire_and_forget=query_params.fire_and_forget, |
102 | 97 | dry_run=query_params.dry_run, |
103 | 98 | ) |
104 | | - return web.json_response( |
105 | | - { |
106 | | - "error": None, |
107 | | - "data": response, |
108 | | - }, |
109 | | - dumps=json_dumps, |
110 | | - ) |
| 99 | + return Envelope[SyncMetadataResponse](data=response) |
0 commit comments