-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
114 changed files
with
1,260 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from litestar import Litestar, Request, get | ||
from litestar.connection.base import empty_receive, empty_send | ||
from litestar.enums import HttpMethod | ||
from litestar.types import Receive, Scope, Send | ||
|
||
KITTEN_NAMES_MAP = { | ||
HttpMethod.GET: "Whiskers", | ||
} | ||
|
||
|
||
class CustomRequest(Request): | ||
"""Enrich request with the kitten name.""" | ||
|
||
__slots__ = ("kitten_name",) | ||
|
||
def __init__(self, scope: Scope, receive: Receive = empty_receive, send: Send = empty_send) -> None: | ||
"""Initialize CustomRequest class.""" | ||
super().__init__(scope=scope, receive=receive, send=send) | ||
self.kitten_name = KITTEN_NAMES_MAP.get(scope["method"], "Mittens") | ||
|
||
|
||
@get(path="/kitten-name") | ||
def get_kitten_name(request: CustomRequest) -> str: | ||
"""Get kitten name based on the HTTP method.""" | ||
return request.kitten_name | ||
|
||
|
||
app = Litestar( | ||
route_handlers=[get_kitten_name], | ||
request_class=CustomRequest, | ||
debug=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import Any, Dict | ||
|
||
import litestar.status_codes | ||
from litestar import Litestar, get | ||
|
||
|
||
@get("/resources", status_code=litestar.status_codes.HTTP_418_IM_A_TEAPOT, media_type="application/problem+json") | ||
async def retrieve_resource() -> Dict[str, Any]: | ||
return { | ||
"title": "Server thinks it is a teapot", | ||
"type": "Server delusion", | ||
"status": litestar.status_codes.HTTP_418_IM_A_TEAPOT, | ||
} | ||
|
||
|
||
app = Litestar(route_handlers=[retrieve_resource]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
from litestar import Litestar, WebSocket, websocket_listener | ||
from litestar.types.asgi_types import WebSocketMode | ||
|
||
|
||
class CustomWebSocket(WebSocket): | ||
async def receive_data(self, mode: WebSocketMode) -> str | bytes: | ||
"""Return fixed response for every websocket message.""" | ||
await super().receive_data(mode=mode) | ||
return "Fixed response" | ||
|
||
|
||
@websocket_listener("/") | ||
async def handler(data: str) -> str: | ||
return data | ||
|
||
|
||
app = Litestar([handler], websocket_class=CustomWebSocket) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.