diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/models.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/models.py index 4aff60e0c4df..30fa9d3327ac 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/models.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/models.py @@ -108,13 +108,13 @@ class UnknownIdentifier(object): Represents an identifier of an unknown type. It will be encountered in communications with endpoints that are not identifiable by this version of the SDK. - :ivar identifier: Unknown communication identifier. - :vartype identifier: str + :ivar raw_id: Unknown communication identifier. + :vartype raw_id: str :param identifier: Value to initialize UnknownIdentifier. :type identifier: str """ def __init__(self, identifier): - self.identifier = identifier + self.raw_id = identifier class _CaseInsensitiveEnumMeta(EnumMeta): def __getitem__(self, name): @@ -148,7 +148,7 @@ class MicrosoftTeamsUserIdentifier(object): :vartype user_id: str :param user_id: Value to initialize MicrosoftTeamsUserIdentifier. :type user_id: str - :ivar rawId: Raw id of the Microsoft Teams user. + :ivar raw_id: Raw id of the Microsoft Teams user. :vartype raw_id: str :ivar cloud: Cloud environment that this identifier belongs to :vartype cloud: CommunicationCloudEnvironment 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 index 972716b833be..13e540c7a67c 100644 --- 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 @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- +from enum import Enum from ._generated.models import ( CommunicationIdentifierModel, @@ -17,12 +18,17 @@ UnknownIdentifier, ) +class _IdentifierType(Enum): + COMMUNICATION_USER_IDENTIFIER = "COMMUNICATION_USER_IDENTIFIER" + PHONE_NUMBER_IDENTIFIER = "PHONE_NUMBER_IDENTIFIER" + UNKNOWN_IDENTIFIER = "UNKNOWN_IDENTIFIER" + MICROSOFT_TEAMS_IDENTIFIER = "MICROSOFT_TEAMS_IDENTIFIER" + class CommunicationUserIdentifierSerializer(object): @classmethod def serialize(cls, communicationIdentifier): """ Serialize the Communication identifier into CommunicationIdentifierModel - :param identifier: Identifier object :type identifier: Union[CommunicationUserIdentifier, PhoneNumberIdentifier, MicrosoftTeamsUserIdentifier, UnknownIdentifier] @@ -30,16 +36,18 @@ def serialize(cls, communicationIdentifier): :rtype: ~azure.communication.chat.CommunicationIdentifierModel :raises Union[TypeError, ValueError] """ - if isinstance(communicationIdentifier, CommunicationUserIdentifier): + identifierType = CommunicationUserIdentifierSerializer._getIdentifierType(communicationIdentifier) + + if identifierType == _IdentifierType.COMMUNICATION_USER_IDENTIFIER: return CommunicationIdentifierModel( communication_user=CommunicationUserIdentifierModel(id=communicationIdentifier.identifier) ) - if isinstance(communicationIdentifier, PhoneNumberIdentifier): + if identifierType == _IdentifierType.PHONE_NUMBER_IDENTIFIER: return CommunicationIdentifierModel( raw_id=communicationIdentifier.raw_id, phone_number=PhoneNumberIdentifierModel(value=communicationIdentifier.phone_number) ) - if isinstance(communicationIdentifier, MicrosoftTeamsUserIdentifier): + if identifierType == _IdentifierType.MICROSOFT_TEAMS_IDENTIFIER: return CommunicationIdentifierModel( raw_id=communicationIdentifier.raw_id, microsoft_teams_user=MicrosoftTeamsUserIdentifierModel(user_id=communicationIdentifier.user_id, @@ -47,9 +55,9 @@ def serialize(cls, communicationIdentifier): cloud=communicationIdentifier.cloud) ) - if isinstance(communicationIdentifier, UnknownIdentifier): + if identifierType == _IdentifierType.UNKNOWN_IDENTIFIER: return CommunicationIdentifierModel( - raw_id=communicationIdentifier.identifier + raw_id=communicationIdentifier.raw_id ) raise TypeError("Unsupported identifier type " + communicationIdentifier.__class__.__name__) @@ -71,7 +79,6 @@ def assertMaximumOneNestedModel(cls, identifierModel): def deserialize(cls, identifierModel): """ Deserialize the CommunicationIdentifierModel into Communication Identifier - :param identifierModel: CommunicationIdentifierModel :type identifierModel: CommunicationIdentifierModel :return: Union[CommunicationUserIdentifier, CommunicationPhoneNumberIdentifier] @@ -106,3 +113,20 @@ def deserialize(cls, identifierModel): ) return UnknownIdentifier(raw_id) + + @classmethod + def _getIdentifierType(cls, communicationIdentifier): + def has_attributes(obj, attributes): + return all([hasattr(obj, attr) for attr in attributes]) + + if has_attributes(communicationIdentifier, ["identifier"]): + return _IdentifierType.COMMUNICATION_USER_IDENTIFIER + + if has_attributes(communicationIdentifier, ['phone_number', 'raw_id']): + return _IdentifierType.PHONE_NUMBER_IDENTIFIER + + if has_attributes(communicationIdentifier, ["raw_id", "user_id", "is_anonymous", "cloud"]): + return _IdentifierType.MICROSOFT_TEAMS_IDENTIFIER + + if has_attributes(communicationIdentifier, ["raw_id"]): + return _IdentifierType.UNKNOWN_IDENTIFIER 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 64bf4e403851..b6888d2f3826 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 @@ -94,7 +94,7 @@ def test_deserialize_unknown_identifier(self): unknown_identifier_expected = UnknownIdentifier("an id") assert isinstance(unknown_identifier_actual, UnknownIdentifier) - assert unknown_identifier_actual.identifier == unknown_identifier_expected.identifier + assert unknown_identifier_actual.raw_id == unknown_identifier_expected.raw_id def test_serialize_phone_number(self): phone_number_identifier_model = CommunicationUserIdentifierSerializer.serialize( diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/models.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/models.py index 83bab3945635..67e0a1ff6e2b 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/models.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/models.py @@ -37,13 +37,13 @@ class UnknownIdentifier(object): Represents an identifier of an unknown type. It will be encountered in communications with endpoints that are not identifiable by this version of the SDK. - :ivar identifier: Unknown communication identifier. - :vartype identifier: str + :ivar raw_id: Unknown communication identifier. + :vartype raw_id: str :param identifier: Value to initialize UnknownIdentifier. :type identifier: str """ def __init__(self, identifier): - self.identifier = identifier + self.raw_id = identifier class _CaseInsensitiveEnumMeta(EnumMeta): def __getitem__(cls, name): diff --git a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/models.py b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/models.py index 83bab3945635..67e0a1ff6e2b 100644 --- a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/models.py +++ b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/models.py @@ -37,13 +37,13 @@ class UnknownIdentifier(object): Represents an identifier of an unknown type. It will be encountered in communications with endpoints that are not identifiable by this version of the SDK. - :ivar identifier: Unknown communication identifier. - :vartype identifier: str + :ivar raw_id: Unknown communication identifier. + :vartype raw_id: str :param identifier: Value to initialize UnknownIdentifier. :type identifier: str """ def __init__(self, identifier): - self.identifier = identifier + self.raw_id = identifier class _CaseInsensitiveEnumMeta(EnumMeta): def __getitem__(cls, name): diff --git a/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/models.py b/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/models.py index 2e64b18dc068..adb6f909dd23 100644 --- a/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/models.py +++ b/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/models.py @@ -37,13 +37,13 @@ class UnknownIdentifier(object): Represents an identifier of an unknown type. It will be encountered in communications with endpoints that are not identifiable by this version of the SDK. - :ivar identifier: Unknown communication identifier. - :vartype identifier: str + :ivar raw_id: Unknown communication identifier. + :vartype raw_id: str :param identifier: Value to initialize UnknownIdentifier. :type identifier: str """ def __init__(self, identifier): - self.identifier = identifier + self.raw_id = identifier class CommunicationIdentifierModel(msrest.serialization.Model): """Communication Identifier Model.