From cd17daae3d6a91360b4b3ca950d08bd698fe5003 Mon Sep 17 00:00:00 2001 From: Rajarshi Sarkar <73562869+sarkar-rajarshi@users.noreply.github.com> Date: Fri, 5 Mar 2021 19:24:26 -0800 Subject: [PATCH 1/5] make CommunicationUserIdentifierSerializer private --- ...er_serializer.py => _communication_identifier_serializer.py} | 0 .../azure-communication-chat/azure/communication/chat/_utils.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename sdk/communication/azure-communication-chat/azure/communication/chat/{communication_identifier_serializer.py => _communication_identifier_serializer.py} (100%) diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/communication_identifier_serializer.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_communication_identifier_serializer.py similarity index 100% rename from sdk/communication/azure-communication-chat/azure/communication/chat/communication_identifier_serializer.py rename to sdk/communication/azure-communication-chat/azure/communication/chat/_communication_identifier_serializer.py diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_utils.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_utils.py index 11fcd38bbd0e..4cf0e2dfd7a8 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_utils.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_utils.py @@ -3,7 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- -from .communication_identifier_serializer import CommunicationUserIdentifierSerializer +from ._communication_identifier_serializer import CommunicationUserIdentifierSerializer def _to_utc_datetime(value): return value.strftime('%Y-%m-%dT%H:%M:%SZ') From 20f7bec5bcfd151cc5f74df3c41bea12260a9ee1 Mon Sep 17 00:00:00 2001 From: Rajarshi Sarkar <73562869+sarkar-rajarshi@users.noreply.github.com> Date: Mon, 8 Mar 2021 14:03:20 -0800 Subject: [PATCH 2/5] CommunicationTokenCredential + AccessToken fix - Initialize CommunicationTokenCredential with token and not with CommunicationTokenRefreshOptions - Convert AccessToken.expires_on to int as compared to datetime --- .../azure/communication/chat/__init__.py | 5 ++++- .../communication/chat/_shared/user_credential.py | 4 +++- .../chat/_shared/user_credential_async.py | 4 +++- .../azure/communication/chat/aio/__init__.py | 5 +++-- .../test_communication_identifier_serializer.py | 2 +- .../tests/test_chat_client_e2e.py | 13 ++++--------- .../tests/test_chat_client_e2e_async.py | 8 +++----- .../tests/test_chat_thread_client_e2e.py | 11 ++++------- .../tests/test_chat_thread_client_e2e_async.py | 11 ++++------- .../identity/_shared/user_credential.py | 5 ++++- .../identity/_shared/user_credential_async.py | 5 ++++- .../azure/communication/identity/_shared/utils.py | 8 +++++++- 12 files changed, 44 insertions(+), 37 deletions(-) diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/__init__.py b/sdk/communication/azure-communication-chat/azure/communication/chat/__init__.py index 94266498df10..c1424426fc20 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/__init__.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/__init__.py @@ -17,6 +17,8 @@ CreateChatThreadResult ) +from ._shared.user_credential import CommunicationTokenCredential + __all__ = [ 'ChatClient', 'ChatThreadClient', @@ -29,6 +31,7 @@ 'ChatThreadParticipant', 'ChatMessageType', 'CreateChatThreadResult', - 'CommunicationError' + 'CommunicationError', + 'CommunicationTokenCredential' ] __version__ = VERSION diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py index e920d356a518..1eef9a4bc8b5 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py @@ -9,6 +9,7 @@ cast, Tuple, ) +from .user_token_refresh_options import CommunicationTokenRefreshOptions from msrest.serialization import TZ_UTC @@ -21,8 +22,9 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - communication_token_refresh_options + token # type: str ): + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py index 0918074eb04a..3c4736733436 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py @@ -9,6 +9,7 @@ cast, Tuple, ) +from .user_token_refresh_options import CommunicationTokenRefreshOptions from msrest.serialization import TZ_UTC @@ -22,8 +23,9 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - communication_token_refresh_options + token # type: str ): + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/aio/__init__.py b/sdk/communication/azure-communication-chat/azure/communication/chat/aio/__init__.py index 40356a5edc3a..ebda8b1a392f 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/aio/__init__.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/aio/__init__.py @@ -4,8 +4,9 @@ # ------------------------------------ from ._chat_client_async import ChatClient from ._chat_thread_client_async import ChatThreadClient - +from .._shared.user_credential_async import CommunicationTokenCredential __all__ = [ "ChatClient", - "ChatThreadClient" + "ChatThreadClient", + "CommunicationTokenCredential" ] diff --git a/sdk/communication/azure-communication-chat/tests/_shared/test_communication_identifier_serializer.py b/sdk/communication/azure-communication-chat/tests/_shared/test_communication_identifier_serializer.py index b6888d2f3826..837928939394 100644 --- a/sdk/communication/azure-communication-chat/tests/_shared/test_communication_identifier_serializer.py +++ b/sdk/communication/azure-communication-chat/tests/_shared/test_communication_identifier_serializer.py @@ -4,7 +4,7 @@ # license information. # ------------------------------------------------------------------------- import unittest -from azure.communication.chat.communication_identifier_serializer import CommunicationUserIdentifierSerializer +from azure.communication.chat._communication_identifier_serializer import CommunicationUserIdentifierSerializer from azure.communication.chat._generated.models import( CommunicationIdentifierModel, MicrosoftTeamsUserIdentifierModel, diff --git a/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e.py b/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e.py index 221afd927561..b2f4968d09a8 100644 --- a/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e.py +++ b/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e.py @@ -12,10 +12,9 @@ from uuid import uuid4 from azure.communication.identity import CommunicationIdentityClient -from azure.communication.identity._shared.user_credential import CommunicationTokenCredential -from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions from azure.communication.chat import ( ChatClient, + CommunicationTokenCredential, ChatThreadParticipant ) from azure.communication.chat._shared.utils import parse_connection_str @@ -50,8 +49,7 @@ def setUp(self): self.token = tokenresponse.token # create ChatClient - refresh_options = CommunicationTokenRefreshOptions(self.token) - self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token)) def tearDown(self): super(ChatClientTest, self).tearDown() @@ -82,12 +80,9 @@ def test_access_token_validation(self): This is to make sure that consecutive calls made using the same chat_client or chat_thread_client does not throw an exception due to mismatch in the generation of azure.core.credentials.AccessToken """ - from azure.communication.identity._shared.user_token_refresh_options import \ - CommunicationTokenRefreshOptions as IdentityCommunicationTokenRefreshOptions # create ChatClient - refresh_options = IdentityCommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token)) raised = False try: # create chat thread @@ -109,7 +104,7 @@ def test_access_token_validation(self): except: raised = True - assert raised is True + assert raised is False @pytest.mark.live_test_only def test_create_chat_thread(self): diff --git a/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e_async.py b/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e_async.py index 0b2fe07c045d..9e8155ef6f5a 100644 --- a/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e_async.py +++ b/sdk/communication/azure-communication-chat/tests/test_chat_client_e2e_async.py @@ -11,10 +11,9 @@ from uuid import uuid4 from azure.communication.identity import CommunicationIdentityClient -from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential -from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions from azure.communication.chat.aio import ( - ChatClient + ChatClient, + CommunicationTokenCredential ) from azure.communication.chat import ( ChatThreadParticipant @@ -48,8 +47,7 @@ def setUp(self): self.token = token_response.token # create ChatClient - refresh_options = CommunicationTokenRefreshOptions(self.token) - self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token)) def tearDown(self): super(ChatClientTestAsync, self).tearDown() diff --git a/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e.py b/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e.py index bf26081d5616..509a1bccda01 100644 --- a/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e.py +++ b/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e.py @@ -11,12 +11,11 @@ from msrest.serialization import TZ_UTC from azure.communication.identity import CommunicationIdentityClient -from azure.communication.identity._shared.user_credential import CommunicationTokenCredential -from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions from azure.communication.chat import ( ChatClient, ChatThreadParticipant, - ChatMessageType + ChatMessageType, + CommunicationTokenCredential ) from azure.communication.chat._shared.utils import parse_connection_str @@ -54,10 +53,8 @@ def setUp(self): self.token_new_user = tokenresponse.token # create ChatClient - refresh_options = CommunicationTokenRefreshOptions(self.token) - refresh_options_new_user = CommunicationTokenRefreshOptions(self.token_new_user) - self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) - self.chat_client_new_user = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options_new_user)) + self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token)) + self.chat_client_new_user = ChatClient(self.endpoint, CommunicationTokenCredential(self.token_new_user)) def tearDown(self): super(ChatThreadClientTest, self).tearDown() diff --git a/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e_async.py b/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e_async.py index de3228ef62ff..9ba196e397c5 100644 --- a/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e_async.py +++ b/sdk/communication/azure-communication-chat/tests/test_chat_thread_client_e2e_async.py @@ -10,10 +10,9 @@ from msrest.serialization import TZ_UTC from azure.communication.identity import CommunicationIdentityClient -from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential -from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions from azure.communication.chat.aio import ( - ChatClient + ChatClient, + CommunicationTokenCredential ) from azure.communication.chat import ( ChatThreadParticipant, @@ -57,10 +56,8 @@ def setUp(self): self.token_new_user = token_response.token # create ChatClient - refresh_option = CommunicationTokenRefreshOptions(self.token) - refresh_option_new_user = CommunicationTokenRefreshOptions(self.token_new_user) - self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_option)) - self.chat_client_new_user = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_option_new_user)) + self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token)) + self.chat_client_new_user = ChatClient(self.endpoint, CommunicationTokenCredential(self.token_new_user)) def tearDown(self): super(ChatThreadClientTestAsync, self).tearDown() diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py index e920d356a518..a44694198675 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py @@ -12,6 +12,8 @@ from msrest.serialization import TZ_UTC +from .user_token_refresh_options import CommunicationTokenRefreshOptions + class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service @@ -21,8 +23,9 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - communication_token_refresh_options + token # type: str ): + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py index 0918074eb04a..2cc1fb1008b6 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py @@ -12,6 +12,8 @@ from msrest.serialization import TZ_UTC +from .user_token_refresh_options import CommunicationTokenRefreshOptions + class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service @@ -22,8 +24,9 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - communication_token_refresh_options + token # type: str ): + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py index 18da505150ba..88ac8969aa96 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py @@ -65,10 +65,16 @@ def create_access_token(token): try: padded_base64_payload = base64.b64decode(parts[1] + "==").decode('ascii') payload = json.loads(padded_base64_payload) - return AccessToken(token, datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC)) + return AccessToken(token, + _convert_expires_on_datetime_to_utc_int(datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC))) except ValueError: raise ValueError(token_parse_err_msg) +def _convert_expires_on_datetime_to_utc_int(expires_on): + epoch = time.mktime(datetime(1970, 1, 1).timetuple()) + return epoch-time.mktime(expires_on.timetuple()) + + def get_authentication_policy( endpoint, # type: str credential, # type: TokenCredential or str From 82b1e6c3424de60024ca9e8f89be785f8a5a2621 Mon Sep 17 00:00:00 2001 From: Rajarshi Sarkar <73562869+sarkar-rajarshi@users.noreply.github.com> Date: Mon, 8 Mar 2021 17:19:08 -0800 Subject: [PATCH 3/5] add token_refresher to CommunicationTokenCredential --- .../communication/chat/_shared/user_credential.py | 11 +++++++---- .../chat/_shared/user_credential_async.py | 11 +++++++---- .../communication/identity/_shared/user_credential.py | 6 ++++-- .../identity/_shared/user_credential_async.py | 8 +++++--- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py index 1eef9a4bc8b5..9f0b6881bc2d 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py @@ -9,10 +9,11 @@ cast, Tuple, ) -from .user_token_refresh_options import CommunicationTokenRefreshOptions from msrest.serialization import TZ_UTC +from .user_token_refresh_options import CommunicationTokenRefreshOptions + class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service @@ -22,9 +23,11 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - token # type: str - ): - communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) + token, # type: str + token_refresher=None + ): + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token, + token_refresher=token_refresher) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py index 3c4736733436..6d93ee81d917 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py @@ -9,10 +9,11 @@ cast, Tuple, ) -from .user_token_refresh_options import CommunicationTokenRefreshOptions from msrest.serialization import TZ_UTC +from .user_token_refresh_options import CommunicationTokenRefreshOptions + class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service @@ -23,9 +24,11 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - token # type: str - ): - communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) + token, # type: str + token_refresher=None + ): + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token, + token_refresher=token_refresher) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py index a44694198675..188452d68609 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py @@ -23,9 +23,11 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - token # type: str + token, # type: str + token_refresher=None ): - communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token, + token_refresher=token_refresher) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py index 2cc1fb1008b6..6d93ee81d917 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py @@ -24,9 +24,11 @@ class CommunicationTokenCredential(object): ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 def __init__(self, - token # type: str - ): - communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token) + token, # type: str + token_refresher=None + ): + communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token, + token_refresher=token_refresher) self._token = communication_token_refresh_options.get_token() self._token_refresher = communication_token_refresh_options.get_token_refresher() self._lock = Condition(Lock()) From cf53c8deaa8fd03362e304a31c0510d12a6b1928 Mon Sep 17 00:00:00 2001 From: Rajarshi Sarkar <73562869+sarkar-rajarshi@users.noreply.github.com> Date: Tue, 9 Mar 2021 11:39:17 -0800 Subject: [PATCH 4/5] update docstring --- .../azure/communication/chat/_shared/user_credential.py | 5 +++-- .../communication/chat/_shared/user_credential_async.py | 4 ++-- .../azure/communication/identity/_shared/user_credential.py | 5 +++-- .../communication/identity/_shared/user_credential_async.py | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py index 9f0b6881bc2d..89987833663d 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py @@ -16,8 +16,9 @@ class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. - :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service - :type communication_token_refresh_options: ~azure.communication.chat.CommunicationTokenRefreshOptions + :param str token: The token used to authenticate to an Azure Communication service + :param token_refresher: The token refresher to provide capacity to fetch fresh token + :raises: TypeError """ ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py index 6d93ee81d917..c81ea2b3211b 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py @@ -16,8 +16,8 @@ class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. - :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service - :type communication_token_refresh_options: ~azure.communication.chat.aio.CommunicationTokenRefreshOptions + :param str token: The token used to authenticate to an Azure Communication service + :param token_refresher: The token refresher to provide capacity to fetch fresh token :raises: TypeError """ diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py index 188452d68609..77e854318318 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential.py @@ -16,8 +16,9 @@ class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. - :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service - :type communication_token_refresh_options: ~azure.communication.chat.CommunicationTokenRefreshOptions + :param str token: The token used to authenticate to an Azure Communication service + :param token_refresher: The token refresher to provide capacity to fetch fresh token + :raises: TypeError """ ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2 diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py index 6d93ee81d917..c81ea2b3211b 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/user_credential_async.py @@ -16,8 +16,8 @@ class CommunicationTokenCredential(object): """Credential type used for authenticating to an Azure Communication service. - :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service - :type communication_token_refresh_options: ~azure.communication.chat.aio.CommunicationTokenRefreshOptions + :param str token: The token used to authenticate to an Azure Communication service + :param token_refresher: The token refresher to provide capacity to fetch fresh token :raises: TypeError """ From 06f463637059441d0ee4e04d2a13e3d484ec1b8a Mon Sep 17 00:00:00 2001 From: Rajarshi Sarkar <73562869+sarkar-rajarshi@users.noreply.github.com> Date: Mon, 8 Mar 2021 18:03:47 -0800 Subject: [PATCH 5/5] Identifier fields renaming _ credential import fixes in sample code --- .../azure-communication-chat/README.md | 7 +- .../azure/communication/chat/_models.py | 12 +-- .../samples/chat_client_sample.py | 70 ++++++++------- .../samples/chat_client_sample_async.py | 90 ++++++++++--------- .../samples/chat_thread_client_sample.py | 16 ++-- .../chat_thread_client_sample_async.py | 15 ++-- 6 files changed, 105 insertions(+), 105 deletions(-) diff --git a/sdk/communication/azure-communication-chat/README.md b/sdk/communication/azure-communication-chat/README.md index 4c9d7c9791e5..494450d069e0 100644 --- a/sdk/communication/azure-communication-chat/README.md +++ b/sdk/communication/azure-communication-chat/README.md @@ -44,14 +44,11 @@ it with this token. It is because the initiator of the create request must be in This will allow you to create, get, list or delete chat threads. ```python -from azure.communication.chat import ChatClient -from azure.communication.identity._shared.user_credential import CommunicationTokenCredential -from azure.communication.identity._shared.user_token_refresh_options import CommunicationTokenRefreshOptions +from azure.communication.chat import ChatClient, CommunicationTokenCredential # Your unique Azure Communication service endpoint endpoint = "https://.communcationservices.azure.com" -refresh_options = CommunicationTokenRefreshOptions(token) -chat_client = ChatClient(endpoint, CommunicationTokenCredential(refresh_options)) +chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) ``` ## Create Chat Thread Client diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_models.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_models.py index 9ec7d7d5a17c..88e975e3e765 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_models.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_models.py @@ -74,8 +74,8 @@ class ChatMessage(object): :ivar created_on: The timestamp when the chat message arrived at the server. The timestamp is in RFC3339 format: ``yyyy-MM-ddTHH:mm:ssZ``. :type created_on: ~datetime.datetime - :ivar sender_communication_identifier: The chat message sender. - :type sender_communication_identifier: CommunicationUserIdentifier + :ivar sender: The chat message sender. + :type sender: CommunicationUserIdentifier :ivar deleted_on: The timestamp when the chat message was deleted. The timestamp is in RFC3339 format: ``yyyy-MM-ddTHH:mm:ssZ``. :type deleted_on: ~datetime.datetime @@ -97,7 +97,7 @@ def __init__( self.content = kwargs['content'] self.sender_display_name = kwargs['sender_display_name'] self.created_on = kwargs['created_on'] - self.sender_communication_identifier = kwargs['sender_communication_identifier'] + self.sender = kwargs['sender'] self.deleted_on = kwargs['deleted_on'] self.edited_on = kwargs['edited_on'] @@ -125,7 +125,7 @@ def _from_generated(cls, chat_message): content=ChatMessageContent._from_generated(chat_message.content), # pylint:disable=protected-access sender_display_name=chat_message.sender_display_name, created_on=chat_message.created_on, - sender_communication_identifier=sender_communication_identifier, + sender=sender_communication_identifier, deleted_on=chat_message.deleted_on, edited_on=chat_message.edited_on ) @@ -141,9 +141,9 @@ class ChatMessageContent(object): :ivar participants: Chat message content for messages of types participantAdded or participantRemoved. :type participants: list[~azure.communication.chat.models.ChatParticipant] - :ivar initiator_communication_identifier: Chat message content for messages of types participantAdded or + :ivar initiator: Chat message content for messages of types participantAdded or participantRemoved. - :type initiator_communication_identifier: CommunicationUserIdentifier + :type initiator: Union[CommunicationUserIdentifier, MicrosoftTeamsUserIdentifier] """ def __init__( diff --git a/sdk/communication/azure-communication-chat/samples/chat_client_sample.py b/sdk/communication/azure-communication-chat/samples/chat_client_sample.py index 63b55c3ebd97..2df54685801f 100644 --- a/sdk/communication/azure-communication-chat/samples/chat_client_sample.py +++ b/sdk/communication/azure-communication-chat/samples/chat_client_sample.py @@ -44,33 +44,34 @@ class ChatClientSamples(object): _thread_id = None def create_chat_client(self): + token = self.token + endpoint = self.endpoint # [START create_chat_client] - from azure.communication.chat import ChatClient - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + from azure.communication.chat import ChatClient, CommunicationTokenCredential + + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) # [END create_chat_client] def create_thread(self): + token = self.token + endpoint = self.endpoint + user = self.user # [START create_thread] from datetime import datetime from azure.communication.identity import CommunicationUserIdentifier - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions from azure.communication.chat import( ChatClient, - ChatThreadParticipant + ChatThreadParticipant, + CommunicationTokenCredential ) - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) topic = "test topic" participants = [ChatThreadParticipant( - user=self.user, + user=user, display_name='name', share_history_time=datetime.utcnow() )] @@ -91,41 +92,42 @@ def create_thread(self): print("thread created, id: " + self._thread_id) def get_chat_thread_client(self): + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id + # [START get_chat_thread_client] - from azure.communication.chat import ChatClient - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + from azure.communication.chat import ChatClient, CommunicationTokenCredential - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) - chat_thread_client = chat_client.get_chat_thread_client(self._thread_id) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) + chat_thread_client = chat_client.get_chat_thread_client(thread_id) # [END get_chat_thread_client] - print("chat_thread_client created with thread id: ", chat_thread_client.thread_id) + print("get_chat_thread_client succeeded with thread id: ", chat_thread_client.thread_id) def get_thread(self): + token = self.token + endpoint = self.endpoint + # [START get_thread] - from azure.communication.chat import ChatClient - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + from azure.communication.chat import ChatClient, CommunicationTokenCredential - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) chat_thread = chat_client.get_chat_thread(self._thread_id) # [END get_thread] print("get_thread succeeded, thread id: " + chat_thread.id + ", thread topic: " + chat_thread.topic) def list_threads(self): + token = self.token + endpoint = self.endpoint + # [START list_threads] - from azure.communication.chat import ChatClient - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + from azure.communication.chat import ChatClient, CommunicationTokenCredential from datetime import datetime, timedelta import pytz - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) start_time = datetime.utcnow() - timedelta(days=2) start_time = start_time.replace(tzinfo=pytz.utc) chat_thread_infos = chat_client.list_chat_threads(results_per_page=5, start_time=start_time) @@ -137,14 +139,14 @@ def list_threads(self): # [END list_threads] def delete_thread(self): + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id # [START delete_thread] - from azure.communication.chat import ChatClient - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + from azure.communication.chat import ChatClient, CommunicationTokenCredential - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) - chat_client.delete_chat_thread(self._thread_id) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) + chat_client.delete_chat_thread(thread_id) # [END delete_thread] print("delete_thread succeeded") diff --git a/sdk/communication/azure-communication-chat/samples/chat_client_sample_async.py b/sdk/communication/azure-communication-chat/samples/chat_client_sample_async.py index 094347c5a262..2ae74279db83 100644 --- a/sdk/communication/azure-communication-chat/samples/chat_client_sample_async.py +++ b/sdk/communication/azure-communication-chat/samples/chat_client_sample_async.py @@ -44,27 +44,30 @@ class ChatClientSamplesAsync(object): _thread_id = None def create_chat_client(self): + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id + # [START create_chat_client] - from azure.communication.chat.aio import ChatClient - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) # [END create_chat_client] print("chat_client created") async def create_thread_async(self): + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id + + # [START create_thread] from datetime import datetime - from azure.communication.chat.aio import ChatClient - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential from azure.communication.chat import ChatThreadParticipant - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: - # [START create_thread] + topic = "test topic" participants = [ChatThreadParticipant( user=self.user, @@ -80,46 +83,51 @@ async def create_thread_async(self): topic, thread_participants=participants, repeatability_request_id=repeatability_request_id) - # [END create_thread] + # [END create_thread] self._thread_id = create_chat_thread_result.chat_thread.id print("thread created, id: " + self._thread_id) def get_chat_thread_client(self): + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id + # [START get_chat_thread_client] - from azure.communication.chat.aio import ChatClient - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) - chat_thread_client = chat_client.get_chat_thread_client(self._thread_id) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) + chat_thread_client = chat_client.get_chat_thread_client(thread_id) # [END get_chat_thread_client] print("chat_thread_client created with thread id: ", chat_thread_client.thread_id) async def get_thread_async(self): - from azure.communication.chat.aio import ChatClient - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id + + # [START get_thread] + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: - # [START get_thread] - chat_thread = await chat_client.get_chat_thread(self._thread_id) - # [END get_thread] + chat_thread = await chat_client.get_chat_thread(thread_id) + print("Chat thread id: " + chat_thread.id + ", thread topic: " + chat_thread.topic) + # [END get_thread] print("get_thread succeeded, thread id: " + chat_thread.id + ", thread topic: " + chat_thread.topic) async def list_threads_async(self): - from azure.communication.chat.aio import ChatClient - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + # [START list_threads] + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential + + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: - # [START list_threads] + from datetime import datetime, timedelta import pytz start_time = datetime.utcnow() - timedelta(days=2) @@ -129,19 +137,21 @@ async def list_threads_async(self): async for chat_thread_info_page in chat_thread_infos.by_page(): async for chat_thread_info in chat_thread_info_page: print("thread id: ", chat_thread_info.id) - # [END list_threads] + # [END list_threads] async def delete_thread_async(self): - from azure.communication.chat.aio import ChatClient - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions + token = self.token + endpoint = self.endpoint + thread_id = self._thread_id - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + # [START delete_thread] + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential + + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: - # [START delete_thread] + await chat_client.delete_chat_thread(self._thread_id) - # [END delete_thread] + # [END delete_thread] print("delete_thread succeeded") def clean_up(self): diff --git a/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample.py b/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample.py index c79b9fa0cd56..8db2890cb06d 100644 --- a/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample.py +++ b/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample.py @@ -28,10 +28,9 @@ class ChatThreadClientSamples(object): from azure.communication.identity import CommunicationIdentityClient - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions from azure.communication.chat import ( - ChatClient + ChatClient, + CommunicationTokenCredential ) connection_string = os.environ.get("AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING", None) if not connection_string: @@ -50,8 +49,7 @@ class ChatThreadClientSamples(object): _message_id = None new_user = identity_client.create_user() - refresh_options = CommunicationTokenRefreshOptions(token) - _chat_client = ChatClient(endpoint, CommunicationTokenCredential(refresh_options)) + _chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) def create_chat_thread_client(self): token = self.token @@ -60,18 +58,16 @@ def create_chat_thread_client(self): # [START create_chat_thread_client] from datetime import datetime from azure.communication.identity import CommunicationUserIdentifier - from azure.communication.identity._shared.user_credential import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions from azure.communication.chat import ( ChatClient, - ChatThreadParticipant + ChatThreadParticipant, + CommunicationTokenCredential ) # retrieve `token` using CommunicationIdentityClient.get_token method # set `endpoint` to ACS service endpoint # create `user` using CommunicationIdentityClient.create_user method for new users; # else for existing users set `user` = CommunicationUserIdentifier(some_user_id) - refresh_options = CommunicationTokenRefreshOptions(token) - chat_client = ChatClient(endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) topic = "test topic" participants = [ChatThreadParticipant( user=user, diff --git a/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample_async.py b/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample_async.py index c006b7bc805b..6fcc5e9702f4 100644 --- a/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample_async.py +++ b/sdk/communication/azure-communication-chat/samples/chat_thread_client_sample_async.py @@ -28,10 +28,8 @@ class ChatThreadClientSamplesAsync(object): - from azure.communication.chat.aio import ChatClient + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential from azure.communication.identity import CommunicationIdentityClient - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions connection_string = os.environ.get("AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING", None) if not connection_string: @@ -50,20 +48,17 @@ class ChatThreadClientSamplesAsync(object): _message_id = None new_user = identity_client.create_user() - refresh_options = CommunicationTokenRefreshOptions(token) - _chat_client = ChatClient(endpoint, CommunicationTokenCredential(refresh_options)) + _chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async def create_chat_thread_client_async(self): + token = self.token # [START create_chat_thread_client] from datetime import datetime - from azure.communication.chat.aio import ChatClient + from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential from azure.communication.chat import ChatThreadParticipant from azure.communication.identity import CommunicationUserIdentifier - from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential - from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions - refresh_options = CommunicationTokenRefreshOptions(self.token) - chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) + chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(token)) async with chat_client: topic = "test topic"