From 28d8059a81677eb7799a623b334c6f07eabdc549 Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 2 Dec 2023 03:40:28 +0530 Subject: [PATCH] Fix request headers --- integration_tests/base_routes.py | 2 -- robyn/robyn.pyi | 20 +++++++++++++------- robyn/router.py | 7 ------- src/types/request.rs | 4 +--- unit_tests/test_request_object.py | 5 ++--- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/integration_tests/base_routes.py b/integration_tests/base_routes.py index 886ee31ed..193b7e62b 100644 --- a/integration_tests/base_routes.py +++ b/integration_tests/base_routes.py @@ -161,7 +161,6 @@ async def async_before_request(request: Request): @app.after_request("/async/middlewares") async def async_after_request(response: Response): - # response.headers["after"] = "async_after_request" response.headers.set("after", "async_after_request") response.description = response.description + " after" return response @@ -170,7 +169,6 @@ async def async_after_request(response: Response): @app.get("/async/middlewares") async def async_middlewares(request: Request): assert request.headers.contains("before") - # assert request.headers["before"] == "async_before_request" assert request.headers.get("before") == "async_before_request" assert request.ip_addr == "127.0.0.1" return "async middlewares" diff --git a/robyn/robyn.pyi b/robyn/robyn.pyi index a261d7c80..14fec95a8 100644 --- a/robyn/robyn.pyi +++ b/robyn/robyn.pyi @@ -65,7 +65,6 @@ class Identity: -@dataclass class QueryParams: """ The query params object passed to the route handler. @@ -74,8 +73,6 @@ class QueryParams: queries (dict[str, list[str]]): The query parameters of the request. e.g. /user?id=123 -> {"id": "123"} """ - queries: dict[str, list[str]] - def set(self, key: str, value: str) -> None: """ Sets the value of the query parameter with the given key. @@ -172,13 +169,12 @@ class Headers: """ pass - def get(self, key: str, default: Optional[str]) -> Optional[str]: + def get(self, key: str) -> Optional[str]: """ Gets the last value of the header with the given key. Args: key (str): The key of the header - default (Optional[str]): The default value if the key does not exist """ pass @@ -191,6 +187,16 @@ class Headers: """ pass + def contains(self, key: str) -> bool: + """ + Returns: + True if the headers contain the key, False otherwise + + Args: + key (str): The key of the header + """ + pass + def is_empty(self) -> bool: pass @@ -203,7 +209,7 @@ class Request: Attributes: query_params (QueryParams): The query parameters of the request. e.g. /user?id=123 -> {"id": "123"} - headers (dict[str, str]): The headers of the request. e.g. {"Content-Type": "application/json"} + headers Headers: The headers of the request. e.g. Headers({"Content-Type": "application/json"}) params (dict[str, str]): The parameters of the request. e.g. /user/:id -> {"id": "123"} body (Union[str, bytes]): The body of the request. If the request is a JSON, it will be a dict. method (str): The method of the request. e.g. GET, POST, PUT, DELETE @@ -211,7 +217,7 @@ class Request: """ query_params: QueryParams - headers: dict[str, str] + headers: Headers path_params: dict[str, str] body: Union[str, bytes] method: str diff --git a/robyn/router.py b/robyn/router.py index 28217fb6d..13dd12e64 100644 --- a/robyn/router.py +++ b/robyn/router.py @@ -46,10 +46,8 @@ def _format_response( self, res: dict, ) -> Response: - # TODO: Add support for custom headers headers = Headers({"Content-Type": "text/plain"}) - # we should create a header object here response = {} if isinstance(res, dict): # this should change @@ -59,7 +57,6 @@ def _format_response( if not headers.contains("Content-Type"): headers.set("Content-Type", "text/plain") - description = res.get("description", "") if not isinstance(status_code, int): @@ -100,14 +97,12 @@ async def async_inner_handler(*args): try: response = self._format_response( await handler(*args), - # response_headers, ) except Exception as err: if exception_handler is None: raise response = self._format_response( exception_handler(err), - # response_headers, ) return response @@ -116,14 +111,12 @@ def inner_handler(*args): try: response = self._format_response( handler(*args), - # response_headers, ) except Exception as err: if exception_handler is None: raise response = self._format_response( exception_handler(err), - # response_headers, ) return response diff --git a/src/types/request.rs b/src/types/request.rs index 439c3a7d2..fce8ac074 100644 --- a/src/types/request.rs +++ b/src/types/request.rs @@ -109,7 +109,7 @@ impl PyRequest { #[new] #[allow(clippy::too_many_arguments)] pub fn new( - query_params: &PyDict, + query_params: QueryParams, headers: Py, path_params: Py, body: Py, @@ -118,8 +118,6 @@ impl PyRequest { identity: Option, ip_addr: Option, ) -> Self { - let query_params = QueryParams::from_py_dict(query_params); - Self { query_params, headers, diff --git a/unit_tests/test_request_object.py b/unit_tests/test_request_object.py index beda577ba..f1f46cd88 100644 --- a/unit_tests/test_request_object.py +++ b/unit_tests/test_request_object.py @@ -1,4 +1,4 @@ -from robyn.robyn import Headers, Request, Url +from robyn.robyn import Headers, QueryParams, Request, Url def test_request_object(): @@ -8,8 +8,7 @@ def test_request_object(): path="/user", ) request = Request( - query_params={}, - # headers={"Content-Type": "application/json"}, + query_params=QueryParams(), headers=Headers({"Content-Type": "application/json"}), path_params={}, body="",