(20250620) Minor upgrade to the SMS sending util.
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
Tuesday, 10th Sept., 2024.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To provide a data structure for the JSOn received in the API calls to send SMSs through Nimbus IT's service.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
N/A
|
||||
|
||||
DOWNLOADS:
|
||||
|
||||
N/A
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** IMPORT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
# For making data models:
|
||||
from pydantic import BaseModel, Field, field_validator, Extra
|
||||
from typing import Optional, Any, Dict
|
||||
from typing_extensions import Annotated
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import regex
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class NimbusSendSMS(BaseModel):
|
||||
|
||||
"""
|
||||
This data model is used when the API call is made to send an SMS message.
|
||||
'entityId': is the id given to you by DLT.
|
||||
'templateId' is the id given to you by DLT for a template of a message.
|
||||
'recipientNo' is the phone number of the person you want to send the message to.
|
||||
'senderId' 6-char code like "HDFCBK", "NSESMS", "ZRODHA" that you see in your SMS inbox.
|
||||
'userId' is the 6-digit id given to you by Nimbus.
|
||||
'apiKey' is the key generated on Nimbus's portal.
|
||||
"""
|
||||
|
||||
entityId: str | int
|
||||
templateId: str | int
|
||||
recipientNo: str | int
|
||||
message: str
|
||||
senderId: str | int
|
||||
userId: str | int
|
||||
apiKey: str
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
def get(self, key: str, default = None):
|
||||
return getattr(self, key, default)
|
||||
|
||||
@field_validator(
|
||||
"entityId",
|
||||
"templateId",
|
||||
"recipientNo",
|
||||
"senderId"
|
||||
)
|
||||
def validate_fields(cls, value):
|
||||
if isinstance(value, str): return value
|
||||
elif isinstance(value, int): return str(value)
|
||||
raise ValueError
|
||||
|
||||
@field_validator("userId")
|
||||
def validate_user_id(cls, value):
|
||||
if isinstance(value, int): value = str(value)
|
||||
if regex.match(text = value, pattern = r"^[\d]{6}$"): return value
|
||||
raise ValueError("userId must be 6-digits long")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class NimbusGetBalance(BaseModel):
|
||||
|
||||
"""
|
||||
This data model is used when the API call is made to check how much balance is remaining in your Nimbus wallet.
|
||||
'userId' is the 6-digit id given to you by Nimbus.
|
||||
'apiKey' is the key generated on Nimbus's portal.
|
||||
"""
|
||||
|
||||
userId: str | int
|
||||
apiKey: str
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
def get(self, key: str, default = None):
|
||||
return getattr(self, key, default)
|
||||
|
||||
@field_validator("userId")
|
||||
def validate_user_id(cls, value):
|
||||
if isinstance(value, int): value = str(value)
|
||||
if regex.match(text = value, pattern = r"^[\d]{6}$"): return value
|
||||
raise ValueError("userId must be 6-digits long")
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
from utils_v2.string import json
|
||||
|
||||
my_msg = NimbusSendSMS(
|
||||
entityId = 123,
|
||||
templateId = 456,
|
||||
recipientNo = "789",
|
||||
message = "Hello, World!",
|
||||
senderId = "TCAOFF",
|
||||
userId = "123456",
|
||||
apiKey = "123@ABC"
|
||||
)
|
||||
|
||||
print(json.to_string(my_msg.model_dump()))
|
||||
Reference in New Issue
Block a user