From 6713d4658b1ae8a1893a12d78c89666471783f7d Mon Sep 17 00:00:00 2001 From: Firq Date: Mon, 2 Oct 2023 15:29:19 +0200 Subject: [PATCH] Moved models --- backend/src/models/interface.py | 19 +++++++++++++++++ backend/src/models/requests.py | 27 +++++++++++++++++++++++++ backend/src/routes/health.py | 14 +------------ backend/src/routes/openapi.py | 5 +---- backend/src/routes/requests/base.py | 7 +------ backend/src/routes/requests/create.py | 9 +++------ backend/src/routes/requests/interact.py | 14 +++---------- backend/src/routes/version.py | 5 +---- 8 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 backend/src/models/interface.py create mode 100644 backend/src/models/requests.py diff --git a/backend/src/models/interface.py b/backend/src/models/interface.py new file mode 100644 index 0000000..56a8e29 --- /dev/null +++ b/backend/src/models/interface.py @@ -0,0 +1,19 @@ +from enum import Enum +import marshmallow as ma + +class HealthStatus(Enum): + OK = 0 + WARNING = 1 + ERROR = 2 + CRITICAL = 3 + +class HealthGet(ma.Schema): + alive_since = ma.fields.String() + alive_for = ma.fields.String() + status = ma.fields.Enum(HealthStatus, type=ma.fields.String) + +class ApiVersionGet(ma.Schema): + version = ma.fields.String(example="0.1") + +class OpenAPIGet(ma.Schema): + pass diff --git a/backend/src/models/requests.py b/backend/src/models/requests.py new file mode 100644 index 0000000..48d3c4e --- /dev/null +++ b/backend/src/models/requests.py @@ -0,0 +1,27 @@ +from enum import Enum +import marshmallow as ma + +class RequestStatus(Enum): + CREATED = 0 + ACTIVE = 1 + COMPLETED = 64 + DELETED = 255 + +class RequestsAll(ma.Schema): + uuid = ma.fields.String(description="UUID v4", example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a") + status = ma.fields.String() + +class RequestsCreate(ma.Schema): + message = ma.fields.String(example="Request 3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a was created") + status = ma.fields.Enum(RequestStatus, type=ma.fields.String) + uuid = ma.fields.String(example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a") + +class UuidSchema(ma.Schema): + uuid = ma.fields.String(description="UUID of the request", example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a") + +class RequestsInteraction(ma.Schema): + status = ma.fields.String() + +class RequestsInteractionDelete(ma.Schema): + message = ma.fields.String(example="Request 3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a was deleted.") + status = ma.fields.Enum(RequestStatus, type=ma.fields.String) diff --git a/backend/src/routes/health.py b/backend/src/routes/health.py index 39d8b1c..a88e1a2 100644 --- a/backend/src/routes/health.py +++ b/backend/src/routes/health.py @@ -1,24 +1,12 @@ from datetime import datetime -from enum import Enum from flask.views import MethodView -import marshmallow as ma from app import Application +from models.interface import HealthGet, HealthStatus from . import routes as blp instance = Application.get_instance() -class HealthStatus(Enum): - OK = 0 - WARNING = 1 - ERROR = 2 - CRITICAL = 3 - -class HealthGet(ma.Schema): - alive_since = ma.fields.String() - alive_for = ma.fields.String() - status = ma.fields.Enum(HealthStatus, type=ma.fields.String) - @blp.route("/health") class ApiVersion(MethodView): @blp.doc(summary="Returns the status and alive-time of the server") diff --git a/backend/src/routes/openapi.py b/backend/src/routes/openapi.py index 55ade67..308e377 100644 --- a/backend/src/routes/openapi.py +++ b/backend/src/routes/openapi.py @@ -1,12 +1,9 @@ from flask import redirect, url_for from flask.views import MethodView -import marshmallow as ma +from models.interface import OpenAPIGet from . import routes as blp -class OpenAPIGet(ma.Schema): - pass - @blp.route("/openapi") class ApiVersion(MethodView): @blp.doc(summary="Get the OpenAPI spec as a JSON.") diff --git a/backend/src/routes/requests/base.py b/backend/src/routes/requests/base.py index d5289a2..ecb803f 100644 --- a/backend/src/routes/requests/base.py +++ b/backend/src/routes/requests/base.py @@ -1,16 +1,11 @@ from flask.views import MethodView -import marshmallow as ma from database import Database +from models.requests import RequestsAll from . import routes_requests as blp db = Database.get_instance().db -class RequestsAll(ma.Schema): - uuid = ma.fields.String(description="UUID v4", example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a") - status = ma.fields.String() - - @blp.route("/all") class AllRequests(MethodView): @blp.doc(summary="Returns all requests") diff --git a/backend/src/routes/requests/create.py b/backend/src/routes/requests/create.py index f01e3be..a8624e8 100644 --- a/backend/src/routes/requests/create.py +++ b/backend/src/routes/requests/create.py @@ -1,16 +1,12 @@ from uuid import uuid4 from flask.views import MethodView -import marshmallow as ma from database import Database +from models.requests import RequestsCreate, RequestStatus from . import routes_requests as blp db = Database.get_instance().db -class RequestsCreate(ma.Schema): - status = ma.fields.String(example="Request 3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a was created") - uuid = ma.fields.String(example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a") - @blp.route("/create") class CreateRequest(MethodView): @blp.doc(summary="Create a new request") @@ -21,7 +17,8 @@ class CreateRequest(MethodView): requests[uuid] = { "status": "created" } session.write() response = { - "status": f"Request {uuid} was created", + "status": RequestStatus.CREATED, + "message": f"Request {uuid} was created", "uuid": uuid } return response diff --git a/backend/src/routes/requests/interact.py b/backend/src/routes/requests/interact.py index 480be62..20295c1 100644 --- a/backend/src/routes/requests/interact.py +++ b/backend/src/routes/requests/interact.py @@ -1,21 +1,12 @@ from flask_smorest import abort from flask.views import MethodView -import marshmallow as ma from database import Database +from models.requests import UuidSchema, RequestsInteraction, RequestsInteractionDelete, RequestStatus from . import routes_requests as blp db = Database.get_instance().db -class UuidSchema(ma.Schema): - uuid = ma.fields.String(description="UUID of the request", example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a") - -class RequestsInteraction(ma.Schema): - status = ma.fields.String() - -class RequestsInteractionDelete(ma.Schema): - status = ma.fields.String(example="Request 3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a was deleted.") - @blp.route("/") class InteractRequest(MethodView): @blp.doc(summary="Returns a specific request by its UID") @@ -39,6 +30,7 @@ class InteractRequest(MethodView): del requests[uuid] session.write() response = { - "status": f"Request {uuid} was deleted." + "status": RequestStatus.DELETED, + "message": f"Request {uuid} was deleted." } return response diff --git a/backend/src/routes/version.py b/backend/src/routes/version.py index 37a4b9a..62b005b 100644 --- a/backend/src/routes/version.py +++ b/backend/src/routes/version.py @@ -1,12 +1,9 @@ from flask.views import MethodView -import marshmallow as ma from config.api_settings import DefaultSettings +from models.interface import ApiVersionGet from . import routes as blp -class ApiVersionGet(ma.Schema): - version = ma.fields.String(example="0.1") - @blp.route("/version") class ApiVersion(MethodView): @blp.doc(summary="Get the REST interface version identification.")