Skip to content

Commit

Permalink
Communication chat preview4 (#16905)
Browse files Browse the repository at this point in the history
* [Communication] Generate identifier Models from new swagger (#16735)
* Add generated chat code from new swagger
* Address PR Feedback
* Remove CommunicationUserIdentifierModel in identity,phone number package
* Check schema of the object to determine the type [preview4] (#16838)
* Replace identifier with rawId
* Change serilizer
* Replace indentifier with rawId in test code
* Sync models across modules
* fix typo in serizliser
* Rearrange imports
* Replace rawId with raw_id
* remove trailing newline

Co-authored-by: turalf <tufarhad@microsoft.com>

* preview4 changes made + unit tests fixed

* Chat - preview4 changes
- CommunicationUserIdentifier models added
- create_chat_thread - returns CreateChatThreadResult instead of ChatThreadClient
- add_participant - docstring update AddChatParticipantsResult instead of None
- add_participants - docstring update AddChatParticipantsResult instead of None

* pylint-changes

* pylint changes

* Method signature changed for add_pariticipant and add_participants
- add_participant - AddChatParticipantsResult -> tuple(ChatThreadParticipant, CommunicationError)
- add_participants - AddChatParticipantsResult -> list(tuple(ChatThreadParticipant, CommunicationError))
- unit tests modified as per signature change
- CommunicationErrorResponseConverter added to cosolidate list(ChatThreadParticipant) and list(CommunicationError) into list(tuple(ChatThreadParticipant, CommunicationError))
- e2e tests modified as per signature change

* CreateChatThreadResult modified to handle partial errors in batch calls with ease
- CreateChatThreadResult -> attributes changed to
  - chat_thread -> ChatThread (no change)
  - Errors -> CreateChatThreadErrors -> list(tuple(ChatThreadParticipant, CommunicationError))
- create_chat_thread -> `thread_participants` and `repeatability_request_id` changed to keyword arguments
- Modify unit tests to capture method signature modifications
- Modify e2e tests to capture method signature modifications

* pylint-changes

* pylint fixes

* README.md update + pylint fixes

* test recordings added

* add_participant -> raises error
- Update README.md with modified signature
- Update samples with new method signatures
- Add test to detect invalid instantiation of AccessToken
- Minor documentation updates
- Modify unit tests to capture method signature modifications
- Modify e2e tests to capture method signature modifications

* pylint fixes

* cls removed from docstring + update_topic async refactored

* cls removed from docstring

Co-authored-by: Sam Cheung <sacheu@microsoft.com>
Co-authored-by: turalf <tural.ferhadov@gmail.com>
Co-authored-by: turalf <tufarhad@microsoft.com>
  • Loading branch information
4 people authored Mar 3, 2021
1 parent 1a60ae0 commit 3683f00
Show file tree
Hide file tree
Showing 81 changed files with 5,906 additions and 3,696 deletions.
367 changes: 267 additions & 100 deletions sdk/communication/azure-communication-chat/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
from ._generated.models import (
SendChatMessageResult,
ChatThreadInfo,
ChatMessageType
ChatMessageType,
CommunicationError
)
from ._shared.user_credential import CommunicationTokenCredential
from ._shared.user_token_refresh_options import CommunicationTokenRefreshOptions

from ._models import (
ChatThreadParticipant,
ChatMessage,
ChatThread,
ChatMessageReadReceipt,
ChatMessageContent
ChatMessageContent,
CreateChatThreadResult
)
from ._shared.models import CommunicationUserIdentifier

__all__ = [
'ChatClient',
Expand All @@ -26,10 +26,9 @@
'SendChatMessageResult',
'ChatThread',
'ChatThreadInfo',
'CommunicationTokenCredential',
'CommunicationTokenRefreshOptions',
'CommunicationUserIdentifier',
'ChatThreadParticipant',
'ChatMessageType'
'ChatMessageType',
'CreateChatThreadResult',
'CommunicationError'
]
__version__ = VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
from ._generated.models import CreateChatThreadRequest
from ._models import (
ChatThread,
ChatThreadParticipant
CreateChatThreadResult
)
from ._utils import ( # pylint: disable=unused-import
_to_utc_datetime,
return_response,
CommunicationErrorResponseConverter
)
from ._utils import _to_utc_datetime, return_response # pylint: disable=unused-import
from ._version import SDK_MONIKER

if TYPE_CHECKING:
Expand Down Expand Up @@ -119,26 +123,24 @@ def get_chat_thread_client(
@distributed_trace
def create_chat_thread(
self, topic, # type: str
thread_participants, # type: list[ChatThreadParticipant]
repeatability_request_id=None, # type: Optional[str]
**kwargs # type: Any
):
# type: (...) -> ChatThreadClient
# type: (...) -> CreateChatThreadResult
"""Creates a chat thread.
:param topic: Required. The thread topic.
:type topic: str
:param thread_participants: Required. Participants to be added to the thread.
:type thread_participants: list[~azure.communication.chat.ChatThreadParticipant]
:param repeatability_request_id: If specified, the client directs that the request is
:keyword thread_participants: Optional. Participants to be added to the thread.
:paramtype thread_participants: list[~azure.communication.chat.ChatThreadParticipant]
:keyword repeatability_request_id: Optional. If specified, the client directs that the request is
repeatable; that is, that the client can make the request multiple times with the same
Repeatability-Request-ID and get back an appropriate response without the server executing the
request multiple times. The value of the Repeatability-Request-ID is an opaque string
representing a client-generated, globally unique for all time, identifier for the request. If not
specified, a new unique id would be generated.
:type repeatability_request_id: str
:return: ChatThreadClient
:rtype: ~azure.communication.chat.ChatThreadClient
:paramtype repeatability_request_id: str
:return: CreateChatThreadResult
:rtype: ~azure.communication.chat.CreateChatThreadResult
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
.. admonition:: Example:
Expand All @@ -148,40 +150,45 @@ def create_chat_thread(
:end-before: [END create_thread]
:language: python
:dedent: 8
:caption: Creating ChatThreadClient by creating a new chat thread.
:caption: Creating ChatThread by creating a new chat thread.
"""
if not topic:
raise ValueError("topic cannot be None.")
if not thread_participants:
raise ValueError("List of ChatThreadParticipant cannot be None.")

repeatability_request_id = kwargs.pop('repeatability_request_id', None)
if repeatability_request_id is None:
repeatability_request_id = str(uuid4())

participants = [m._to_generated() for m in thread_participants] # pylint:disable=protected-access
create_thread_request = \
CreateChatThreadRequest(topic=topic, participants=participants)
thread_participants = kwargs.pop('thread_participants', None)
participants = []
if thread_participants is not None:
participants = [m._to_generated() for m in thread_participants] # pylint:disable=protected-access

create_thread_request = CreateChatThreadRequest(topic=topic, participants=participants)

create_chat_thread_result = self._client.chat.create_chat_thread(
create_chat_thread_request=create_thread_request,
repeatability_request_id=repeatability_request_id,
**kwargs)

errors = None
if hasattr(create_chat_thread_result, 'errors') and \
create_chat_thread_result.errors is not None:
participants = \
create_chat_thread_result.errors.invalid_participants
errors = []
for participant in participants:
errors.append('participant ' + participant.target +
' failed to join thread due to: ' + participant.message)
raise RuntimeError(errors)
thread_id = create_chat_thread_result.chat_thread.id
return ChatThreadClient(
endpoint=self._endpoint,
credential=self._credential,
thread_id=thread_id,
**kwargs
errors = CommunicationErrorResponseConverter._convert( # pylint:disable=protected-access
participants=[thread_participants],
communication_errors=create_chat_thread_result.errors.invalid_participants
)

chat_thread = ChatThread._from_generated( # pylint:disable=protected-access
create_chat_thread_result.chat_thread)

create_chat_thread_result = CreateChatThreadResult(
chat_thread=chat_thread,
errors=errors
)

return create_chat_thread_result

@distributed_trace
def get_chat_thread(
self, thread_id, # type: str
Expand All @@ -192,8 +199,7 @@ def get_chat_thread(
:param thread_id: Required. Thread id to get.
:type thread_id: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: ChatThread, or the result of cls(response)
:return: ChatThread
:rtype: ~azure.communication.chat.ChatThread
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
Expand Down Expand Up @@ -254,8 +260,7 @@ def delete_chat_thread(
:param thread_id: Required. Thread id to delete.
:type thread_id: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:return: None
:rtype: None
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
Expand Down
Loading

0 comments on commit 3683f00

Please sign in to comment.