Skip to content

Commit

Permalink
fix: factorizing code (#322)
Browse files Browse the repository at this point in the history
* fix: factorizing code

* fix: make FunctionInfo a dataclass

* fix: changes after review
  • Loading branch information
AntoineRR authored Dec 4, 2022
1 parent 07b2212 commit 383ffc3
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 640 deletions.
10 changes: 5 additions & 5 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from robyn.log_colors import Colors
from robyn.processpool import spawn_process
from robyn.responses import jsonify, static_file
from robyn.robyn import SocketHeld
from robyn.robyn import FunctionInfo, SocketHeld
from robyn.router import MiddlewareRouter, Router, WebSocketRouter
from robyn.ws import WS
from robyn.env_populator import load_vars
Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self, file_object: str) -> None:
load_vars(project_root=directory_path)
self._config_logger()

def _add_route(self, route_type, endpoint, handler, const=False):
def _add_route(self, route_type, endpoint, handler, is_const=False):
"""
[This is base handler for all the decorators]
Expand All @@ -54,7 +54,7 @@ def _add_route(self, route_type, endpoint, handler, const=False):

""" We will add the status code here only
"""
return self.router.add_route(route_type, endpoint, handler, const)
return self.router.add_route(route_type, endpoint, handler, is_const)

def before_request(self, endpoint: str) -> Callable[..., None]:
"""
Expand Down Expand Up @@ -89,13 +89,13 @@ def add_header(self, key: str, value: str) -> None:
def add_web_socket(self, endpoint: str, ws: WS) -> None:
self.web_socket_router.add_route(endpoint, ws)

def _add_event_handler(self, event_type: Events, handler) -> None:
def _add_event_handler(self, event_type: Events, handler: Callable) -> None:
logger.debug(f"Add event {event_type} handler")
if event_type not in {Events.STARTUP, Events.SHUTDOWN}:
return

is_async = asyncio.iscoroutinefunction(handler)
self.event_handlers[event_type] = (handler, is_async)
self.event_handlers[event_type] = FunctionInfo(handler, is_async, 0)

def startup_handler(self, handler: Callable) -> None:
self._add_event_handler(Events.STARTUP, handler)
Expand Down
24 changes: 8 additions & 16 deletions robyn/processpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, Tuple

from robyn.events import Events
from robyn.robyn import Server, SocketHeld
from robyn.robyn import FunctionInfo, Server, SocketHeld
from robyn.router import MiddlewareRoute, Route
from robyn.ws import WS

Expand Down Expand Up @@ -34,7 +34,7 @@ def spawn_process(
routes: Tuple[Route, ...],
middlewares: Tuple[MiddlewareRoute, ...],
web_sockets: Dict[str, WS],
event_handlers: Dict[Events, list],
event_handlers: Dict[Events, FunctionInfo],
socket: SocketHeld,
workers: int,
):
Expand Down Expand Up @@ -67,26 +67,18 @@ def spawn_process(
server.add_header(key, val)

for route in routes:
route_type, endpoint, handler, is_async, number_of_params, const = route
server.add_route(
route_type, endpoint, handler, is_async, number_of_params, const
)
route_type, endpoint, function, is_const = route
server.add_route(route_type, endpoint, function, is_const)

for route in middlewares:
route_type, endpoint, handler, is_async, number_of_params = route
server.add_middleware_route(
route_type, endpoint, handler, is_async, number_of_params
)
route_type, endpoint, function = route
server.add_middleware_route(route_type, endpoint, function)

if "startup" in event_handlers:
server.add_startup_handler(
event_handlers[Events.STARTUP][0], event_handlers[Events.STARTUP][1]
)
server.add_startup_handler(event_handlers[Events.STARTUP])

if "shutdown" in event_handlers:
server.add_shutdown_handler(
event_handlers[Events.SHUTDOWN][0], event_handlers[Events.SHUTDOWN][1]
)
server.add_shutdown_handler(event_handlers[Events.SHUTDOWN])

for endpoint in web_sockets:
web_socket = web_sockets[endpoint]
Expand Down
29 changes: 16 additions & 13 deletions robyn/robyn.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from __future__ import annotations
from dataclasses import dataclass

from typing import Callable, Optional, Tuple
from typing import Callable, Optional

class SocketHeld:
def __init__(self, url: str, port: int):
pass
def try_clone(self) -> SocketHeld:
pass

@dataclass
class FunctionInfo:
function: Callable
is_async: bool
number_of_params: int

class Server:
def __init__(self) -> None:
pass
Expand All @@ -25,31 +32,27 @@ class Server:
self,
route_type: str,
route: str,
handler: Callable,
is_async: bool,
number_of_params: int,
const: bool,
function: FunctionInfo,
is_const: bool,
) -> None:
pass
def add_middleware_route(
self,
route_type: str,
route: str,
handler: Callable,
is_async: bool,
number_of_params: int,
function: FunctionInfo,
) -> None:
pass
def add_startup_handler(self, handler: Callable, is_async: bool) -> None:
def add_startup_handler(self, function: FunctionInfo) -> None:
pass
def add_shutdown_handler(self, handler: Callable, is_async: bool) -> None:
def add_shutdown_handler(self, function: FunctionInfo) -> None:
pass
def add_web_socket_route(
self,
route: str,
connect_route: Tuple[Callable, bool, int],
close_route: Tuple[Callable, bool, int],
message_route: Tuple[Callable, bool, int],
connect_route: FunctionInfo,
close_route: FunctionInfo,
message_route: FunctionInfo,
) -> None:
pass
def start(self, socket: SocketHeld, workers: int) -> None:
Expand Down
32 changes: 8 additions & 24 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from inspect import signature
from typing import Callable, Dict, List, Tuple, Union
from types import CoroutineType
from robyn.robyn import FunctionInfo

from robyn.ws import WS

Expand Down Expand Up @@ -40,7 +41,7 @@ def _format_response(self, res):
return response

def add_route(
self, route_type: str, endpoint: str, handler: Callable, const: bool
self, route_type: str, endpoint: str, handler: Callable, is_const: bool
) -> Union[Callable, CoroutineType]:
@wraps(handler)
async def async_inner_handler(*args):
Expand All @@ -54,22 +55,12 @@ def inner_handler(*args):

number_of_params = len(signature(handler).parameters)
if iscoroutinefunction(handler):
self.routes.append(
(
route_type,
endpoint,
async_inner_handler,
True,
number_of_params,
const,
)
)

function = FunctionInfo(async_inner_handler, True, number_of_params)
self.routes.append((route_type, endpoint, function, is_const))
return async_inner_handler
else:
self.routes.append(
(route_type, endpoint, inner_handler, False, number_of_params, const)
)
function = FunctionInfo(inner_handler, False, number_of_params)
self.routes.append((route_type, endpoint, function, is_const))
return inner_handler

def get_routes(self) -> List[Route]:
Expand All @@ -83,15 +74,8 @@ def __init__(self) -> None:

def add_route(self, route_type: str, endpoint: str, handler: Callable) -> Callable:
number_of_params = len(signature(handler).parameters)
self.routes.append(
(
route_type,
endpoint,
handler,
iscoroutinefunction(handler),
number_of_params,
)
)
function = FunctionInfo(handler, iscoroutinefunction(handler), number_of_params)
self.routes.append((route_type, endpoint, function))
return handler

# These inner function is basically a wrapper arround the closure(decorator)
Expand Down
8 changes: 4 additions & 4 deletions robyn/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from inspect import signature
from typing import TYPE_CHECKING, Callable

from robyn.robyn import FunctionInfo

if TYPE_CHECKING:
from robyn import Robyn

Expand All @@ -21,10 +23,8 @@ def inner(handler):
if type not in ["connect", "close", "message"]:
raise Exception(f"Socket method {type} does not exist")
else:
self.methods[type] = (
handler,
self._is_async(handler),
self._num_params(handler),
self.methods[type] = FunctionInfo(
handler, self._is_async(handler), self._num_params(handler)
)
self.robyn_object.add_web_socket(self.endpoint, self)

Expand Down
Loading

0 comments on commit 383ffc3

Please sign in to comment.