From 5b6ad9eadb0cca4fdb6fe25317dc2704709e153b Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Tue, 11 Apr 2023 13:10:34 +0200 Subject: [PATCH] mypying --- .../handlers_datasets.py | 16 ++++++++++++---- .../simcore_service_storage/handlers_files.py | 19 ++++++++++++------- .../handlers_simcore_s3.py | 6 ++++-- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/services/storage/src/simcore_service_storage/handlers_datasets.py b/services/storage/src/simcore_service_storage/handlers_datasets.py index 772aaf66c92a..154f4f62f8e3 100644 --- a/services/storage/src/simcore_service_storage/handlers_datasets.py +++ b/services/storage/src/simcore_service_storage/handlers_datasets.py @@ -8,6 +8,7 @@ parse_request_path_parameters_as, parse_request_query_parameters_as, ) +from servicelib.json_serialization import json_dumps # Exclusive for simcore-s3 storage ----------------------- from ._meta import api_vtag @@ -26,8 +27,10 @@ UPLOAD_TASKS_KEY = f"{__name__}.upload_tasks" -@routes.get(f"/{api_vtag}/locations/{{location_id}}/datasets", name="get_datasets_metadata") # type: ignore -async def get_datasets_metadata(request: web.Request): +@routes.get( + f"/{api_vtag}/locations/{{location_id}}/datasets", name="get_datasets_metadata" +) +async def get_datasets_metadata(request: web.Request) -> web.Response: query_params = parse_request_query_parameters_as(StorageQueryParamsBase, request) path_params = parse_request_path_parameters_as(LocationPathParams, request) log.debug( @@ -36,7 +39,9 @@ async def get_datasets_metadata(request: web.Request): ) dsm = get_dsm_provider(request.app).get(path_params.location_id) - return await dsm.list_datasets(query_params.user_id) + return web.json_response( + {"data": await dsm.list_datasets(query_params.user_id)}, dumps=json_dumps + ) @routes.get(f"/{api_vtag}/locations/{{location_id}}/datasets/{{dataset_id}}/metadata", name="get_files_metadata_dataset") # type: ignore @@ -54,4 +59,7 @@ async def get_files_metadata_dataset(request: web.Request): user_id=query_params.user_id, dataset_id=path_params.dataset_id, ) - return [jsonable_encoder(FileMetaDataGet.from_orm(d)) for d in data] + return web.json_response( + {"data": [jsonable_encoder(FileMetaDataGet.from_orm(d)) for d in data]}, + dumps=json_dumps, + ) diff --git a/services/storage/src/simcore_service_storage/handlers_files.py b/services/storage/src/simcore_service_storage/handlers_files.py index 9240226c2fed..913838c0b1c8 100644 --- a/services/storage/src/simcore_service_storage/handlers_files.py +++ b/services/storage/src/simcore_service_storage/handlers_files.py @@ -95,7 +95,9 @@ async def get_file_metadata(request: web.Request) -> web.Response: # NOTE: This is what happens Larry... data must be an empty {} or else some old # dynamic services will FAIL (sic) # TODO: once all legacy services are gone, remove the try except, it will default to 404 - return web.json_response({"error": "No result found", "data": {}}) + return web.json_response( + {"error": "No result found", "data": {}}, dumps=json_dumps + ) if request.headers.get("User-Agent") == "OpenAPI-Generator/0.1.0/python": # LEGACY compatiblity with API v0.1.0 @@ -119,7 +121,8 @@ async def get_file_metadata(request: web.Request) -> web.Response: "user_name": None, }, "error": None, - } + }, + dumps=json_dumps, ) return jsonable_encoder(FileMetaDataGet.from_orm(data)) @@ -139,7 +142,7 @@ async def download_file(request: web.Request) -> web.Response: link = await dsm.create_file_download_link( query_params.user_id, path_params.file_id, query_params.link_type ) - return web.json_response({"link": link}) + return web.json_response({"data": {"link": link}}, dumps=json_dumps) @routes.put( @@ -188,7 +191,8 @@ async def upload_file(request: web.Request) -> web.Response: # return v1 response assert len(links.urls) == 1 # nosec return web.json_response( - {"link": jsonable_encoder(links.urls[0], by_alias=True)} + {"data": {"link": jsonable_encoder(links.urls[0], by_alias=True)}}, + dumps=json_dumps, ) # v2 response @@ -227,7 +231,7 @@ async def upload_file(request: web.Request) -> web.Response: f"/{api_vtag}/locations/{{location_id}}/files/{{file_id}}:abort", name="abort_upload_file", ) -async def abort_upload_file(request: web.Request): +async def abort_upload_file(request: web.Request) -> NoReturn: query_params = parse_request_query_parameters_as(StorageQueryParamsBase, request) path_params = parse_request_path_parameters_as(FilePathParams, request) log.debug( @@ -244,7 +248,7 @@ async def abort_upload_file(request: web.Request): f"/{api_vtag}/locations/{{location_id}}/files/{{file_id}}:complete", name="complete_upload_file", ) -async def complete_upload_file(request: web.Request): +async def complete_upload_file(request: web.Request) -> web.Response: query_params = parse_request_query_parameters_as(StorageQueryParamsBase, request) path_params = parse_request_path_parameters_as(FilePathParams, request) body = await parse_request_body_as(FileUploadCompletionBody, request) @@ -282,6 +286,7 @@ async def complete_upload_file(request: web.Request): return web.json_response( status=web.HTTPAccepted.status_code, data={"data": jsonable_encoder(response, by_alias=True)}, + dumps=json_dumps, ) @@ -289,7 +294,7 @@ async def complete_upload_file(request: web.Request): f"/{api_vtag}/locations/{{location_id}}/files/{{file_id}}:complete/futures/{{future_id}}", name="is_completed_upload_file", ) -async def is_completed_upload_file(request: web.Request): +async def is_completed_upload_file(request: web.Request) -> web.Response: query_params = parse_request_query_parameters_as(StorageQueryParamsBase, request) path_params = parse_request_path_parameters_as( FilePathIsUploadCompletedParams, request diff --git a/services/storage/src/simcore_service_storage/handlers_simcore_s3.py b/services/storage/src/simcore_service_storage/handlers_simcore_s3.py index 73743ef8e782..51a0fb58d179 100644 --- a/services/storage/src/simcore_service_storage/handlers_simcore_s3.py +++ b/services/storage/src/simcore_service_storage/handlers_simcore_s3.py @@ -16,6 +16,7 @@ parse_request_path_parameters_as, parse_request_query_parameters_as, ) +from servicelib.json_serialization import json_dumps from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON from settings_library.s3 import S3Settings from simcore_service_storage.dsm import get_dsm_provider @@ -48,7 +49,7 @@ async def get_or_create_temporary_s3_access(request: web.Request) -> web.Respons s3_settings: S3Settings = await sts.get_or_create_temporary_token_for_user( request.app, query_params.user_id ) - return web.json_response({"data": s3_settings.dict()}) + return web.json_response({"data": s3_settings.dict()}, dumps=json_dumps) async def _copy_folders_from_project( @@ -136,5 +137,6 @@ async def search_files_starting_with(request: web.Request) -> web.Response: log.debug("Found %d files starting with '%s'", len(data), query_params.startswith) return web.json_response( - [jsonable_encoder(FileMetaDataGet.from_orm(d)) for d in data] + {"data": [jsonable_encoder(FileMetaDataGet.from_orm(d)) for d in data]}, + dumps=json_dumps, )