(20241214) SMS also ported to the 'tokenKey' system.

This commit is contained in:
2024-12-14 13:02:57 +05:30
parent dc181c4995
commit 364894c2af
4 changed files with 42 additions and 31 deletions
+18 -6
View File
@@ -145,26 +145,38 @@ async def send_sms(
:return: A standard response structure. :return: A standard response structure.
""" """
# ┏┓ # ┏┓ ┓ ┏┓┓ ┓
# ┃┃┏┓┏┓┏┓┏┓┏┓┏┏┓┏ # ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃
# ┣┛┛ ┗ ┣┛┛ ┗┛┗┗ ┛┛ # ┛┗┗┻┗┛┗ ┗┛┗┗ ┗┛┗
# ┛
# If the session token is invalid/expired: # If the session token is invalid/expired:
if kwargs.get("session_info") is None: if kwargs.get("session_info") is None:
return ResponseModel( return ResponseModel(
status_code = StatusCodes.FAILED, status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED http_code = HttpCodes.UNAUTHORIZED,
message = "invalid session"
) )
# ┏┓ ┓ ┏┳┓┓ ┏┓┳┳┓┏┓ # ┏┓ ┓ ┏┳┓┓ ┏┓┳┳┓┏┓
# ┗┓┏┓┏┓┏┫ ┃ ┣┓┏┓ ┗┓┃┃┃┗┓ # ┗┓┏┓┏┓┏┫ ┃ ┣┓┏┓ ┗┓┃┃┃┗┓
# ┗┛┗ ┛┗┗┻ ┻ ┛┗┗ ┗┛┛ ┗┗┛ # ┗┛┗ ┛┗┗┻ ┻ ┛┗┗ ┗┛┛ ┗┗┛
# Get the token from the token key:
auth_token = await current_app.mail_controller.get_token(
mongo_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey
)
if auth_token is None: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = f"no such token key"
)
# Send the SMS:
client_response = await current_app.sms_controller.send( client_response = await current_app.sms_controller.send(
mongo_conn = current_app.data_mongo, mongo_conn = current_app.data_mongo,
http_client = current_app.http_client, http_client = current_app.http_client,
token_id = inbound_data.tokenId, auth_token = auth_token,
messages = inbound_data.message messages = inbound_data.message
) )
+18 -21
View File
@@ -141,7 +141,7 @@ class SMSController:
success = False success = False
# Get a token id: # Get a token id:
token_id = await current_app.core_auth_token_controller.get_token_id( token_key = await current_app.core_auth_token_controller.get_token_key(
db_conn = db_conn, db_conn = db_conn,
mongo_conn = mongo_conn, mongo_conn = mongo_conn,
auth_token = auth_token, auth_token = auth_token,
@@ -153,7 +153,7 @@ class SMSController:
success = await current_app.core_auth_token_controller.set_token( success = await current_app.core_auth_token_controller.set_token(
db_conn = db_conn, db_conn = db_conn,
mongo_conn = mongo_conn, mongo_conn = mongo_conn,
token_id = token_id, token_key = token_key,
auth_token = auth_token, auth_token = auth_token,
token_notes = {}, token_notes = {},
session_token = session_token session_token = session_token
@@ -165,13 +165,13 @@ class SMSController:
@staticmethod @staticmethod
async def get_token( async def get_token(
mongo_conn: AsyncMongo, mongo_conn: AsyncMongo,
token_id: ObjectId | str = None, token_key: ObjectId | str = None,
) -> CoreAuthTokenModel | None: ) -> CoreAuthTokenModel | None:
# Simply call the core model: # Simply call the core model:
return await current_app.core_auth_token_controller.get_token( return await current_app.core_auth_token_controller.get_token(
mongo_conn = mongo_conn, mongo_conn = mongo_conn,
token_id = token_id token_key = token_key
) )
# ┏┓ ┓ # ┏┓ ┓
@@ -181,7 +181,6 @@ class SMSController:
@staticmethod @staticmethod
async def __send_from_nimbus_sms_india( async def __send_from_nimbus_sms_india(
http_client: httpx.AsyncClient, http_client: httpx.AsyncClient,
token_id: ObjectId | str,
auth_token: CoreAuthTokenModel, auth_token: CoreAuthTokenModel,
messages: List[NimbusSMSIndiaMessage], messages: List[NimbusSMSIndiaMessage],
) -> SMSSendManyResults: ) -> SMSSendManyResults:
@@ -215,7 +214,7 @@ class SMSController:
send_results.smsMessages.append(CoreMessageModel( send_results.smsMessages.append(CoreMessageModel(
ts = client_response.ts, ts = client_response.ts,
syncTs = date_time.get_current_utc_date_time(as_string = False), syncTs = date_time.get_current_utc_date_time(as_string = False),
tokenId = ObjectId(token_id), tokenId = auth_token.authTokenId,
serviceType = auth_token.serviceType, serviceType = auth_token.serviceType,
client = auth_token.client, client = auth_token.client,
clientMessageId = client_response.messageId, clientMessageId = client_response.messageId,
@@ -238,7 +237,6 @@ class SMSController:
@staticmethod @staticmethod
async def __send_from_savvy_bulk_sms_kenya( async def __send_from_savvy_bulk_sms_kenya(
http_client: httpx.AsyncClient, http_client: httpx.AsyncClient,
token_id: ObjectId | str,
auth_token: CoreAuthTokenModel, auth_token: CoreAuthTokenModel,
messages: List[SavvyBulkSMSKenyaMessage], messages: List[SavvyBulkSMSKenyaMessage],
) -> SMSSendManyResults: ) -> SMSSendManyResults:
@@ -270,7 +268,7 @@ class SMSController:
send_results.smsMessages.append(CoreMessageModel( send_results.smsMessages.append(CoreMessageModel(
ts = client_response.ts, ts = client_response.ts,
syncTs = date_time.get_current_utc_date_time(as_string = False), syncTs = date_time.get_current_utc_date_time(as_string = False),
tokenId = ObjectId(token_id), tokenId = auth_token.authTokenId,
serviceType = auth_token.serviceType, serviceType = auth_token.serviceType,
client = auth_token.client, client = auth_token.client,
clientMessageId = client_response.messageId, clientMessageId = client_response.messageId,
@@ -294,37 +292,36 @@ class SMSController:
self, self,
mongo_conn: AsyncMongo, mongo_conn: AsyncMongo,
http_client: httpx.AsyncClient, http_client: httpx.AsyncClient,
token_id: ObjectId | str, # token_id: ObjectId | str,
auth_token: CoreAuthTokenModel,
messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage] messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage]
) -> SMSSendManyResults: ) -> SMSSendManyResults:
# Start by assuming failure: # Start by assuming failure:
send_results = SMSSendManyResults() send_results = SMSSendManyResults()
# We first load the authorization tokens: # # We first load the authorization tokens:
auth_token = await self.get_token( # auth_token = await self.get_token(
mongo_conn = mongo_conn, # mongo_conn = mongo_conn,
token_id = token_id, # token_id = token_id,
) # )
#
# If we failed to load the authorization tokens: # # If we failed to load the authorization tokens:
if not auth_token: # if not auth_token:
send_results.message = f"no such token id '{token_id}'" # send_results.message = f"no such token id '{token_id}'"
return send_results # return send_results
# Now we route the message to the appropriate client: # Now we route the message to the appropriate client:
match auth_token.client: match auth_token.client:
case "nimbusSmsIndia": case "nimbusSmsIndia":
send_results = await self.__send_from_nimbus_sms_india( send_results = await self.__send_from_nimbus_sms_india(
http_client = http_client, http_client = http_client,
token_id = token_id,
auth_token = auth_token, auth_token = auth_token,
messages = messages messages = messages
) )
case "savvyBulkSmsKenya": case "savvyBulkSmsKenya":
send_results = await self.__send_from_savvy_bulk_sms_kenya( send_results = await self.__send_from_savvy_bulk_sms_kenya(
http_client = http_client, http_client = http_client,
token_id = token_id,
auth_token = auth_token, auth_token = auth_token,
messages = messages messages = messages
) )
+2 -2
View File
@@ -177,7 +177,7 @@ class SMSSendRequestHeaders(BaseModel):
class SMSSendRequestData(BaseModel): class SMSSendRequestData(BaseModel):
tokenId: ObjectId = Field(description = "the auth token to use to send this message") tokenKey: ObjectId = Field(description = "the auth token to use to send this message")
message: List[NimbusSMSIndiaMessage] | List[SavvyBulkSMSKenyaMessage] message: List[NimbusSMSIndiaMessage] | List[SavvyBulkSMSKenyaMessage]
# ┏┓ ┏• # ┏┓ ┏•
@@ -193,7 +193,7 @@ class SMSSendRequestData(BaseModel):
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓ # ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗ # ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("tokenId", mode = "before") @field_validator("tokenKey", mode = "before")
def parse_oid(cls, value): def parse_oid(cls, value):
try: value = ObjectId(value) try: value = ObjectId(value)
except: pass except: pass
+4 -2
View File
@@ -4,6 +4,10 @@
# Run this on the development machine. # Run this on the development machine.
# NOT to be used in a collaborative environment: # NOT to be used in a collaborative environment:
# Accept the name of the branch that you want to work on:
echo "Branch : "
read -r BRANCH
# Accept a comment from the terminal: # Accept a comment from the terminal:
echo "Comment: " echo "Comment: "
read -r COMMENT read -r COMMENT
@@ -17,7 +21,5 @@ git commit -m "$COMMENT"
echo "Commit done." echo "Commit done."
# Push th commit to git: # Push th commit to git:
echo "Branch : "
read -r BRANCH
git push -u https://wtt.ditscentre.in/ditscentre/api_utils_converse_v2.git "$BRANCH" git push -u https://wtt.ditscentre.in/ditscentre/api_utils_converse_v2.git "$BRANCH"
echo "Attempt done. Exiting." echo "Attempt done. Exiting."