Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 9, 2023
1 parent 7b3f63b commit 2454156
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 24 deletions.
1 change: 0 additions & 1 deletion integration_tests/subroutes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from robyn import SubRouter, jsonify, WebSocket

from subroutes.di_subrouter import di_subrouter

sub_router = SubRouter(__name__, prefix="/sub_router")

Expand Down
1 change: 1 addition & 0 deletions integration_tests/subroutes/di_subrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions integration_tests/test_dependency_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 1 addition & 8 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions robyn/dependency_injection.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
}
3 changes: 3 additions & 0 deletions robyn/robyn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MiddlewareType(Enum):
BEFORE_REQUEST: str
AFTER_REQUEST: str
"""

BEFORE_REQUEST: str
AFTER_REQUEST: str

Expand All @@ -47,6 +48,7 @@ class HttpMethod(Enum):
TRACE: str
CONNECT: str
"""

GET: str
POST: str
PUT: str
Expand All @@ -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
Expand Down
9 changes: 1 addition & 8 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
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

from robyn.robyn import FunctionInfo, HttpMethod, MiddlewareType, Request, Response
from robyn import status_codes
Expand Down Expand Up @@ -135,15 +134,13 @@ def inner_handler(*args, **kwargs):
print("This is the params", params)
print("This is the injected dependencies", injected_dependencies)


new_injected_dependencies = {}
for dependency in injected_dependencies:
if dependency in params:
new_injected_dependencies[dependency] = injected_dependencies[dependency]
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))
Expand All @@ -168,17 +165,13 @@ 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: Optional[dict]) -> Callable:

# add a docstring here
params = dict(inspect.signature(handler).parameters)
number_of_params = len(params)

# need to do something here
dependency_map = {}




function = FunctionInfo(handler, iscoroutinefunction(handler), number_of_params, {}, {})
self.route_middlewares.append(RouteMiddleware(middleware_type, endpoint, function))
return handler
Expand Down
4 changes: 1 addition & 3 deletions robyn/ws.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."""
Expand All @@ -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)

Expand All @@ -50,4 +49,3 @@ def inner(handler):
self.robyn_object.add_web_socket(self.endpoint, self)

return inner

0 comments on commit 2454156

Please sign in to comment.