diff --git a/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_project_chroots.py b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_project_chroots.py index c63dd5729..0e7150879 100644 --- a/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_project_chroots.py +++ b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_project_chroots.py @@ -4,7 +4,7 @@ import flask from flask_restx import Namespace, Resource -from coprs.views.misc import api_login_required, payload_multiple_get +from coprs.views.misc import api_login_required, request_multiple_args from coprs.views.apiv3_ns import apiv3_ns, api, rename_fields_helper from coprs.views.apiv3_ns.schema.schemas import ( project_chroot_model, @@ -82,11 +82,11 @@ def get(self): Get a project chroot Get settings for a single project chroot. """ - ownername, projectname, chrootname = payload_multiple_get( - apiv3_project_chroots_ns.payload, "ownername", "projectname", "chrootname" + ownername, projectname, chrootname = request_multiple_args( + "ownername", "projectname", "chrootname" ) copr = get_copr(ownername, projectname) - chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname) + chroot = ComplexLogic.get_copr_chroot(copr, chrootname) return to_dict(chroot) @@ -99,11 +99,11 @@ def get(self): Get a build config Generate a build config based on a project chroot settings. """ - ownername, projectname, chrootname = payload_multiple_get( - apiv3_project_chroots_ns.payload, "ownername", "projectname", "chrootname" + ownername, projectname, chrootname = request_multiple_args( + "ownername", "projectname", "chrootname" ) copr = get_copr(ownername, projectname) - chroot = ComplexLogic.get_copr_chroot_safe(copr, chrootname) + chroot = ComplexLogic.get_copr_chroot(copr, chrootname) if not chroot: raise ObjectNotFound('Chroot not found.') return to_build_config_dict(chroot) diff --git a/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_projects.py b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_projects.py index 4904fb392..94eb6a39c 100644 --- a/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_projects.py +++ b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_projects.py @@ -11,7 +11,7 @@ restx_editable_copr) from coprs.views.apiv3_ns.json2form import get_form_compatible_data, get_input_dict from coprs import db, models, forms, db_session_scope -from coprs.views.misc import restx_api_login_required, payload_multiple_get +from coprs.views.misc import restx_api_login_required, request_multiple_args from coprs.views.apiv3_ns import rename_fields_helper, api from coprs.views.apiv3_ns.schema.schemas import ( project_model, @@ -117,9 +117,7 @@ def get(self): Get a project Get details for a single Copr project according to ownername and projectname. """ - ownername, projectname = payload_multiple_get( - apiv3_projects_ns.payload, "ownername", "projectname" - ) + ownername, projectname = request_multiple_args("ownername", "projectname") copr = get_copr(ownername, projectname) return to_dict(copr) diff --git a/frontend/coprs_frontend/coprs/views/apiv3_ns/schema/schemas.py b/frontend/coprs_frontend/coprs/views/apiv3_ns/schema/schemas.py index 168746c8b..963d18fcb 100644 --- a/frontend/coprs_frontend/coprs/views/apiv3_ns/schema/schemas.py +++ b/frontend/coprs_frontend/coprs/views/apiv3_ns/schema/schemas.py @@ -65,7 +65,11 @@ def input_model(self): Returns an input model (input to @ns.expect()) with properly set required parameters. """ - for field in self.get_required_attrs: + required_args = self.get_required_attrs + if getattr(self, "__all_required", False): + required_args = fields(self) + + for field in required_args: field.required = True return api.model(self.__class__.__name__, self.schema()) @@ -116,9 +120,7 @@ class ProjectChrootGet(InputSchema): projectname: String chrootname: String = mock_chroot - @property - def get_required_attrs(self) -> list: - return [self.ownername, self.projectname, self.chrootname] + __all_required: bool = True @dataclass @@ -320,6 +322,8 @@ class ProjectGet(InputSchema): ownername: String projectname: String + __all_required: bool = True + # OUTPUT MODELS project_chroot_model = ProjectChroot.get_cls().model() diff --git a/frontend/coprs_frontend/coprs/views/misc.py b/frontend/coprs_frontend/coprs/views/misc.py index 1cb1b8a01..7c9e86767 100644 --- a/frontend/coprs_frontend/coprs/views/misc.py +++ b/frontend/coprs_frontend/coprs/views/misc.py @@ -353,6 +353,7 @@ def make_response(content, status=HTTPStatus.OK): def payload_multiple_get(payload: dict, *parameters) -> list: """ Get multiple values from dictionary. + Args: payload: Any dictionary obtain from API request *parameters: list of parameters to obtain values from request @@ -363,3 +364,27 @@ def payload_multiple_get(payload: dict, *parameters) -> list: return multiple_get(payload, parameters) except KeyError as exc: raise BadRequest(str(exc)) from exc + + +def request_multiple_args(*query_params, raise_on_missing=True) -> list: + """ + Get multiple values from query parameters. + + Args: + raise_on_missing: raises BadRequest if set to True, otherwise ignores + missing query parameters + *query_params: list of args to obtain values from flask.request.args + Returns: + *args values in the same order as they were given. + """ + result = [] + flask_args = flask.request.args + empty = "__empty_content" + for arg in query_params: + flask_arg = flask_args.get(arg, empty) + if raise_on_missing and flask_arg == empty: + raise BadRequest(f"Missing arg: {arg}") + + result.append(flask_arg) + + return result