Skip to content

Commit

Permalink
chore: deprecate views
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed Dec 27, 2024
1 parent 7495342 commit ba15129
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 212 deletions.
64 changes: 0 additions & 64 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Optional

from integration_tests.subroutes import di_subrouter, sub_router
from integration_tests.views import AsyncView, SyncView
from robyn import Headers, Request, Response, Robyn, WebSocket, WebSocketConnector, jsonify, serve_file, serve_html
from robyn.authentication import AuthenticationHandler, BearerGetter, Identity
from robyn.robyn import QueryParams, Url
Expand Down Expand Up @@ -712,67 +711,6 @@ async def async_body_patch(request: Request):
return request.body


# ===== Views =====


@app.view("/sync/view/decorator")
def sync_decorator_view():
def get():
return "Hello, world!"

def post(request: Request):
body = request.body
return body


@app.view("/async/view/decorator")
def async_decorator_view():
async def get():
return "Hello, world!"

async def post(request: Request):
body = request.body
return body


# ==== Exception Handling ====


@app.exception
def handle_exception(error):
return Response(status_code=500, description=f"error msg: {error}", headers={})


@app.get("/sync/exception/get")
def sync_exception_get():
raise ValueError("value error")


@app.get("/async/exception/get")
async def async_exception_get():
raise ValueError("value error")


@app.put("/sync/exception/put")
def sync_exception_put(request: Request):
raise ValueError("value error")


@app.put("/async/exception/put")
async def async_exception_put(request: Request):
raise ValueError("value error")


@app.post("/sync/exception/post")
def sync_exception_post(request: Request):
raise ValueError("value error")


@app.post("/async/exception/post")
async def async_exception_post(request: Request):
raise ValueError("value error")


# ===== Authentication =====


Expand Down Expand Up @@ -1090,8 +1028,6 @@ def main():
index_file="index.html",
)
app.startup_handler(startup_handler)
app.add_view("/sync/view", SyncView)
app.add_view("/async/view", AsyncView)
app.include_router(sub_router)
app.include_router(di_subrouter)

Expand Down
51 changes: 0 additions & 51 deletions integration_tests/test_views.py

This file was deleted.

4 changes: 0 additions & 4 deletions integration_tests/views/__init__.py

This file was deleted.

9 changes: 0 additions & 9 deletions integration_tests/views/async_view.py

This file was deleted.

9 changes: 0 additions & 9 deletions integration_tests/views/sync_view.py

This file was deleted.

109 changes: 34 additions & 75 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,54 +97,51 @@ def _handle_dev_mode(self):

def add_route(
self,
route_type: Union[HttpMethod, str],
route_type: HttpMethod,
endpoint: str,
handler: Callable,
is_const: bool = False,
const: bool = False,
auth_required: bool = False,
):
"""
Connect a URI to a handler
This is base handler for the route decorators
:param route_type str: route type between GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS/TRACE
:param route_type HttpMethod: represents the type of route added
: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 is_const bool: represents if the handler is a const function or not
:param handler function: represents the function passed as a parent handler for single route with different route types
:param const bool: represents if the handler is a const function or not
:param auth_required bool: represents if the route needs authentication or not
"""
if auth_required and self.auth_handler is None:
raise AuthenticationNotConfiguredError()

params = dict(inspect.signature(handler).parameters)
num_params = len(params)
is_async = asyncio.iscoroutinefunction(handler)

""" We will add the status code here only
"""
injected_dependencies = self.dependencies.get_dependency_map(self)

if auth_required:
self.middleware_router.add_auth_middleware(endpoint)(handler)

if isinstance(route_type, str):
http_methods = {
"GET": HttpMethod.GET,
"POST": HttpMethod.POST,
"PUT": HttpMethod.PUT,
"DELETE": HttpMethod.DELETE,
"PATCH": HttpMethod.PATCH,
"HEAD": HttpMethod.HEAD,
"OPTIONS": HttpMethod.OPTIONS,
}
route_type = http_methods[route_type]

add_route_response = self.router.add_route(
route_type=route_type,
endpoint=endpoint,
handler=handler,
is_const=is_const,
exception_handler=self.exception_handler,
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.debug(f"Dependency {dependency} is not used in the handler {handler.__name__}")

self.server.add_route(
route_type,
endpoint,
FunctionInfo(
handler,
is_async,
num_params,
params,
const,
auth_required,
kwargs=new_injected_dependencies,
),
)

logger.info("Added route %s %s", route_type, endpoint)

return add_route_response

def inject(self, **kwargs):
"""
Injects the dependencies for the route
Expand Down Expand Up @@ -305,50 +302,12 @@ def start(self, host: str = "127.0.0.1", port: int = 8080, _check_port: bool = T
)

def exception(self, exception_handler: Callable):
self.exception_handler = exception_handler

def add_view(self, endpoint: str, view: Callable, const: bool = False):
"""
This is base handler for the view decorators
The @app.exception decorator to add an exception handler
: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 exception_handler function: represents the function passed as a handler for exceptions
"""
http_methods = {
"GET": HttpMethod.GET,
"POST": HttpMethod.POST,
"PUT": HttpMethod.PUT,
"DELETE": HttpMethod.DELETE,
"PATCH": HttpMethod.PATCH,
"HEAD": HttpMethod.HEAD,
"OPTIONS": HttpMethod.OPTIONS,
}

def get_functions(view) -> List[Tuple[HttpMethod, Callable]]:
functions = get_all_nested(view)
output = []
for name, handler in functions:
route_type = name.upper()
method = http_methods.get(route_type)
if method is not None:
output.append((method, handler))
return output

handlers = get_functions(view)
for route_type, handler in handlers:
self.add_route(route_type, endpoint, handler, const)

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
self.exception_handler = exception_handler

def get(
self,
Expand Down

0 comments on commit ba15129

Please sign in to comment.