Skip to content

Commit

Permalink
fix(__init__.py): update import statement for RouterMiddleware to ref…
Browse files Browse the repository at this point in the history
…lect new file structure

feat(router_middleware.py): add RouterMiddleware class to manage routes in a web application
feat(namespace_middleware.py): add NamespaceMiddleware class to create namespaces for routes
  • Loading branch information
marcuxyz committed Nov 17, 2023
1 parent a805177 commit e15d270
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion mvc_flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask import Flask
from flask.blueprints import Blueprint

from .router import Router
from .middlewares.http.router_middleware import RouterMiddleware as Router

from .middlewares.http.method_override_middleware import MethodOverrideMiddleware
from .middlewares.http.custom_request_middleware import CustomRequestMiddleware
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""Namespace module."""


class Namespace:
"""Namespace."""
class NamespaceMiddleware:
"""NamespaceMiddleware."""

def __init__(self, name: str, router):
self.name = name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from collections import namedtuple

from .namespace import Namespace
from .namespace_middleware import NamespaceMiddleware

Model = namedtuple("Model", "method path controller action")


class Router:
class RouterMiddleware:
"""
Router class for managing routes in a web application.
RouterMiddleware class for managing routes in a web application.
This class provides methods to define and manage different HTTP routes
(GET, POST, PUT, DELETE) for the application's controllers and actions.
Expand Down Expand Up @@ -51,7 +51,7 @@ def _method_route():

routes = {}

for route in Router.ROUTES:
for route in RouterMiddleware.ROUTES:
value = list(route.values())[0]
for key in route:
if key not in routes:
Expand All @@ -63,16 +63,16 @@ def _method_route():
@staticmethod
def namespace(name: str):
"""
Creates a namespace for routes.
Creates a namespace middleware for routes.
Args:
name (str): The name of the namespace.
Returns:
Namespace: An instance of Namespace associated with the given name.
NamespaceMiddleware: An instance of NamespaceMiddleware associated with the given name.
"""

return Namespace(name, Router)
return NamespaceMiddleware(name, RouterMiddleware)

@staticmethod
def get(path: str, resource: str):
Expand All @@ -85,7 +85,9 @@ def get(path: str, resource: str):
"""

controller, action = resource.split("#")
Router.ROUTES.append({controller: Model(["GET"], path, controller, action)})
RouterMiddleware.ROUTES.append(
{controller: Model(["GET"], path, controller, action)}
)

@staticmethod
def post(path: str, resource: str):
Expand All @@ -98,7 +100,9 @@ def post(path: str, resource: str):
"""

controller, action = resource.split("#")
Router.ROUTES.append({controller: Model(["POST"], path, controller, action)})
RouterMiddleware.ROUTES.append(
{controller: Model(["POST"], path, controller, action)}
)

@staticmethod
def put(path: str, resource: str):
Expand All @@ -111,7 +115,7 @@ def put(path: str, resource: str):
"""

controller, action = resource.split("#")
Router.ROUTES.append(
RouterMiddleware.ROUTES.append(
{controller: Model(["PUT", "PATCH"], path, controller, action)},
)

Expand All @@ -126,7 +130,9 @@ def delete(path: str, resource: str):
"""

controller, action = resource.split("#")
Router.ROUTES.append({controller: Model(["DELETE"], path, controller, action)})
RouterMiddleware.ROUTES.append(
{controller: Model(["DELETE"], path, controller, action)}
)

@staticmethod
def all(resource: str, only=None, base_path=""):
Expand All @@ -149,7 +155,7 @@ def all(resource: str, only=None, base_path=""):
"delete",
]
actions = only.split() if isinstance(only, str) else only
Router._add_routes(resource, actions if actions else group, base_path)
RouterMiddleware._add_routes(resource, actions if actions else group, base_path)

@staticmethod
def _add_routes(name, actions, base_path):
Expand Down Expand Up @@ -185,7 +191,7 @@ def _add_routes(name, actions, base_path):
path = f"{base_path}/{name}{urls.get(action, '')}"

if action in parameters:
getattr(Router, parameters[action])(path, f"{name}#{action}")
getattr(RouterMiddleware, parameters[action])(path, f"{name}#{action}")
continue

getattr(Router, groups[action])(path, f"{name}#{action}")
getattr(RouterMiddleware, groups[action])(path, f"{name}#{action}")

0 comments on commit e15d270

Please sign in to comment.