Skip to content

Commit

Permalink
uses json_resonpose with status
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Oct 21, 2024
1 parent f52b669 commit 93c9669
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from aiohttp import web
from models_library.utils.json_serialization import json_dumps
from pydantic import BaseModel
from servicelib.aiohttp import status

from ...long_running_tasks._errors import TaskNotCompletedError, TaskNotFoundError
from ...long_running_tasks._models import TaskGet, TaskId, TaskStatus
from ...long_running_tasks._task import TrackedTask
from ...mimetype_constants import MIMETYPE_APPLICATION_JSON
from ..requests_validation import parse_request_path_parameters_as
from ._dependencies import get_task_context, get_tasks_manager

Expand Down Expand Up @@ -89,7 +89,7 @@ async def cancel_and_delete_task(request: web.Request) -> web.Response:
tasks_manager = get_tasks_manager(request.app)
task_context = get_task_context(request)
await tasks_manager.remove_task(path_params.task_id, with_task_context=task_context)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


__all__: tuple[str, ...] = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
parse_request_path_parameters_as,
parse_request_query_parameters_as,
)
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON

from ._meta import API_VTAG
from .dsm import get_dsm_provider
Expand Down Expand Up @@ -249,7 +248,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) -> NoReturn:
async def abort_upload_file(request: web.Request) -> web.Response:
query_params: StorageQueryParamsBase = parse_request_query_parameters_as(
StorageQueryParamsBase, request
)
Expand All @@ -261,7 +260,7 @@ async def abort_upload_file(request: web.Request) -> NoReturn:

dsm = get_dsm_provider(request.app).get(path_params.location_id)
await dsm.abort_file_upload(query_params.user_id, path_params.file_id)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.post(
Expand Down Expand Up @@ -386,7 +385,7 @@ async def delete_file(request: web.Request) -> NoReturn:

dsm = get_dsm_provider(request.app).get(path_params.location_id)
await dsm.delete_file(query_params.user_id, path_params.file_id)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.post(f"/{API_VTAG}/files/{{file_id}}:soft-copy", name="copy_as_soft_link")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging
from typing import NoReturn, cast
from typing import cast

from aiohttp import web
from aiohttp.web import RouteTableDef
from models_library.api_schemas_storage import FileMetaDataGet, FoldersBody
from models_library.projects import ProjectID
from models_library.utils.fastapi_encoders import jsonable_encoder
from models_library.utils.json_serialization import json_dumps
from servicelib.aiohttp import status
from servicelib.aiohttp.long_running_tasks.server import (
TaskProgress,
start_long_running_task,
Expand Down Expand Up @@ -106,7 +107,7 @@ async def copy_folders_from_project(request: web.Request) -> web.Response:
@routes.delete(
f"/{API_VTAG}/simcore-s3/folders/{{folder_id}}", name="delete_folders_of_project"
)
async def delete_folders_of_project(request: web.Request) -> NoReturn:
async def delete_folders_of_project(request: web.Request) -> web.Response:
query_params: DeleteFolderQueryParams = parse_request_query_parameters_as(
DeleteFolderQueryParams, request
)
Expand All @@ -126,7 +127,7 @@ async def delete_folders_of_project(request: web.Request) -> NoReturn:
query_params.node_id,
)

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.post(f"/{API_VTAG}/simcore-s3/files/metadata:search", name="search_files")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from models_library.api_schemas_webserver.auth import ApiKeyCreate
from models_library.users import UserID
from pydantic import Field
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import RequestParams, parse_request_body_as
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
from simcore_postgres_database.errors import DatabaseError
Expand Down Expand Up @@ -84,4 +85,4 @@ async def delete_api_key(request: web.Request):
"Failed to delete API key %s. Ignoring error", name, exc_info=err
)

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from models_library.utils.json_serialization import json_dumps
from pydantic import BaseModel, Field, ValidationError, parse_obj_as
from pydantic.types import NonNegativeInt
from servicelib.aiohttp import status
from servicelib.aiohttp.rest_responses import create_http_error, exception_to_response
from servicelib.aiohttp.web_exceptions_extension import get_http_error_class_or_none
from servicelib.common_headers import (
UNDEFINED_DEFAULT_SIMCORE_USER_AGENT_VALUE,
X_SIMCORE_USER_AGENT,
)
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
from servicelib.request_keys import RQT_USERID_KEY
from simcore_postgres_database.utils_groups_extra_properties import (
GroupExtraPropertiesRepo,
Expand Down Expand Up @@ -202,7 +202,7 @@ async def stop_computation(request: web.Request) -> web.Response:
await asyncio.gather(
*[computations.stop(pid, req_ctx.user_id) for pid in project_ids]
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)

except DirectorServiceError as exc:
return create_http_error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from models_library.utils.common_validators import null_or_none_str_to_none_validator
from models_library.workspaces import WorkspaceID
from pydantic import Extra, Field, Json, parse_obj_as, validator
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import (
RequestParams,
StrictRequestParams,
Expand Down Expand Up @@ -246,4 +247,4 @@ async def delete_folder_group(request: web.Request):
folder_id=path_params.folder_id,
product_name=req_ctx.product_name,
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from models_library.users import GroupID, UserID
from models_library.utils.json_serialization import json_dumps
from pydantic import BaseModel, Extra, Field, parse_obj_as
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import (
parse_request_path_parameters_as,
parse_request_query_parameters_as,
Expand Down Expand Up @@ -169,7 +170,7 @@ async def delete_group(request: web.Request):
path_params = parse_request_path_parameters_as(_GroupPathParams, request)

await api.delete_user_group(request.app, req_ctx.user_id, path_params.gid)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.get(f"/{API_VTAG}/groups/{{gid}}/users", name="get_group_users")
Expand Down Expand Up @@ -215,7 +216,7 @@ async def add_group_user(request: web.Request):
new_user_id=new_user_id,
new_user_email=new_user_email,
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


class _GroupUserPathParams(BaseModel):
Expand Down Expand Up @@ -275,7 +276,7 @@ async def delete_group_user(request: web.Request):
await api.delete_user_in_group(
request.app, req_ctx.user_id, path_params.gid, path_params.uid
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from models_library.users import UserID
from pydantic import BaseModel, Field
from servicelib.aiohttp import status
from servicelib.aiohttp.application_keys import APP_FIRE_AND_FORGET_TASKS_KEY
from servicelib.aiohttp.requests_validation import parse_request_body_as
from servicelib.logging_utils import get_log_record_extra, log_context
Expand Down Expand Up @@ -85,7 +86,7 @@ async def request_product_account(request: web.Request):
task_suffix_name=f"{__name__}.request_product_account.send_account_request_email_to_support",
fire_and_forget_tasks_collection=request.app[APP_FIRE_AND_FORGET_TASKS_KEY],
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


class _AuthenticatedContext(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from models_library.rest_pagination_utils import paginate_data
from pydantic import BaseModel, Extra, Field, NonNegativeInt
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import (
parse_request_body_as,
parse_request_path_parameters_as,
Expand Down Expand Up @@ -223,7 +224,7 @@ async def delete_project_comment(request: web.Request):
request=request,
comment_id=path_params.comment_id,
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from models_library.utils.fastapi_encoders import jsonable_encoder
from models_library.utils.json_serialization import json_dumps
from pydantic import parse_obj_as
from servicelib.aiohttp import status
from servicelib.aiohttp.long_running_tasks.server import start_long_running_task
from servicelib.aiohttp.requests_validation import (
parse_request_body_as,
Expand Down Expand Up @@ -576,7 +577,7 @@ async def patch_project(request: web.Request):
product_name=req_ctx.product_name,
)

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


#
Expand Down Expand Up @@ -661,7 +662,7 @@ async def delete_project(request: web.Request):
except ProjectDeleteError as err:
raise web.HTTPConflict(reason=f"{err}") from err

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from models_library.projects import ProjectID
from models_library.utils.common_validators import null_or_none_str_to_none_validator
from pydantic import BaseModel, Extra, validator
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import parse_request_path_parameters_as
from servicelib.aiohttp.typing_extension import Handler
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON

from .._meta import api_version_prefix as VTAG
from ..login.decorators import login_required
Expand Down Expand Up @@ -69,4 +69,4 @@ async def replace_project_folder(request: web.Request):
folder_id=path_params.folder_id,
product_name=req_ctx.product_name,
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from models_library.projects import ProjectID
from models_library.users import GroupID
from pydantic import BaseModel, Extra
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import (
parse_request_body_as,
parse_request_path_parameters_as,
)
from servicelib.aiohttp.typing_extension import Handler
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON

from .._meta import api_version_prefix as VTAG
from ..login.decorators import login_required
Expand Down Expand Up @@ -154,4 +154,4 @@ async def delete_project_group(request: web.Request):
group_id=path_params.group_id,
product_name=req_ctx.product_name,
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from models_library.utils.fastapi_encoders import jsonable_encoder
from models_library.utils.json_serialization import json_dumps
from pydantic import BaseModel, Field, parse_obj_as
from servicelib.aiohttp import status
from servicelib.aiohttp.long_running_tasks.server import (
TaskProgress,
start_long_running_task,
Expand Down Expand Up @@ -238,7 +239,7 @@ async def patch_project_node(request: web.Request) -> web.Response:
node_patch=node_patch,
)

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.delete(f"/{VTAG}/projects/{{project_id}}/nodes/{{node_id}}", name="delete_node")
Expand All @@ -263,7 +264,7 @@ async def delete_node(request: web.Request) -> web.Response:
req_ctx.product_name,
)

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.post(
Expand Down Expand Up @@ -310,7 +311,7 @@ async def update_node_outputs(request: web.Request) -> web.Response:
node_errors=None,
ui_changed_keys=ui_changed_keys,
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.post(
Expand All @@ -333,7 +334,7 @@ async def start_node(request: web.Request) -> web.Response:
node_id=path_params.node_id,
)

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


async def _stop_dynamic_service_task(
Expand All @@ -347,15 +348,15 @@ async def _stop_dynamic_service_task(
await dynamic_scheduler_api.stop_dynamic_service(
app, dynamic_service_stop=dynamic_service_stop
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)

except (RPCServerError, ServiceWaitingForManualInterventionError) as exc:
# in case there is an error reply as not found
raise web.HTTPNotFound(reason=f"{exc}") from exc

except ServiceWasNotFoundError:
# in case the service is not found reply as all OK
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


@routes.post(
Expand Down Expand Up @@ -413,7 +414,7 @@ async def restart_node(request: web.Request) -> web.Response:

await director_v2_api.restart_dynamic_service(request.app, f"{path_params.node_id}")

return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from aiohttp import web
from models_library.projects_state import ProjectState
from pydantic import BaseModel
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import (
parse_request_path_parameters_as,
parse_request_query_parameters_as,
Expand All @@ -20,7 +21,6 @@
UNDEFINED_DEFAULT_SIMCORE_USER_AGENT_VALUE,
X_SIMCORE_USER_AGENT,
)
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
from simcore_postgres_database.models.users import UserRole
from simcore_postgres_database.webserver_models import ProjectType

Expand Down Expand Up @@ -222,7 +222,7 @@ async def close_project(request: web.Request) -> web.Response:
),
)
await project_logs.unsubscribe(request.app, path_params.project_id)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from models_library.utils.common_validators import null_or_none_str_to_none_validator
from models_library.workspaces import WorkspaceID
from pydantic import BaseModel, Extra, validator
from servicelib.aiohttp import status
from servicelib.aiohttp.requests_validation import parse_request_path_parameters_as
from servicelib.aiohttp.typing_extension import Handler
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON

from .._meta import api_version_prefix as VTAG
from ..folders.errors import FolderAccessForbiddenError, FolderNotFoundError
Expand Down Expand Up @@ -81,4 +81,4 @@ async def replace_project_workspace(request: web.Request):
workspace_id=path_params.workspace_id,
product_name=req_ctx.product_name,
)
return web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON)
return web.json_response(status=status.HTTP_204_NO_CONTENT)
Loading

0 comments on commit 93c9669

Please sign in to comment.