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

Communication Identity Model serializer/deserializer #16268

Merged
merged 21 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -22,8 +22,8 @@ class PhoneNumberIdentifier(object):
:param value: Value to initialize PhoneNumberIdentifier.
:type value: str
"""
def __init__(self, value):
self.value = value
def __init__(self, phone_number):
self.phone_number = phone_number

class UnknownIdentifier(object):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

from .models import (
CommunicationIdentifierKind,
CommunicationIdentifierModel,
CommunicationUserIdentifier,
PhoneNumberIdentifier,
MicrosoftTeamsUserIdentifier,
UnknownIdentifier
)

class CommunicationUserIdentifierSerializer(object):

@classmethod
def serialize(cls, communicationIdentifier):
""" Serialize the Communication identifier into CommunicationIdentifierModel

:param identifier: Communication service identifier
:type identifier: Union[CommunicationUserIdentifier, CommunicationPhoneNumberIdentifier]
:return: CommunicationIdentifierModel
:rtype: ~azure.communication.chat.CommunicationIdentifierModel
"""
if isinstance(communicationIdentifier, CommunicationUserIdentifier):
return CommunicationIdentifierModel(
kind=CommunicationIdentifierKind.CommunicationUser,
id=communicationIdentifier.identifier
)
if isinstance(communicationIdentifier, PhoneNumberIdentifier):
return CommunicationIdentifierModel(
kind=CommunicationIdentifierKind.PhoneNumber,
id=communicationIdentifier.phone_number
)
if isinstance(communicationIdentifier, MicrosoftTeamsUserIdentifier):
return CommunicationIdentifierModel(
kind=CommunicationIdentifierKind.MicrosoftTeamsUser,
id=communicationIdentifier.user_id
)

return CommunicationIdentifierModel(
kind=CommunicationIdentifierKind.Unknown,
id=communicationIdentifier.identifier
)

@classmethod
def deserialize(cls, identifierModel):
"""
Deserialize the CommunicationIdentifierModel into Communication Identifier

:param identifierModel: CommunicationIdentifierModel
:type identifierModel: CommunicationIdentifierModel
:return: Union[CommunicationUserIdentifier, CommunicationPhoneNumberIdentifier]
:rtype: Union[CommunicationUserIdentifier, CommunicationPhoneNumberIdentifier]
:rasies: ValueError
"""

identifier, kind = identifierModel.id, identifierModel.kind

if kind == CommunicationIdentifierKind.CommunicationUser:
if not identifier:
raise ValueError("CommunictionUser must have a valid id")
return CommunicationUserIdentifier(id)
if kind == CommunicationIdentifierKind.PhoneNumber:
if not identifierModel.phone_number:
raise ValueError("PhoneNumberIdentifier must have a valid attribute - phone_number")
return PhoneNumberIdentifier(identifierModel.phone_number)
if kind == CommunicationIdentifierKind.MicrosoftTeamsUser:
if identifierModel.is_anonymous not in [True, False]:
raise ValueError("MicrosoftTeamsUser must have a valid attribute - is_anonymous")
if not identifierModel.microsoft_teams_user_id:
raise ValueError("MicrosoftTeamsUser must have a valid attribute - microsoft_teams_user_id")
return MicrosoftTeamsUserIdentifier(
identifierModel.microsoft_teams_user_id,
is_anonymous=identifierModel.is_anonymous
)

if not identifier:
raise ValueError("UnknownIdentifier must have a valid id")

return UnknownIdentifier(identifier)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
# pylint: skip-file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add these in ...n/azure-communication-administration/azure/communication/administration/_shared/models.py?


from enum import Enum, EnumMeta
from six import with_metaclass

import msrest

class CommunicationUserIdentifier(object):
"""
Expand All @@ -22,8 +28,8 @@ class PhoneNumberIdentifier(object):
:param value: Value to initialize PhoneNumberIdentifier.
:type value: str
"""
def __init__(self, value):
self.value = value
def __init__(self, phone_number):
self.phone_number = phone_number

class UnknownIdentifier(object):
"""
Expand Down Expand Up @@ -53,3 +59,69 @@ class MicrosoftTeamsUserIdentifier(object):
def __init__(self, user_id, is_anonymous=False):
self.user_id = user_id
self.is_anonymous = is_anonymous

class CommunicationIdentifierModel(msrest.serialization.Model):
"""Communication Identifier Model.

All required parameters must be populated in order to send to Azure.

:param kind: Required. Kind of Communication Identifier.
:type kind: CommunicationIdentifierKind
:param id: identifies the Communication Identitity.
:type id: str
:param phone_number: phone number in case the identity is phone number.
:type phone_number: str
:param is_anonymous: is the Microsoft Teams user is anaynimous.
:type is_anonymous: bool
:param microsoft_teams_user_id: Microsoft Teams user id.
:type microsoft_teams_user_id: str
"""

_validation = {
'kind': {'required': True},
}

_attribute_map = {
'kind': {'key': 'kind', 'type': 'str'},
'id': {'key': 'id', 'type': 'str'},
'phone_number': {'key': 'phoneNumber', 'type': 'str'},
'is_anonymous': {'key': 'isAnonymous', 'type': 'bool'},
'microsoft_teams_user_id': {'key': 'microsoftTeamsUserId', 'type': 'str'},
}

def __init__(
self,
**kwargs
):
super(CommunicationIdentifierModel, self).__init__(**kwargs)
self.kind = kwargs['kind']
self.id = kwargs.get('id', None)
self.phone_number = kwargs.get('phone_number', None)
self.is_anonymous = kwargs.get('is_anonymous', None)
self.microsoft_teams_user_id = kwargs.get('microsoft_teams_user_id', None)


class _CaseInsensitiveEnumMeta(EnumMeta):
def __getitem__(cls, name):
return super().__getitem__(name.upper())

def __getattr__(cls, name):
"""Return the enum member matching `name`
We use __getattr__ instead of descriptors or inserting into the enum
class' __dict__ in order to support `name` and `value` being both
properties for enum members (which live in the class' __dict__) and
enum members themselves.
"""
try:
return cls._member_map_[name.upper()]
except KeyError:
raise AttributeError(name)

class CommunicationIdentifierKind(with_metaclass(_CaseInsensitiveEnumMeta, str, Enum)):
"""Communication Identifier Kind.
"""
Unknown = "UNKNOWN"
CommunicationUser = "COMMUNICATIONuSER"
PhoneNumber = "PHONEnUMBER"
CallingApplication = "CALLINGAPPLICATION"
MicrosoftTeamsUser = "MICROSOFTTEAMSuSER"
Loading