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 chat preview4 (#16905) #17037

Merged
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
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