Skip to content

Commit

Permalink
fix CI, fix direct responses, handlers are required for cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
devkral committed Jan 17, 2025
1 parent 5748808 commit ff97e42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
32 changes: 20 additions & 12 deletions esmerald/routing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ def _get_response_container_handler(
cookies: ResponseCookies,
headers: Dict[str, Any],
media_type: str,
) -> Callable[[ResponseContainer, Type[Esmerald], Dict[str, Any]], LilyaResponse]:
) -> Callable[
[Union[ResponseContainer, LilyaResponse], Type[Esmerald], Dict[str, Any]], LilyaResponse
]:
"""
Creates a handler for ResponseContainer types.
Expand All @@ -290,22 +292,30 @@ def _get_response_container_handler(
"""

async def response_content(
data: ResponseContainer, app: Type["Esmerald"], **kwargs: Dict[str, Any]
data: Union[ResponseContainer, LilyaResponse],
app: Type["Esmerald"],
**kwargs: Dict[str, Any],
) -> LilyaResponse:
_headers = {**self.get_headers(headers), **data.headers}
_cookies = self.get_cookies(data.cookies, cookies)
response: Response = data.to_response(
app=app,
headers=_headers,
status_code=data.status_code or self.status_code,
media_type=media_type,
)
if isinstance(data, LilyaResponse):
response: LilyaResponse = data
else:
response = data.to_response(
app=app,
headers=_headers,
status_code=data.status_code or self.status_code,
media_type=media_type,
)
for cookie in _cookies:
response.set_cookie(**cookie)
return response

return cast(
Callable[[ResponseContainer, Type["Esmerald"], Dict[str, Any]], LilyaResponse],
Callable[
[Union[ResponseContainer, LilyaResponse], Type["Esmerald"], Dict[str, Any]],
LilyaResponse,
],
response_content,
)

Expand Down Expand Up @@ -435,7 +445,7 @@ def _get_default_handler(
async def response_content(data: Any, **kwargs: Dict[str, Any]) -> LilyaResponse:
data = await self.get_response_data(data=data)
_cookies = self.get_cookies(cookies)
if isinstance(data, JSONResponse):
if isinstance(data, LilyaResponse):
response = data
response.status_code = self.status_code
response.background = self.background
Expand Down Expand Up @@ -523,8 +533,6 @@ async def get_response_for_request(
parameter_model=parameter_model,
request=request,
)
if isinstance(response_data, LilyaResponse):
return response_data

response = await self.to_response(
app=scope["app"],
Expand Down
2 changes: 0 additions & 2 deletions esmerald/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2270,8 +2270,6 @@ def validate_handler(self) -> None:
self.validate_reserved_kwargs()

async def to_response(self, app: "Esmerald", data: Any) -> LilyaResponse:
if isinstance(data, LilyaResponse):
return data
response_handler = self.get_response_for_handler()
return await response_handler(app=app, data=data) # type: ignore[call-arg]

Expand Down

0 comments on commit ff97e42

Please sign in to comment.