-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
018c31a
Add swagger json from the link to the local file
b4fc81f
Add CommunicationIdentifierModel into swagger
7103bf7
Add CommunicationIdentifierModel to models
d7021b1
Add py3 CommunicationIdentifierModel to models
b51bfeb
Add CommunicationIdentifierKind
3af2c5a
Add serialize to CommunicationIdentifierModel
8ac3fe5
Remove Value suffix from the "kind" enums entries
b301e37
Add deserizlize to CommunicationUserIdentifierSerializer
50dfc4a
Replace "self" with "cls" in classmethods
0dc145c
Replace value property with phone_number
b887fa8
Add fileds validation in anonymous
1090b03
Add serialize/deserialize unittest
ee1d54b
Fix model doc strings
7849ab2
Remove whiltespaces
5f84ea7
Add header; remove whitespace
fe19ab3
Replace self with cls
901ea47
Remove empty line
c10ca4c
Remove py3 model
247f26a
Rename var name from id to identifier
495ae78
Capitalize enum values
418f181
Add pylint skip for models file
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...ommunication-chat/azure/communication/chat/_shared/communication_identifier_serializer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?