diff --git a/integration_tests/base_routes.py b/integration_tests/base_routes.py index e761337de..8e5e2c633 100644 --- a/integration_tests/base_routes.py +++ b/integration_tests/base_routes.py @@ -5,6 +5,8 @@ from robyn.robyn import Response from robyn.templating import JinjaTemplate +from integration_tests.views.test_view import TestView + app = Robyn(__file__) websocket = WS(app, "/web_socket") @@ -484,6 +486,16 @@ async def async_raise(): # ===== Main ===== +@app.view("/test_decorator_view") +def decorator_view(): + def get(): + return "Hello, world!" + + def post(request): + body = bytearray(request["body"]).decode("utf-8") + return {"status_code": 200, "body": body} + + if __name__ == "__main__": app.add_request_header("server", "robyn") @@ -493,4 +505,5 @@ async def async_raise(): index_file="index.html", ) app.startup_handler(startup_handler) + app.add_view("/test_view", TestView) app.start(port=8080) diff --git a/integration_tests/test_views.py b/integration_tests/test_views.py new file mode 100644 index 000000000..6ced6d196 --- /dev/null +++ b/integration_tests/test_views.py @@ -0,0 +1,21 @@ +from http_methods_helpers import get, post + + +def test_view(session): + r = get("/test_view") + assert r.status_code == 200 + assert r.text == "Get Request!" + + r = post("/test_view", data={"name": "John"}) + assert r.status_code == 200 + assert r.text == "Post Request!" + + +def test_decorator_view(session): + r = get("/test_decorator_view") + assert r.status_code == 200 + assert r.text == "Hello, world!" + + r = post("/test_decorator_view") + assert r.status_code == 200 + assert r.text == "Hello, world!" diff --git a/integration_tests/views/test_view.py b/integration_tests/views/test_view.py new file mode 100644 index 000000000..161fccf72 --- /dev/null +++ b/integration_tests/views/test_view.py @@ -0,0 +1,12 @@ +def TestView(): + def get(): + return "Hello, world!" + + def post(request): + body = bytes(request["body"]).decode("utf-8") + print(body) + return { + "status": 200, + "body": body, + "headers": {"Content-Type": "text/json"}, + } diff --git a/pyproject.toml b/pyproject.toml index 529223046..cd9be59bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ name = "robyn" dependencies = [ 'watchdog == 2.2.1', 'multiprocess == 0.70.14', + 'nestd==0.3.0', # conditional 'uvloop == 0.17.0; sys_platform == "darwin"', 'uvloop == 0.17.0; platform_machine == "x86_64"', diff --git a/robyn/__init__.py b/robyn/__init__.py index 3b5b730d3..84a54125a 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -20,8 +20,6 @@ from robyn.types import Directory, Header from robyn.ws import WS -logger = logging.getLogger(__name__) - class Robyn: """This is the python wrapper for the Robyn binaries.""" @@ -168,36 +166,27 @@ def terminating_signal_handler(_sig, _frame): observer.stop() observer.join() - def add_view(self, endpoint: str, view, const: bool = False): + def add_view(self, endpoint: str, view: Callable, const: bool = False): """ [This is base handler for the view decorators] :param endpoint [str]: [endpoint for the route added] :param handler [function]: [represents the function passed as a parent handler for single route with different route types] """ + http_methods = {"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"} def get_functions(view): functions = get_all_nested(view) output = [] for name, handler in functions: route_type = name.upper() - if route_type in [ - "GET", - "POST", - "PUT", - "DELETE", - "PATCH", - "HEAD", - "OPTIONS", - ]: + if route_type in http_methods: output.append((route_type.upper(), handler)) return output handlers = get_functions(view) - routes = [] for route_type, handler in handlers: - routes.append(self._add_route(route_type, endpoint, handler, const)) - return routes + self._add_route(route_type, endpoint, handler, const) def view(self, endpoint: str, const: bool = False): """ @@ -205,7 +194,6 @@ def view(self, endpoint: str, const: bool = False): :param endpoint str: endpoint to server the route """ - def inner(handler): return self.add_view(endpoint, handler, const)