Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add view controllers #407

Merged
merged 8 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add view controllers
  • Loading branch information
Mikaeel authored and sansyrox committed Feb 18, 2023
commit 8eefdd1b2aa08953c6ee667faa468cb52c7e4071
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ uvloop; platform_system!="Windows"
watchdog==2.2.1
multiprocess==0.70.14
jinja2==3.1.2
nestd===0.3.0
35 changes: 35 additions & 0 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import signal
from typing import Callable, List, Optional
from nestd import get_all_nested

from watchdog.observers import Observer

Expand Down Expand Up @@ -165,6 +166,40 @@ def terminating_signal_handler(_sig, _frame):
observer.stop()
observer.join()


def add_view(self, endpoint: str, view, 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]
sansyrox marked this conversation as resolved.
Show resolved Hide resolved
"""
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']:
output.append((route_type.upper(), handler))
sansyrox marked this conversation as resolved.
Show resolved Hide resolved
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

def view(self, endpoint: str, const: bool = False):
"""
The @app.view decorator to add a view with the GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS method

:param endpoint str: endpoint to server the route
"""
def inner(handler):
return self.add_view(endpoint, handler, const)

return inner

def get(self, endpoint: str, const: bool = False):
"""
The @app.get decorator to add a route with the GET method
Expand Down