1616
1717import json
1818from os import environ
19- from typing import Any , Awaitable , Callable , Dict , Union
19+ from typing import Any , Dict
2020
2121from httpx import AsyncClient , HTTPStatusError
2222from supertokens_python .ingredients .emaildelivery import EmailDeliveryInterface
2828from supertokens_python .utils import handle_httpx_client_exceptions
2929
3030
31- def default_create_and_send_custom_email (
32- app_info : AppInfo ,
33- ) -> Callable [[PasswordlessLoginEmailTemplateVars , Dict [str , Any ]], Awaitable [None ]]:
34- async def func (input_ : PasswordlessLoginEmailTemplateVars , _ : Dict [str , Any ]):
35- if ("SUPERTOKENS_ENV" in environ ) and (environ ["SUPERTOKENS_ENV" ] == "testing" ):
36- return
37- data = {
38- "email" : input_ .email ,
39- "appName" : app_info .app_name ,
40- "codeLifetime" : input_ .code_life_time ,
41- }
42- if input_ .url_with_link_code :
43- data ["urlWithLinkCode" ] = input_ .url_with_link_code
44- if input_ .user_input_code :
45- data ["userInputCode" ] = input_ .user_input_code
46- try :
47- async with AsyncClient () as client :
48- resp = await client .post ("https://api.supertokens.io/0/st/auth/passwordless/login" , json = data , headers = {"api-version" : "0" }) # type: ignore
49- resp .raise_for_status ()
50- log_debug_message ("Passwordless login email sent to %s" , input_ .email )
51- except Exception as e :
52- log_debug_message ("Error sending passwordless login email" )
53- handle_httpx_client_exceptions (e , data )
54- # If the error is thrown from the API:
55- if isinstance (e , HTTPStatusError ):
56- body : Dict [str , Any ] = e .response .json () # type: ignore
57- if body .get ("err" ):
58- msg = body ["err" ]
59- else :
60- msg = json .dumps (body )
31+ async def create_and_send_email_with_supertokens_service (
32+ app_info : AppInfo , input_ : PasswordlessLoginEmailTemplateVars
33+ ) -> None :
34+ if ("SUPERTOKENS_ENV" in environ ) and (environ ["SUPERTOKENS_ENV" ] == "testing" ):
35+ return
6136
62- raise Exception (msg )
37+ data = {
38+ "email" : input_ .email ,
39+ "appName" : app_info .app_name ,
40+ "codeLifetime" : input_ .code_life_time ,
41+ }
42+ if input_ .url_with_link_code :
43+ data ["urlWithLinkCode" ] = input_ .url_with_link_code
44+ if input_ .user_input_code :
45+ data ["userInputCode" ] = input_ .user_input_code
6346
64- raise e
47+ try :
48+ async with AsyncClient () as client :
49+ resp = await client .post ("https://api.supertokens.io/0/st/auth/passwordless/login" , json = data , headers = {"api-version" : "0" }) # type: ignore
50+ resp .raise_for_status ()
51+ log_debug_message ("Passwordless login email sent to %s" , input_ .email )
52+ except Exception as e :
53+ log_debug_message ("Error sending passwordless login email" )
54+ handle_httpx_client_exceptions (e , data )
55+ # If the error is thrown from the API:
56+ if isinstance (e , HTTPStatusError ):
57+ body : Dict [str , Any ] = e .response .json () # type: ignore
58+ if body .get ("err" ):
59+ msg = body ["err" ]
60+ else :
61+ msg = json .dumps (body )
6562
66- return func
63+ raise Exception (msg )
64+
65+ raise e
6766
6867
6968class BackwardCompatibilityService (
@@ -72,24 +71,14 @@ class BackwardCompatibilityService(
7271 def __init__ (
7372 self ,
7473 app_info : AppInfo ,
75- create_and_send_custom_email : Union [
76- Callable [
77- [PasswordlessLoginEmailTemplateVars , Dict [str , Any ]], Awaitable [None ]
78- ],
79- None ,
80- ] = None ,
8174 ) -> None :
82- self .create_and_send_custom_email = (
83- create_and_send_custom_email
84- if create_and_send_custom_email is not None
85- else default_create_and_send_custom_email (app_info )
86- )
75+ self .app_info = app_info
8776
8877 async def send_email (
8978 self ,
9079 template_vars : PasswordlessLoginEmailTemplateVars ,
9180 user_context : Dict [str , Any ],
9281 ) -> None :
93- await self . create_and_send_custom_email (
94- template_vars , user_context
82+ await create_and_send_email_with_supertokens_service (
83+ self . app_info , template_vars
9584 ) # Note: intentionally not using try-except (unlike other recipes)
0 commit comments