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
Prev Previous commit
style: Refactor code to make things cleaner
  • Loading branch information
sansyrox committed Feb 19, 2023
commit 4b5dd5be1451706935aedc94cfe8d89bdad12d3c
8 changes: 4 additions & 4 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ async def async_raise():
# ===== Views =====


@app.view("/sync_decorator_view")
@app.view("/sync/view/decorator")
def sync_decorator_view():
def get():
return "Hello, world!"
Expand All @@ -497,7 +497,7 @@ def post(request):
return {"status_code": 200, "body": body}


@app.view("/async_decorator_view")
@app.view("/async/view/decorator")
def async_decorator_view():
async def get():
return "Hello, world!"
Expand All @@ -518,6 +518,6 @@ async def post(request):
index_file="index.html",
)
app.startup_handler(startup_handler)
app.add_view("/sync_view", SyncView)
app.add_view("/async_view", AsyncView)
app.add_view("/sync/view", SyncView)
app.add_view("/async/view", AsyncView)
app.start(port=8080)
25 changes: 0 additions & 25 deletions integration_tests/test_async_views.py

This file was deleted.

25 changes: 0 additions & 25 deletions integration_tests/test_sync_views.py

This file was deleted.

41 changes: 41 additions & 0 deletions integration_tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from helpers.http_methods_helpers import get, post


def test_get_sync_view(session):
r = get("/sync/view")
assert r.text == "Hello, world!"


def test_post_sync_view(session):
r = post("/sync/view", data={"name": "John"})
assert "John" in r.text


def test_get_sync_decorator_view(session):
r = get("/sync/view/decorator")
assert r.text == "Hello, world!"


def test_post_sync_decorator_view(session):
r = post("/sync/view/decorator", data={"name": "John"})
assert "John" in r.text


def test_get_async_view(session):
r = get("/async/view")
assert r.text == "Hello, world!"


def test_post_async_view(session):
r = post("/async/view", data={"name": "John"})
assert "John" in r.text


def test_get_async_decorator_view(session):
r = get("/async/view/decorator")
assert r.text == "Hello, world!"


def test_post_async_decorator_view(session):
r = post("/async/view/decorator", data={"name": "John"})
assert "John" in r.text
2 changes: 1 addition & 1 deletion integration_tests/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .test_view import SyncView
from .sync_view import SyncView
from .async_view import AsyncView

__all__ = ["SyncView", "AsyncView"]
16 changes: 8 additions & 8 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def __init__(self, file_object: str) -> None:

def _add_route(self, route_type, endpoint, handler, is_const=False):
"""
[This is base handler for all the decorators]
This is base handler for all the decorators

:param route_type [str]: [route type between GET/POST/PUT/DELETE/PATCH]
:param endpoint [str]: [endpoint for the route added]
:param handler [function]: [represents the sync or async function passed as a handler for the route]
:param route_type str: route type between GET/POST/PUT/DELETE/PATCH
:param endpoint str: endpoint for the route added
:param handler function: represents the sync or async function passed as a handler for the route
"""

""" We will add the status code here only
Expand Down Expand Up @@ -168,10 +168,10 @@ def terminating_signal_handler(_sig, _frame):

def add_view(self, endpoint: str, view: Callable, const: bool = False):
"""
[This is base handler for the view decorators]
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]
: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"}
AntoineRR marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -181,7 +181,7 @@ def get_functions(view):
for name, handler in functions:
route_type = name.upper()
if route_type in http_methods:
output.append((route_type.upper(), handler))
output.append((route_type, handler))
return output

handlers = get_functions(view)
Expand Down