-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
adeb05e
commit 3555989
Showing
9 changed files
with
93 additions
and
10 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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
from .database import shutdown_db, startup_db # noqa: F401 | ||
from .email import get_email_settings # noqa: F401 | ||
from .settings import get_settings # noqa: F401 | ||
from .sms import get_sms_config # noqa: F401 | ||
from .swaggers import enable_endpoint # noqa: F401 | ||
from .token import get_token_settings # noqa: F401 | ||
|
||
settings = get_settings() | ||
sms_config = get_sms_config() | ||
jwt_settings = get_token_settings() | ||
email_settings = get_email_settings() | ||
enable_endpoint = enable_endpoint() | ||
|
||
__all__ = ["settings", "startup_db", "shutdown_db", "email_settings", "jwt_settings", "enable_endpoint"] | ||
__all__ = ["settings", "startup_db", "shutdown_db", "email_settings", "jwt_settings", "sms_config", "enable_endpoint"] |
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,16 @@ | ||
from functools import lru_cache | ||
|
||
from pydantic import Field | ||
from pydantic_settings import BaseSettings | ||
|
||
|
||
class SmsBaseConfig(BaseSettings): | ||
SMS_ENDPOINT: str = Field(..., alias="SMS_ENDPOINT") | ||
SMS_USERNAME: str = Field(..., alias="SMS_USERNAME") | ||
SMS_SENDER: str = Field(..., alias="SMS_SENDER") | ||
SMS_TOKEN: str = Field(..., alias="SMS_TOKEN") | ||
|
||
|
||
@lru_cache | ||
def get_sms_config() -> SmsBaseConfig: | ||
return SmsBaseConfig() |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from .send_email import email_sender_handler # noqa: F401 | ||
from .send_email import email_sender_handler | ||
from .send_sms import sms_sender_handler | ||
from .utils import TokenBlacklistHandler | ||
|
||
mail_service = email_sender_handler() | ||
sms_service = sms_sender_handler() | ||
blacklist_token = TokenBlacklistHandler() | ||
|
||
__all__ = ["mail_service", "blacklist_token"] | ||
__all__ = ["mail_service", "sms_service", "blacklist_token"] |
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,50 @@ | ||
import httpx | ||
import logging | ||
from starlette.background import BackgroundTasks | ||
|
||
from src.config import sms_config | ||
|
||
|
||
logging.basicConfig(format="%(message)s", level=logging.INFO) | ||
_log = logging.getLogger(__name__) | ||
|
||
|
||
class SendSMSHandler: | ||
|
||
def __init__(self, url: str, username: str, token: str, sender: str): | ||
self._url = url | ||
self._token = token | ||
self._sender = sender | ||
self._username = username | ||
|
||
async def send_sms(self, recipient: str, message: str): | ||
|
||
async with httpx.AsyncClient() as client: | ||
payload = { | ||
"Username": self._username, | ||
"Token": self._token, | ||
"Dest": recipient, | ||
"Sms": message, | ||
"Sender": self._sender, | ||
} | ||
response = await client.post(self._url, json=payload) | ||
try: | ||
response.raise_for_status() | ||
except httpx.HTTPStatusError as exc: | ||
_log.error(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.") | ||
raise | ||
|
||
return response.text | ||
|
||
def send_sms_background(self, background_task: BackgroundTasks, recipient: str, message: str): | ||
background_task.add_task(self.send_sms, recipient, message) | ||
_log.info("Email scheduled to be sent in the background.") | ||
|
||
|
||
def sms_sender_handler() -> SendSMSHandler: | ||
return SendSMSHandler( | ||
url=sms_config.SMS_ENDPOINT, | ||
username=sms_config.SMS_USERNAME, | ||
token=sms_config.SMS_TOKEN, | ||
sender=sms_config.SMS_SENDER, | ||
) |
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