From 676a0e6778fbaadd3f969596235399bf8ee17946 Mon Sep 17 00:00:00 2001 From: "kacper.golem" Date: Wed, 2 Oct 2024 12:56:46 +0200 Subject: [PATCH] OCT-1992: Fix the projects details schema --- backend/app/exceptions.py | 8 +++++ backend/app/infrastructure/routes/projects.py | 35 +++++++++---------- .../validations/project_details_input.py | 12 +++++++ 3 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 backend/app/infrastructure/routes/validations/project_details_input.py diff --git a/backend/app/exceptions.py b/backend/app/exceptions.py index ef4dbc21c9..3318ba3167 100644 --- a/backend/app/exceptions.py +++ b/backend/app/exceptions.py @@ -354,3 +354,11 @@ class InvalidAddressFormat(OctantException): def __init__(self): super().__init__(self.description, self.code) + + +class InvalidProjectDetailsInput(OctantException): + code = 400 + description = "Invalid input for projects details" + + def __init__(self): + super().__init__(self.description, self.code) diff --git a/backend/app/infrastructure/routes/projects.py b/backend/app/infrastructure/routes/projects.py index 1d9f633b07..30263c8d86 100644 --- a/backend/app/infrastructure/routes/projects.py +++ b/backend/app/infrastructure/routes/projects.py @@ -8,6 +8,9 @@ get_projects_metadata, ) from app.modules.projects.details import controller as projects_details_controller +from app.infrastructure.routes.validations.project_details_input import ( + validate_project_details_input, +) ns = Namespace("projects", description="Octant projects") api.add_namespace(ns) @@ -24,26 +27,20 @@ }, ) +project_model = api.model( + "Project", + { + "name": fields.String(required=True, description="Project name"), + "address": fields.String(required=True, description="Project address"), + "epoch": fields.String(required=True, description="Project epoch"), + }, +) + projects_details_model = api.model( "ProjectsDetails", { - "projects_details": fields.List( - fields.Nested( - api.model( - "ProjectsDetails", - { - "name": fields.String( - required=True, description="Project name" - ), - "address": fields.String( - required=True, description="Project address" - ), - "epoch": fields.String( - required=True, description="Project epoch" - ), - }, - ) - ), + "projectsDetails": fields.List( + fields.Nested(project_model), required=False, description="Projects details", ), @@ -85,7 +82,7 @@ class ProjectsDetails(OctantResource): @ns.response(200, "Projects metadata is successfully retrieved") def get(self): search_phrases = request.args.get("searchPhrases", "").split(",") - epochs = list(map(int, request.args.get("epochs", "").split(","))) + epochs = validate_project_details_input(request.args.get("epochs", "")) app.logger.debug( f"Getting projects details for epochs {epochs} and search phrase {search_phrases}" @@ -98,7 +95,7 @@ def get(self): app.logger.debug(f"Projects details for epochs {epochs}: {projects_details}") return { - "projects_details": [ + "projectsDetails": [ { "name": project["name"], "address": project["address"], diff --git a/backend/app/infrastructure/routes/validations/project_details_input.py b/backend/app/infrastructure/routes/validations/project_details_input.py new file mode 100644 index 0000000000..cdb28b7d83 --- /dev/null +++ b/backend/app/infrastructure/routes/validations/project_details_input.py @@ -0,0 +1,12 @@ +from typing import List + +from app.exceptions import InvalidProjectDetailsInput + + +def validate_project_details_input(epochs: str) -> List[int]: + try: + epochs = list(map(int, epochs.split(","))) + except ValueError: + raise InvalidProjectDetailsInput + + return epochs