Skip to content

Commit 1cd0d19

Browse files
committed
feat: refactor Litestar framework integration and update middleware imports
1 parent 4ebe995 commit 1cd0d19

File tree

11 files changed

+66
-63
lines changed

11 files changed

+66
-63
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ freeze-dev-requirements:
2626
with-fastapi:
2727
pip3 install -e .[fastapi]
2828

29+
with-litestar:
30+
pip3 install -e .[litestar]
31+
2932
with-django:
3033
pip3 install -e .[django]
3134

examples/with-litestar/with-thirdpartyemailpassword/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
![SuperTokens banner](https://raw.githubusercontent.com/supertokens/supertokens-logo/master/images/Artboard%20%E2%80%93%2027%402x.png)
22

3-
# FastAPI example
3+
# Litestar example
44

5-
This is an example to show how to use supertokens-python library with FastAPI framework and ThirdpartyEmailpassword recipe.
5+
This is an example to show how to use supertokens-python library with Litestar framework and ThirdpartyEmailpassword recipe.
66

77
This server code can be used to implement social auth using:
88
- Google

examples/with-litestar/with-thirdpartyemailpassword/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
get_all_cors_headers,
1313
init,
1414
)
15-
from supertokens_python.framework.litestar.litestar_middleware import LitestarMiddleware
15+
from supertokens_python.framework.litestar.middleware import LitestarMiddleware
1616
from supertokens_python.recipe import (
1717
dashboard,
1818
emailverification,

supertokens_python/framework/litestar/framework.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
14-
from __future__ import annotations
15-
1614
from typing import TYPE_CHECKING, Any
1715

1816
from supertokens_python.framework.types import Framework
@@ -23,7 +21,7 @@
2321

2422
class LitestarFramework(Framework):
2523
def wrap_request(self, unwrapped: Request[Any, Any, Any]):
26-
from supertokens_python.framework.litestar.litestar_request import (
24+
from supertokens_python.framework.litestar.request import (
2725
LitestarRequest,
2826
)
2927

supertokens_python/framework/litestar/litestar_middleware.py renamed to supertokens_python/framework/litestar/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from litestar.types import ASGIApp, Message, Receive, Scope, Send
88
from supertokens_python import Supertokens
99
from supertokens_python.exceptions import SuperTokensError
10-
from supertokens_python.framework.litestar.litestar_request import LitestarRequest
11-
from supertokens_python.framework.litestar.litestar_response import LitestarResponse
10+
from supertokens_python.framework.litestar.request import LitestarRequest
11+
from supertokens_python.framework.litestar.response import LitestarResponse
1212
from supertokens_python.recipe.session import SessionContainer
1313
from supertokens_python.supertokens import manage_session_post_response
1414
from supertokens_python.utils import default_user_context

supertokens_python/framework/litestar/litestar_request.py renamed to supertokens_python/framework/litestar/request.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
14+
import json
1415
from typing import Any, Dict, Union
1516
from urllib.parse import parse_qsl
1617

1718
from litestar import Request
18-
from litestar.exceptions import SerializationException
1919
from supertokens_python.framework.request import BaseRequest
2020
from supertokens_python.recipe.session.interfaces import SessionContainer
2121

@@ -36,10 +36,17 @@ def get_query_param(
3636
def get_query_params(self) -> Dict[str, Any]:
3737
return dict(self.request.query_params.items()) # type: ignore
3838

39-
async def json(self) -> Union[Any, None]:
39+
async def json(self) -> dict:
40+
"""
41+
Read the entire ASGI stream and JSON-decode it,
42+
sidestepping Litestar’s internal max-body-size logic.
43+
"""
44+
body_bytes = b"".join([chunk async for chunk in self.request.stream()])
45+
if not body_bytes:
46+
return {}
4047
try:
41-
return await self.request.json()
42-
except SerializationException:
48+
return json.loads(body_bytes)
49+
except json.JSONDecodeError:
4350
return {}
4451

4552
def method(self) -> str:
@@ -61,7 +68,7 @@ def set_session_as_none(self):
6168
self.request.state.supertokens = None
6269

6370
def get_path(self) -> str:
64-
return self.request.url.path
71+
return self.request.scope["raw_path"].decode("utf-8")
6572

6673
async def form_data(self):
6774
return dict(parse_qsl((await self.request.body()).decode("utf-8")))

supertokens_python/framework/litestar/litestar_response.py renamed to supertokens_python/framework/litestar/response.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
from math import ceil
1515
from typing import Any, Dict, Literal, Optional
1616

17+
from litestar import Response
18+
from litestar.serialization import encode_json
1719
from supertokens_python.framework.response import BaseResponse
1820
from supertokens_python.utils import get_timestamp_ms
1921

2022

2123
class LitestarResponse(BaseResponse):
22-
from litestar import Response
23-
2424
def __init__(self, response: Response[Any]):
2525
super().__init__({})
2626
self.response = response
@@ -76,8 +76,6 @@ def set_status_code(self, status_code: int):
7676

7777
def set_json_content(self, content: Dict[str, Any]):
7878
if not self.response_sent:
79-
from litestar.serialization import encode_json
80-
8179
body = encode_json(
8280
content,
8381
)

supertokens_python/recipe/session/framework/litestar/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818

1919
from supertokens_python import Supertokens
2020
from supertokens_python.exceptions import SuperTokensError
21-
from supertokens_python.framework.litestar.litestar_request import LitestarRequest
22-
from supertokens_python.framework.litestar.litestar_response import LitestarResponse
23-
from supertokens_python.recipe.session import SessionRecipe
21+
from supertokens_python.framework.litestar.request import LitestarRequest
22+
from supertokens_python.framework.litestar.response import LitestarResponse
23+
from supertokens_python.recipe.session import SessionContainer, SessionRecipe
24+
from supertokens_python.recipe.session.interfaces import SessionClaimValidator
2425
from supertokens_python.types import MaybeAwaitable
2526
from supertokens_python.utils import (
2627
default_user_context,
2728
set_request_in_user_context_if_not_defined,
2829
)
2930

30-
from ...interfaces import SessionClaimValidator, SessionContainer
31-
3231

3332
def verify_session(
3433
anti_csrf_check: Union[bool, None] = None,

supertokens_python/supertokens.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ def __init__(
154154
self.website_base_path = NormalisedURLPath(website_base_path)
155155
if mode is not None:
156156
self.mode = mode
157-
elif framework == "fastapi":
158-
mode = "asgi"
159-
elif framework == "litestar":
157+
elif framework in ["fastapi", "litestar"]:
160158
mode = "asgi"
161159
else:
162160
mode = "wsgi"
File renamed without changes.

0 commit comments

Comments
 (0)