diff --git a/integration_tests/base_routes.py b/integration_tests/base_routes.py index 03f0bdeb0..e679dc0af 100644 --- a/integration_tests/base_routes.py +++ b/integration_tests/base_routes.py @@ -100,12 +100,14 @@ def jsonws_connect(): @websocket_di.on("connect") async def di_message_connect(global_dependencies, router_dependencies): - return global_dependencies["GLOBAL_DEPENDENCY"] + " "+ router_dependencies["ROUTER_DEPENDENCY"] + return global_dependencies["GLOBAL_DEPENDENCY"] + " " + router_dependencies["ROUTER_DEPENDENCY"] + @websocket_di.on("message") async def di_message(): return "" + @websocket_di.on("close") async def di_message_close(): return "" @@ -210,9 +212,11 @@ def sync_middlewares_401(): app.inject(RouterDependency="Router Dependency") + @app.get("/") async def hello_world(request): - return f"Hello, world!" + return "Hello, world!" + @app.get("/sync/str") def sync_str_get(): @@ -788,10 +792,12 @@ async def async_without_decorator(): app.inject_global(GLOBAL_DEPENDENCY=GLOBAL_DEPENDENCY) app.inject(ROUTER_DEPENDENCY=ROUTER_DEPENDENCY) + @app.get("/sync/global_di") def sync_global_di(request, router_dependencies, global_dependencies): return global_dependencies["GLOBAL_DEPENDENCY"] + @app.get("/sync/router_di") def sync_router_di(request, router_dependencies): return router_dependencies["ROUTER_DEPENDENCY"] diff --git a/integration_tests/subroutes/__init__.py b/integration_tests/subroutes/__init__.py index 8ddf78d24..139d7d849 100644 --- a/integration_tests/subroutes/__init__.py +++ b/integration_tests/subroutes/__init__.py @@ -8,6 +8,7 @@ __all__ = ["sub_router", "websocket", "di_subrouter"] + @websocket.on("connect") async def connect(ws): return "Hello world, from ws" diff --git a/integration_tests/subroutes/di_subrouter.py b/integration_tests/subroutes/di_subrouter.py index 6749b4c11..633cdffe8 100644 --- a/integration_tests/subroutes/di_subrouter.py +++ b/integration_tests/subroutes/di_subrouter.py @@ -11,6 +11,7 @@ def sync_subrouter_route_dependency(request, router_dependencies): return router_dependencies["ROUTER_DEPENDENCY"] + @di_subrouter.get("/subrouter_global_di") def sync_subrouter_global_dependency(request, global_dependencies): return global_dependencies["GLOBAL_DEPENDENCY"] diff --git a/integration_tests/test_dependency_injection.py b/integration_tests/test_dependency_injection.py index 012dab2f5..198e763c2 100644 --- a/integration_tests/test_dependency_injection.py +++ b/integration_tests/test_dependency_injection.py @@ -16,6 +16,7 @@ def test_router_dependency_injection(benchmark): assert r.status_code == 200 assert r.text == "ROUTER DEPENDENCY" + @pytest.mark.benchmark def test_subrouter_global_dependency_injection(benchmark): r = get("/di_subrouter/subrouter_global_di") diff --git a/integration_tests/test_web_sockets.py b/integration_tests/test_web_sockets.py index e8e097fc1..e1e1589d0 100644 --- a/integration_tests/test_web_sockets.py +++ b/integration_tests/test_web_sockets.py @@ -48,6 +48,7 @@ def test_web_socket_json(session): assert resp["resp"] == "*chika* *chika* Slim Shady." assert resp["msg"] == msg + def test_websocket_di(session): """ Not using this as the benchmark test since this involves JSON marshalling/unmarshalling @@ -58,4 +59,3 @@ def test_websocket_di(session): ws = create_connection(f"{BASE_URL}/web_socket_di") assert ws.recv() == msg - diff --git a/robyn/__init__.py b/robyn/__init__.py index dbc11f80f..3d3d2780c 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -73,14 +73,7 @@ def __init__( self.exception_handler: Optional[Callable] = None self.authentication_handler: Optional[AuthenticationHandler] = None - def add_route( - self, - route_type: Union[HttpMethod, str], - endpoint: str, - handler: Callable, - is_const: bool = False, - auth_required: bool = False - ): + def add_route(self, route_type: Union[HttpMethod, str], endpoint: str, handler: Callable, is_const: bool = False, auth_required: bool = False): """ Connect a URI to a handler @@ -110,7 +103,6 @@ def add_route( } route_type = http_methods[route_type] - add_route_response = self.router.add_route( route_type=route_type, endpoint=endpoint, diff --git a/robyn/dependency_injection.py b/robyn/dependency_injection.py index 2e396723e..5200f5637 100644 --- a/robyn/dependency_injection.py +++ b/robyn/dependency_injection.py @@ -1,6 +1,7 @@ """ This is Robyn's dependency injection file. """ + class DependencyMap: def __init__(self): #'request' and 'response' mappings are needed for when constructing deps_to_pass in router.py @@ -64,7 +65,7 @@ def merge_dependencies(self, target_router): target_router.dependencies.get_global_dependencies()[dep_key] = self.get_global_dependencies()[dep_key] def get_dependency_map(self, router) -> dict: - return { - "global_dependencies": self.get_global_dependencies(), - "router_dependencies": self.get_router_dependencies(router), - } + return { + "global_dependencies": self.get_global_dependencies(), + "router_dependencies": self.get_router_dependencies(router), + } diff --git a/robyn/robyn.pyi b/robyn/robyn.pyi index 8bd7b0a7d..6e4c1128f 100644 --- a/robyn/robyn.pyi +++ b/robyn/robyn.pyi @@ -29,6 +29,7 @@ class MiddlewareType(Enum): BEFORE_REQUEST: str AFTER_REQUEST: str """ + BEFORE_REQUEST: str AFTER_REQUEST: str @@ -47,6 +48,7 @@ class HttpMethod(Enum): TRACE: str CONNECT: str """ + GET: str POST: str PUT: str @@ -69,6 +71,7 @@ class FunctionInfo: args (dict): The arguments of the function kwargs (dict): The keyword arguments of the function """ + handler: Callable is_async: bool number_of_params: int diff --git a/robyn/router.py b/robyn/router.py index 646918b8f..11bd50efd 100644 --- a/robyn/router.py +++ b/robyn/router.py @@ -4,7 +4,7 @@ from functools import wraps from inspect import signature from types import CoroutineType -from typing import Any, Callable, Dict, List, NamedTuple, Union, Optional +from typing import Callable, Dict, List, NamedTuple, Union, Optional from robyn.authentication import AuthenticationHandler, AuthenticationNotConfiguredError from robyn.dependency_injection import DependencyMap @@ -135,8 +135,6 @@ def inner_handler(*args, **kwargs): # these are the arguments params = dict(inspect.signature(handler).parameters) - - new_injected_dependencies = {} for dependency in injected_dependencies: if dependency in params: @@ -144,7 +142,6 @@ def inner_handler(*args, **kwargs): else: _logger.warning(f"Dependency {dependency} is not used in the handler {handler.__name__}") - if iscoroutinefunction(handler): function = FunctionInfo(async_inner_handler, True, number_of_params, params, new_injected_dependencies) self.routes.append(Route(route_type, endpoint, function, is_const)) @@ -170,12 +167,10 @@ def set_authentication_handler(self, authentication_handler: AuthenticationHandl self.authentication_handler = authentication_handler def add_route(self, middleware_type: MiddlewareType, endpoint: str, handler: Callable, injected_dependencies: dict) -> Callable: - # add a docstring here params = dict(inspect.signature(handler).parameters) number_of_params = len(params) - new_injected_dependencies = {} for dependency in injected_dependencies: if dependency in params: @@ -185,8 +180,6 @@ def add_route(self, middleware_type: MiddlewareType, endpoint: str, handler: Cal print("This is new injected dependencies", new_injected_dependencies) - - function = FunctionInfo(handler, iscoroutinefunction(handler), number_of_params, params, new_injected_dependencies) self.route_middlewares.append(RouteMiddleware(middleware_type, endpoint, function)) return handler @@ -207,7 +200,7 @@ def inner_handler(request: Request, *args): if identity is None: return self.authentication_handler.unauthorized_response request.identity = identity - + return request self.add_route(MiddlewareType.BEFORE_REQUEST, endpoint, inner_handler, injected_dependencies) @@ -215,7 +208,6 @@ def inner_handler(request: Request, *args): return decorator - # These inner functions are basically a wrapper around the closure(decorator) being returned. # They take a handler, convert it into a closure and return the arguments. # Arguments are returned as they could be modified by the middlewares. diff --git a/robyn/ws.py b/robyn/ws.py index 49b2bddb0..4fe07ef10 100644 --- a/robyn/ws.py +++ b/robyn/ws.py @@ -1,7 +1,6 @@ from __future__ import annotations import asyncio -from inspect import signature import inspect from typing import TYPE_CHECKING, Callable from robyn.argument_parser import Config @@ -16,6 +15,7 @@ _logger = logging.getLogger(__name__) + class WebSocket: # should this be websocket router? """This is the python wrapper for the web socket that will be used here.""" @@ -35,7 +35,6 @@ def inner(handler): params = dict(inspect.signature(handler).parameters) num_params = len(params) is_async = asyncio.iscoroutinefunction(handler) - injected_dependencies = self.dependencies.get_dependency_map(self)