Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check schema of the object to determine the type [preview4] #16838

Merged
merged 8 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,39 +18,46 @@
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]
:return: CommunicationIdentifierModel
: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,
is_anonymous=communicationIdentifier.is_anonymous,
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__)
Expand All @@ -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]
Expand Down Expand Up @@ -106,3 +113,21 @@ 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

Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down