Skip to content

Commit d50442b

Browse files
turalfturalf
andauthored
Communication Identity Model serializer/deserializer (Azure#16268)
* Add swagger json from the link to the local file * Add CommunicationIdentifierModel into swagger * Add CommunicationIdentifierModel to models * Add py3 CommunicationIdentifierModel to models * Add CommunicationIdentifierKind * Add serialize to CommunicationIdentifierModel * Remove Value suffix from the "kind" enums entries * Add deserizlize to CommunicationUserIdentifierSerializer * Replace "self" with "cls" in classmethods * Replace value property with phone_number * Add fileds validation in anonymous * Add serialize/deserialize unittest * Fix model doc strings * Remove whiltespaces * Add header; remove whitespace * Replace self with cls * Remove empty line * Remove py3 model * Rename var name from id to identifier * Capitalize enum values * Add pylint skip for models file Co-authored-by: turalf <tufarhad@microsoft.com>
1 parent df2f634 commit d50442b

File tree

6 files changed

+1938
-6
lines changed

6 files changed

+1938
-6
lines changed

sdk/communication/azure-communication-administration/azure/communication/administration/_shared/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class PhoneNumberIdentifier(object):
2222
:param value: Value to initialize PhoneNumberIdentifier.
2323
:type value: str
2424
"""
25-
def __init__(self, value):
26-
self.value = value
25+
def __init__(self, phone_number):
26+
self.phone_number = phone_number
2727

2828
class UnknownIdentifier(object):
2929
"""
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
from .models import (
8+
CommunicationIdentifierKind,
9+
CommunicationIdentifierModel,
10+
CommunicationUserIdentifier,
11+
PhoneNumberIdentifier,
12+
MicrosoftTeamsUserIdentifier,
13+
UnknownIdentifier
14+
)
15+
16+
class CommunicationUserIdentifierSerializer(object):
17+
18+
@classmethod
19+
def serialize(cls, communicationIdentifier):
20+
""" Serialize the Communication identifier into CommunicationIdentifierModel
21+
22+
:param identifier: Communication service identifier
23+
:type identifier: Union[CommunicationUserIdentifier, CommunicationPhoneNumberIdentifier]
24+
:return: CommunicationIdentifierModel
25+
:rtype: ~azure.communication.chat.CommunicationIdentifierModel
26+
"""
27+
if isinstance(communicationIdentifier, CommunicationUserIdentifier):
28+
return CommunicationIdentifierModel(
29+
kind=CommunicationIdentifierKind.CommunicationUser,
30+
id=communicationIdentifier.identifier
31+
)
32+
if isinstance(communicationIdentifier, PhoneNumberIdentifier):
33+
return CommunicationIdentifierModel(
34+
kind=CommunicationIdentifierKind.PhoneNumber,
35+
id=communicationIdentifier.phone_number
36+
)
37+
if isinstance(communicationIdentifier, MicrosoftTeamsUserIdentifier):
38+
return CommunicationIdentifierModel(
39+
kind=CommunicationIdentifierKind.MicrosoftTeamsUser,
40+
id=communicationIdentifier.user_id
41+
)
42+
43+
return CommunicationIdentifierModel(
44+
kind=CommunicationIdentifierKind.Unknown,
45+
id=communicationIdentifier.identifier
46+
)
47+
48+
@classmethod
49+
def deserialize(cls, identifierModel):
50+
"""
51+
Deserialize the CommunicationIdentifierModel into Communication Identifier
52+
53+
:param identifierModel: CommunicationIdentifierModel
54+
:type identifierModel: CommunicationIdentifierModel
55+
:return: Union[CommunicationUserIdentifier, CommunicationPhoneNumberIdentifier]
56+
:rtype: Union[CommunicationUserIdentifier, CommunicationPhoneNumberIdentifier]
57+
:rasies: ValueError
58+
"""
59+
60+
identifier, kind = identifierModel.id, identifierModel.kind
61+
62+
if kind == CommunicationIdentifierKind.CommunicationUser:
63+
if not identifier:
64+
raise ValueError("CommunictionUser must have a valid id")
65+
return CommunicationUserIdentifier(id)
66+
if kind == CommunicationIdentifierKind.PhoneNumber:
67+
if not identifierModel.phone_number:
68+
raise ValueError("PhoneNumberIdentifier must have a valid attribute - phone_number")
69+
return PhoneNumberIdentifier(identifierModel.phone_number)
70+
if kind == CommunicationIdentifierKind.MicrosoftTeamsUser:
71+
if identifierModel.is_anonymous not in [True, False]:
72+
raise ValueError("MicrosoftTeamsUser must have a valid attribute - is_anonymous")
73+
if not identifierModel.microsoft_teams_user_id:
74+
raise ValueError("MicrosoftTeamsUser must have a valid attribute - microsoft_teams_user_id")
75+
return MicrosoftTeamsUserIdentifier(
76+
identifierModel.microsoft_teams_user_id,
77+
is_anonymous=identifierModel.is_anonymous
78+
)
79+
80+
if not identifier:
81+
raise ValueError("UnknownIdentifier must have a valid id")
82+
83+
return UnknownIdentifier(identifier)

sdk/communication/azure-communication-chat/azure/communication/chat/_shared/models.py

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5+
# pylint: skip-file
6+
7+
from enum import Enum, EnumMeta
8+
from six import with_metaclass
9+
10+
import msrest
511

612
class CommunicationUserIdentifier(object):
713
"""
@@ -22,8 +28,8 @@ class PhoneNumberIdentifier(object):
2228
:param value: Value to initialize PhoneNumberIdentifier.
2329
:type value: str
2430
"""
25-
def __init__(self, value):
26-
self.value = value
31+
def __init__(self, phone_number):
32+
self.phone_number = phone_number
2733

2834
class UnknownIdentifier(object):
2935
"""
@@ -53,3 +59,69 @@ class MicrosoftTeamsUserIdentifier(object):
5359
def __init__(self, user_id, is_anonymous=False):
5460
self.user_id = user_id
5561
self.is_anonymous = is_anonymous
62+
63+
class CommunicationIdentifierModel(msrest.serialization.Model):
64+
"""Communication Identifier Model.
65+
66+
All required parameters must be populated in order to send to Azure.
67+
68+
:param kind: Required. Kind of Communication Identifier.
69+
:type kind: CommunicationIdentifierKind
70+
:param id: identifies the Communication Identitity.
71+
:type id: str
72+
:param phone_number: phone number in case the identity is phone number.
73+
:type phone_number: str
74+
:param is_anonymous: is the Microsoft Teams user is anaynimous.
75+
:type is_anonymous: bool
76+
:param microsoft_teams_user_id: Microsoft Teams user id.
77+
:type microsoft_teams_user_id: str
78+
"""
79+
80+
_validation = {
81+
'kind': {'required': True},
82+
}
83+
84+
_attribute_map = {
85+
'kind': {'key': 'kind', 'type': 'str'},
86+
'id': {'key': 'id', 'type': 'str'},
87+
'phone_number': {'key': 'phoneNumber', 'type': 'str'},
88+
'is_anonymous': {'key': 'isAnonymous', 'type': 'bool'},
89+
'microsoft_teams_user_id': {'key': 'microsoftTeamsUserId', 'type': 'str'},
90+
}
91+
92+
def __init__(
93+
self,
94+
**kwargs
95+
):
96+
super(CommunicationIdentifierModel, self).__init__(**kwargs)
97+
self.kind = kwargs['kind']
98+
self.id = kwargs.get('id', None)
99+
self.phone_number = kwargs.get('phone_number', None)
100+
self.is_anonymous = kwargs.get('is_anonymous', None)
101+
self.microsoft_teams_user_id = kwargs.get('microsoft_teams_user_id', None)
102+
103+
104+
class _CaseInsensitiveEnumMeta(EnumMeta):
105+
def __getitem__(cls, name):
106+
return super().__getitem__(name.upper())
107+
108+
def __getattr__(cls, name):
109+
"""Return the enum member matching `name`
110+
We use __getattr__ instead of descriptors or inserting into the enum
111+
class' __dict__ in order to support `name` and `value` being both
112+
properties for enum members (which live in the class' __dict__) and
113+
enum members themselves.
114+
"""
115+
try:
116+
return cls._member_map_[name.upper()]
117+
except KeyError:
118+
raise AttributeError(name)
119+
120+
class CommunicationIdentifierKind(with_metaclass(_CaseInsensitiveEnumMeta, str, Enum)):
121+
"""Communication Identifier Kind.
122+
"""
123+
Unknown = "UNKNOWN"
124+
CommunicationUser = "COMMUNICATIONuSER"
125+
PhoneNumber = "PHONEnUMBER"
126+
CallingApplication = "CALLINGAPPLICATION"
127+
MicrosoftTeamsUser = "MICROSOFTTEAMSuSER"

0 commit comments

Comments
 (0)