Skip to content

Commit

Permalink
Fix request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed Dec 1, 2023
1 parent ef65fc6 commit 28d8059
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
2 changes: 0 additions & 2 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
20 changes: 13 additions & 7 deletions robyn/robyn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class Identity:



@dataclass
class QueryParams:
"""
The query params object passed to the route handler.
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -203,15 +209,15 @@ 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
ip_addr (Optional[str]): The IP Address of the client
"""

query_params: QueryParams
headers: dict[str, str]
headers: Headers
path_params: dict[str, str]
body: Union[str, bytes]
method: str
Expand Down
7 changes: 0 additions & 7 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 1 addition & 3 deletions src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl PyRequest {
#[new]
#[allow(clippy::too_many_arguments)]
pub fn new(
query_params: &PyDict,
query_params: QueryParams,
headers: Py<Headers>,
path_params: Py<PyDict>,
body: Py<PyAny>,
Expand All @@ -118,8 +118,6 @@ impl PyRequest {
identity: Option<Identity>,
ip_addr: Option<String>,
) -> Self {
let query_params = QueryParams::from_py_dict(query_params);

Self {
query_params,
headers,
Expand Down
5 changes: 2 additions & 3 deletions unit_tests/test_request_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from robyn.robyn import Headers, Request, Url
from robyn.robyn import Headers, QueryParams, Request, Url


def test_request_object():
Expand All @@ -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="",
Expand Down

0 comments on commit 28d8059

Please sign in to comment.