Skip to content

Commit

Permalink
[communication]-[sms]-Fixes mypy type errors. (Azure#37805)
Browse files Browse the repository at this point in the history
* Fixes mypy type errors.

* Fixed mypy errors

* Fixed pylint annotation errors

---------

Co-authored-by: Dina Saparbaeva <[email protected]>
  • Loading branch information
dinazavyr and Dina Saparbaeva authored Jan 9, 2025
1 parent cff747c commit c7d1d0e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class SmsClient(object): # pylint: disable=client-accepts-api-version-keyword
"""

def __init__(
self,
endpoint, # type: str
credential, # type: Union[TokenCredential, AzureKeyCredential]
**kwargs # type: Any
self,
endpoint, # type: str
credential, # type: Union[TokenCredential, AzureKeyCredential]
**kwargs # type: Any
):
# type: (...) -> None
try:
Expand All @@ -56,9 +56,9 @@ def __init__(

@classmethod
def from_connection_string(
cls,
conn_str, # type: str
**kwargs # type: Any
cls,
conn_str, # type: str
**kwargs # type: Any
): # type: (...) -> SmsClient
"""Create SmsClient from a Connection String.
Expand All @@ -78,33 +78,32 @@ def from_connection_string(
"""
endpoint, access_key = parse_connection_str(conn_str)

return cls(endpoint, access_key, **kwargs)
return cls(endpoint, AzureKeyCredential(access_key), **kwargs)

@distributed_trace
def send(
self,
from_, # type: str
to, # type: Union[str, List[str]]
message, # type: str
*,
enable_delivery_report: bool = False,
tag: Optional[str] = None,
**kwargs: Any
): # type: (...) -> [SmsSendResult]
self,
from_: str,
to: Union[str, List[str]],
message: str,
*,
enable_delivery_report: bool = False,
tag: Optional[str] = None,
**kwargs: Any,
) -> List[SmsSendResult]:
"""Sends SMSs to phone numbers.
:param str from_: The sender of the SMS.
:param to: The single recipient or the list of recipients of the SMS.
:type to: Union[str, List[str]]
:param str message: The message in the SMS.
:param str message: The message in the SMS
:keyword bool enable_delivery_report: Enable this flag to receive a delivery report for this
message on the Azure Resource EventGrid.
:keyword str tag: Use this field to provide metadata that will then be sent back in the corresponding
Delivery Report.
:return: A list of SmsSendResult.
:rtype: [~azure.communication.sms.models.SmsSendResult]
"""

if isinstance(to, str):
to = [to]

Expand All @@ -123,17 +122,17 @@ def send(
**kwargs
)

return self._sms_service_client.sms.send(
response = self._sms_service_client.sms.send(
request,
cls=lambda pr, r, e: [
SmsSendResult(
to=item.to,
message_id=item.message_id,
http_status_code=item.http_status_code,
successful=item.successful,
error_message=item.error_message,
)
for item in r.value
],
**kwargs
)

return [
SmsSendResult(
to=item.to,
message_id=item.message_id,
http_status_code=item.http_status_code,
successful=item.successful,
error_message=item.error_message
) for item in response.value
]
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def from_connection_string(cls, conn_str: str, **kwargs: Any) -> "SmsClient":
:caption: Creating the SmsClient from a connection string.
"""
endpoint, access_key = parse_connection_str(conn_str)
return cls(endpoint, access_key, **kwargs)
return cls(endpoint, AzureKeyCredential(access_key), **kwargs)

@distributed_trace_async
async def send(
Expand Down Expand Up @@ -118,21 +118,21 @@ async def send(
**kwargs,
)

return await self._sms_service_client.sms.send(
response = await self._sms_service_client.sms.send(
request,
cls=lambda pr, r, e: [
SmsSendResult(
to=item.to,
message_id=item.message_id,
http_status_code=item.http_status_code,
successful=item.successful,
error_message=item.error_message,
)
for item in r.value
],
**kwargs,
**kwargs
)

return [
SmsSendResult(
to=item.to,
message_id=item.message_id,
http_status_code=item.http_status_code,
successful=item.successful,
error_message=item.error_message
) for item in response.value
]

async def __aenter__(self) -> "SmsClient":
await self._sms_service_client.__aenter__()
return self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class SmsMultipleRecipientsSample(object):
phone_number = os.getenv("SMS_PHONE_NUMBER")

def send_sms_to_multiple_recipients(self):
if not self.connection_string or not self.phone_number:
raise ValueError(
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
set''')

sms_client = SmsClient.from_connection_string(self.connection_string)

# calling send() with sms values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class SmsMultipleRecipientsSampleAsync(object):
phone_number = os.getenv("SMS_PHONE_NUMBER")

async def send_sms_to_multiple_recipients_async(self):
if not self.connection_string or not self.phone_number:
raise ValueError(
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
set''')

sms_client = SmsClient.from_connection_string(self.connection_string)

async with sms_client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class SmsSingleRecipientSample(object):
phone_number = os.getenv("SMS_PHONE_NUMBER")

def send_sms_to_single_recipient(self):
if not self.connection_string or not self.phone_number:
raise ValueError(
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
set''')

# [START auth_from_connection_string]
sms_client = SmsClient.from_connection_string(self.connection_string)
# [END auth_from_connection_string]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class SmsSingleRecipientSampleAsync(object):
phone_number = os.getenv("SMS_PHONE_NUMBER")

async def send_sms_to_single_recipient_async(self):
if not self.connection_string or not self.phone_number:
raise ValueError(
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
set''')
# [START auth_from_connection_string_async]
sms_client = SmsClient.from_connection_string(self.connection_string)
# [END auth_from_connection_string_async]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class SmsTokenCredentialAuthSample(object):
tenant_id = os.getenv("AZURE_TENANT_ID")

def sms_token_credential_auth(self):
if not self.connection_string or not self.phone_number:
raise ValueError(
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
set''')

# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class SmsTokenCredentialAuthSampleAsync(object):
tenant_id = os.getenv("AZURE_TENANT_ID")

async def sms_token_credential_auth_async(self):
if not self.connection_string or not self.phone_number:
raise ValueError(
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
set''')
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
Expand Down

0 comments on commit c7d1d0e

Please sign in to comment.