@@ -39,11 +39,18 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
3939# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
4040# License for the specific language governing permissions and limitations
4141# under the License.
42+ import json
43+
4244from typing import Any, Callable, Coroutine, Dict, Union, List, Optional
4345
46+ from supertokens_python import Supertokens
4447from supertokens_python.framework.fastapi.fastapi_request import FastApiRequest
48+ from supertokens_python.framework.fastapi.fastapi_response import FastApiResponse
4549from supertokens_python.recipe.session import SessionRecipe
50+ from supertokens_python.exceptions import SuperTokensError
4651from supertokens_python.types import MaybeAwaitable
52+ from fastapi.responses import JSONResponse
53+ from fastapi import Request
4754
4855from ...interfaces import SessionContainer, SessionClaimValidator
4956
@@ -62,13 +69,12 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
6269) -> Callable[..., Coroutine[Any, Any, Union[SessionContainer, None]]]:
6370 if user_context is None:
6471 user_context = {}
65- from fastapi import Request
6672
6773 async def func(request: Request) -> Union[SessionContainer, None]:
68- baseRequest = FastApiRequest(request)
74+ base_req = FastApiRequest(request)
6975 recipe = SessionRecipe.get_instance()
7076 session = await recipe.verify_session(
71- baseRequest ,
77+ base_req ,
7278 anti_csrf_check,
7379 session_required,
7480 check_database,
@@ -78,12 +84,31 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
7884 if session is None:
7985 if session_required:
8086 raise Exception("Should never come here")
81- baseRequest .set_session_as_none()
87+ base_req .set_session_as_none()
8288 else:
83- baseRequest .set_session(session)
84- return baseRequest .get_session()
89+ base_req .set_session(session)
90+ return base_req .get_session()
8591
86- return func</ code > </ pre >
92+ return func
93+
94+
95+ async def session_exception_handler(
96+ request: Request, exc: SuperTokensError
97+ ) -> JSONResponse:
98+ """FastAPI exceptional handler for errors raised by Supertokens SDK when not using middleware
99+
100+ Usage: `app.add_exception_handler(SuperTokensError, st_exception_handler)`
101+ """
102+ base_req = FastApiRequest(request)
103+ base_res = FastApiResponse(JSONResponse())
104+ result = await Supertokens.get_instance().handle_supertokens_error(
105+ base_req, exc, base_res
106+ )
107+ if isinstance(result, FastApiResponse):
108+ body = json.loads(result.response.body)
109+ return JSONResponse(body, status_code=result.response.status_code)
110+
111+ raise Exception("Should never come here")</ code > </ pre >
87112</ details >
88113</ section >
89114< section >
@@ -93,6 +118,35 @@ <h1 class="title">Module <code>supertokens_python.recipe.session.framework.fasta
93118< section >
94119< h2 class ="section-title " id ="header-functions "> Functions</ h2 >
95120< dl >
121+ < dt id ="supertokens_python.recipe.session.framework.fastapi.session_exception_handler "> < code class ="name flex ">
122+ < span > async def < span class ="ident "> session_exception_handler</ span > </ span > (< span > request: starlette.requests.Request, exc: < a title ="supertokens_python.exceptions.SuperTokensError " href ="../../../../exceptions.html#supertokens_python.exceptions.SuperTokensError "> SuperTokensError</ a > ) ‑> starlette.responses.JSONResponse</ span >
123+ </ code > </ dt >
124+ < dd >
125+ < div class ="desc "> < p > FastAPI exceptional handler for errors raised by Supertokens SDK when not using middleware</ p >
126+ < p > Usage: < code > app.add_exception_handler(SuperTokensError, st_exception_handler)</ code > </ p > </ div >
127+ < details class ="source ">
128+ < summary >
129+ < span > Expand source code</ span >
130+ </ summary >
131+ < pre > < code class ="python "> async def session_exception_handler(
132+ request: Request, exc: SuperTokensError
133+ ) -> JSONResponse:
134+ """FastAPI exceptional handler for errors raised by Supertokens SDK when not using middleware
135+
136+ Usage: `app.add_exception_handler(SuperTokensError, st_exception_handler)`
137+ """
138+ base_req = FastApiRequest(request)
139+ base_res = FastApiResponse(JSONResponse())
140+ result = await Supertokens.get_instance().handle_supertokens_error(
141+ base_req, exc, base_res
142+ )
143+ if isinstance(result, FastApiResponse):
144+ body = json.loads(result.response.body)
145+ return JSONResponse(body, status_code=result.response.status_code)
146+
147+ raise Exception("Should never come here")</ code > </ pre >
148+ </ details >
149+ </ dd >
96150< dt id ="supertokens_python.recipe.session.framework.fastapi.verify_session "> < code class ="name flex ">
97151< span > def < span class ="ident "> verify_session</ span > </ span > (< span > anti_csrf_check: Optional[bool] = None, session_required: bool = True, check_database: bool = False, override_global_claim_validators: Optional[Callable[[List[< a title ="supertokens_python.recipe.session.interfaces.SessionClaimValidator " href ="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionClaimValidator "> SessionClaimValidator</ a > ], < a title ="supertokens_python.recipe.session.interfaces.SessionContainer " href ="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionContainer "> SessionContainer</ a > , Dict[str, Any]], Union[Awaitable[List[< a title ="supertokens_python.recipe.session.interfaces.SessionClaimValidator " href ="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionClaimValidator "> SessionClaimValidator</ a > ]], List[< a title ="supertokens_python.recipe.session.interfaces.SessionClaimValidator " href ="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionClaimValidator "> SessionClaimValidator</ a > ]]]] = None, user_context: Optional[Dict[str, Any]] = None) ‑> Callable[..., Coroutine[Any, Any, Optional[< a title ="supertokens_python.recipe.session.interfaces.SessionContainer " href ="../../interfaces.html#supertokens_python.recipe.session.interfaces.SessionContainer "> SessionContainer</ a > ]]]</ span >
98152</ code > </ dt >
@@ -116,13 +170,12 @@ <h2 class="section-title" id="header-functions">Functions</h2>
116170) -> Callable[..., Coroutine[Any, Any, Union[SessionContainer, None]]]:
117171 if user_context is None:
118172 user_context = {}
119- from fastapi import Request
120173
121174 async def func(request: Request) -> Union[SessionContainer, None]:
122- baseRequest = FastApiRequest(request)
175+ base_req = FastApiRequest(request)
123176 recipe = SessionRecipe.get_instance()
124177 session = await recipe.verify_session(
125- baseRequest ,
178+ base_req ,
126179 anti_csrf_check,
127180 session_required,
128181 check_database,
@@ -132,10 +185,10 @@ <h2 class="section-title" id="header-functions">Functions</h2>
132185 if session is None:
133186 if session_required:
134187 raise Exception("Should never come here")
135- baseRequest .set_session_as_none()
188+ base_req .set_session_as_none()
136189 else:
137- baseRequest .set_session(session)
138- return baseRequest .get_session()
190+ base_req .set_session(session)
191+ return base_req .get_session()
139192
140193 return func</ code > </ pre >
141194</ details >
@@ -158,6 +211,7 @@ <h2>Index</h2>
158211</ li >
159212< li > < h3 > < a href ="#header-functions "> Functions</ a > </ h3 >
160213< ul class ="">
214+ < li > < code > < a title ="supertokens_python.recipe.session.framework.fastapi.session_exception_handler " href ="#supertokens_python.recipe.session.framework.fastapi.session_exception_handler "> session_exception_handler</ a > </ code > </ li >
161215< li > < code > < a title ="supertokens_python.recipe.session.framework.fastapi.verify_session " href ="#supertokens_python.recipe.session.framework.fastapi.verify_session "> verify_session</ a > </ code > </ li >
162216</ ul >
163217</ li >
0 commit comments