Skip to content

Commit

Permalink
fix: allow inserting string to add_route
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido Slonimsky committed Sep 6, 2023
1 parent e95906c commit 7826a98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
13 changes: 6 additions & 7 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Optional

from robyn import (
HttpMethod,
Request,
Response,
Robyn,
Expand Down Expand Up @@ -731,12 +730,12 @@ async def async_without_decorator():
return "Success!"


app.add_route(HttpMethod.GET, "/sync/get/no_dec", sync_without_decorator)
app.add_route(HttpMethod.PUT, "/sync/put/no_dec", sync_without_decorator)
app.add_route(HttpMethod.POST, "/sync/post/no_dec", sync_without_decorator)
app.add_route(HttpMethod.GET, "/async/get/no_dec", async_without_decorator)
app.add_route(HttpMethod.PUT, "/async/put/no_dec", async_without_decorator)
app.add_route(HttpMethod.POST, "/async/post/no_dec", async_without_decorator)
app.add_route("GET", "/sync/get/no_dec", sync_without_decorator)
app.add_route("PUT", "/sync/put/no_dec", sync_without_decorator)
app.add_route("POST", "/sync/post/no_dec", sync_without_decorator)
app.add_route("GET", "/async/get/no_dec", async_without_decorator)
app.add_route("PUT", "/async/put/no_dec", async_without_decorator)
app.add_route("POST", "/async/post/no_dec", async_without_decorator)

# ===== Main =====

Expand Down
15 changes: 13 additions & 2 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import multiprocess as mp
import os
from typing import Callable, List, Optional, Tuple
from typing import Callable, List, Optional, Tuple, Union
from nestd import get_all_nested


Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(self, file_object: str, config: Config = Config()) -> None:

def add_route(
self,
route_type: HttpMethod,
route_type: Union[HttpMethod, str],
endpoint: str,
handler: Callable,
is_const: bool = False,
Expand All @@ -87,6 +87,17 @@ def add_route(
"""
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]

return self.router.add_route(
route_type, endpoint, handler, is_const, self.exception_handler
Expand Down

0 comments on commit 7826a98

Please sign in to comment.