Skip to content

Commit c7d1d0e

Browse files
dinazavyrDina Saparbaeva
andauthored
[communication]-[sms]-Fixes mypy type errors. (#37805)
* Fixes mypy type errors. * Fixed mypy errors * Fixed pylint annotation errors --------- Co-authored-by: Dina Saparbaeva <dsaparbaeva@microsoft.com>
1 parent cff747c commit c7d1d0e

File tree

8 files changed

+70
-43
lines changed

8 files changed

+70
-43
lines changed

sdk/communication/azure-communication-sms/azure/communication/sms/_sms_client.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class SmsClient(object): # pylint: disable=client-accepts-api-version-keyword
3333
"""
3434

3535
def __init__(
36-
self,
37-
endpoint, # type: str
38-
credential, # type: Union[TokenCredential, AzureKeyCredential]
39-
**kwargs # type: Any
36+
self,
37+
endpoint, # type: str
38+
credential, # type: Union[TokenCredential, AzureKeyCredential]
39+
**kwargs # type: Any
4040
):
4141
# type: (...) -> None
4242
try:
@@ -56,9 +56,9 @@ def __init__(
5656

5757
@classmethod
5858
def from_connection_string(
59-
cls,
60-
conn_str, # type: str
61-
**kwargs # type: Any
59+
cls,
60+
conn_str, # type: str
61+
**kwargs # type: Any
6262
): # type: (...) -> SmsClient
6363
"""Create SmsClient from a Connection String.
6464
@@ -78,33 +78,32 @@ def from_connection_string(
7878
"""
7979
endpoint, access_key = parse_connection_str(conn_str)
8080

81-
return cls(endpoint, access_key, **kwargs)
81+
return cls(endpoint, AzureKeyCredential(access_key), **kwargs)
8282

8383
@distributed_trace
8484
def send(
85-
self,
86-
from_, # type: str
87-
to, # type: Union[str, List[str]]
88-
message, # type: str
89-
*,
90-
enable_delivery_report: bool = False,
91-
tag: Optional[str] = None,
92-
**kwargs: Any
93-
): # type: (...) -> [SmsSendResult]
85+
self,
86+
from_: str,
87+
to: Union[str, List[str]],
88+
message: str,
89+
*,
90+
enable_delivery_report: bool = False,
91+
tag: Optional[str] = None,
92+
**kwargs: Any,
93+
) -> List[SmsSendResult]:
9494
"""Sends SMSs to phone numbers.
9595
9696
:param str from_: The sender of the SMS.
9797
:param to: The single recipient or the list of recipients of the SMS.
9898
:type to: Union[str, List[str]]
99-
:param str message: The message in the SMS.
99+
:param str message: The message in the SMS
100100
:keyword bool enable_delivery_report: Enable this flag to receive a delivery report for this
101101
message on the Azure Resource EventGrid.
102102
:keyword str tag: Use this field to provide metadata that will then be sent back in the corresponding
103103
Delivery Report.
104104
:return: A list of SmsSendResult.
105105
:rtype: [~azure.communication.sms.models.SmsSendResult]
106106
"""
107-
108107
if isinstance(to, str):
109108
to = [to]
110109

@@ -123,17 +122,17 @@ def send(
123122
**kwargs
124123
)
125124

126-
return self._sms_service_client.sms.send(
125+
response = self._sms_service_client.sms.send(
127126
request,
128-
cls=lambda pr, r, e: [
129-
SmsSendResult(
130-
to=item.to,
131-
message_id=item.message_id,
132-
http_status_code=item.http_status_code,
133-
successful=item.successful,
134-
error_message=item.error_message,
135-
)
136-
for item in r.value
137-
],
138127
**kwargs
139128
)
129+
130+
return [
131+
SmsSendResult(
132+
to=item.to,
133+
message_id=item.message_id,
134+
http_status_code=item.http_status_code,
135+
successful=item.successful,
136+
error_message=item.error_message
137+
) for item in response.value
138+
]

sdk/communication/azure-communication-sms/azure/communication/sms/aio/_sms_client_async.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def from_connection_string(cls, conn_str: str, **kwargs: Any) -> "SmsClient":
7474
:caption: Creating the SmsClient from a connection string.
7575
"""
7676
endpoint, access_key = parse_connection_str(conn_str)
77-
return cls(endpoint, access_key, **kwargs)
77+
return cls(endpoint, AzureKeyCredential(access_key), **kwargs)
7878

7979
@distributed_trace_async
8080
async def send(
@@ -118,21 +118,21 @@ async def send(
118118
**kwargs,
119119
)
120120

121-
return await self._sms_service_client.sms.send(
121+
response = await self._sms_service_client.sms.send(
122122
request,
123-
cls=lambda pr, r, e: [
124-
SmsSendResult(
125-
to=item.to,
126-
message_id=item.message_id,
127-
http_status_code=item.http_status_code,
128-
successful=item.successful,
129-
error_message=item.error_message,
130-
)
131-
for item in r.value
132-
],
133-
**kwargs,
123+
**kwargs
134124
)
135125

126+
return [
127+
SmsSendResult(
128+
to=item.to,
129+
message_id=item.message_id,
130+
http_status_code=item.http_status_code,
131+
successful=item.successful,
132+
error_message=item.error_message
133+
) for item in response.value
134+
]
135+
136136
async def __aenter__(self) -> "SmsClient":
137137
await self._sms_service_client.__aenter__()
138138
return self

sdk/communication/azure-communication-sms/samples/send_sms_to_multiple_recipients_sample.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class SmsMultipleRecipientsSample(object):
3131
phone_number = os.getenv("SMS_PHONE_NUMBER")
3232

3333
def send_sms_to_multiple_recipients(self):
34+
if not self.connection_string or not self.phone_number:
35+
raise ValueError(
36+
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
37+
set''')
38+
3439
sms_client = SmsClient.from_connection_string(self.connection_string)
3540

3641
# calling send() with sms values

sdk/communication/azure-communication-sms/samples/send_sms_to_multiple_recipients_sample_async.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class SmsMultipleRecipientsSampleAsync(object):
3232
phone_number = os.getenv("SMS_PHONE_NUMBER")
3333

3434
async def send_sms_to_multiple_recipients_async(self):
35+
if not self.connection_string or not self.phone_number:
36+
raise ValueError(
37+
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
38+
set''')
39+
3540
sms_client = SmsClient.from_connection_string(self.connection_string)
3641

3742
async with sms_client:

sdk/communication/azure-communication-sms/samples/send_sms_to_single_recipient_sample.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class SmsSingleRecipientSample(object):
3131
phone_number = os.getenv("SMS_PHONE_NUMBER")
3232

3333
def send_sms_to_single_recipient(self):
34+
if not self.connection_string or not self.phone_number:
35+
raise ValueError(
36+
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
37+
set''')
38+
3439
# [START auth_from_connection_string]
3540
sms_client = SmsClient.from_connection_string(self.connection_string)
3641
# [END auth_from_connection_string]

sdk/communication/azure-communication-sms/samples/send_sms_to_single_recipient_sample_async.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class SmsSingleRecipientSampleAsync(object):
3232
phone_number = os.getenv("SMS_PHONE_NUMBER")
3333

3434
async def send_sms_to_single_recipient_async(self):
35+
if not self.connection_string or not self.phone_number:
36+
raise ValueError(
37+
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
38+
set''')
3539
# [START auth_from_connection_string_async]
3640
sms_client = SmsClient.from_connection_string(self.connection_string)
3741
# [END auth_from_connection_string_async]

sdk/communication/azure-communication-sms/samples/sms_token_credential_auth_sample.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class SmsTokenCredentialAuthSample(object):
3636
tenant_id = os.getenv("AZURE_TENANT_ID")
3737

3838
def sms_token_credential_auth(self):
39+
if not self.connection_string or not self.phone_number:
40+
raise ValueError(
41+
'''Environment variables COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING and SMS_PHONE_NUMBER must be
42+
set''')
43+
3944
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
4045
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
4146
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:

sdk/communication/azure-communication-sms/samples/sms_token_credential_auth_sample_async.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class SmsTokenCredentialAuthSampleAsync(object):
3737
tenant_id = os.getenv("AZURE_TENANT_ID")
3838

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

0 commit comments

Comments
 (0)