Skip to content

Commit

Permalink
Updated swagger changes (#16390)
Browse files Browse the repository at this point in the history
* Updated swagger changes
- Generate new models from swagger
- ChatMessage.content -> ChatMessageContent instead of 'str'
- Remove ChatMessagePriority
- Introduce ChatMessageType
- Add tests around ChatMessageType deserialization
- Generate a repeatability ID by default
- Add some test scenarios around repeatability ID
- Update all relevant tests
- Update sample code
- Record new test sessions
  • Loading branch information
sarkar-rajarshi authored Jan 28, 2021
1 parent db42686 commit 78dc2ab
Show file tree
Hide file tree
Showing 65 changed files with 3,230 additions and 2,041 deletions.
42 changes: 39 additions & 3 deletions sdk/communication/azure-communication-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ You can get it by creating a new chat thread using ChatClient:
chat_thread_client = chat_client.create_chat_thread(topic, thread_participants)
```

Additionally, the client can also direct so that the request is repeatable; that is, if the client makes the
request multiple times with the same Repeatability-Request-ID and it will 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.

```python
chat_thread_client = chat_client.create_chat_thread(topic, thread_participants, repeatability_request_id)
```

Alternatively, if you have created a chat thread before and you have its thread_id, you can create it by:

```python
Expand Down Expand Up @@ -141,6 +150,7 @@ Use the `create_chat_thread` method to create a chat thread client object.

- Use `topic` to give a thread topic;
- Use `thread_participants` to list the `ChatThreadParticipant` to be added to the thread;
- Use `repeatability_request_id` to specify the unique identifier for the request
- `user`, required, it is the `CommunicationUser` you created by CommunicationIdentityClient.create_user() from User Access Tokens
<!-- [User Access Tokens](#user-access-tokens) -->
- `display_name`, optional, is the display name for the thread participant.
Expand All @@ -149,6 +159,8 @@ Use the `create_chat_thread` method to create a chat thread client object.
`ChatThreadClient` is the result returned from creating a thread, you can use it to perform other chat operations to this chat thread

```Python
# Without repeatability_request_id

from azure.communication.chat import ChatThreadParticipant
topic = "test topic"
thread_participants = [ChatThreadParticipant(
Expand All @@ -161,6 +173,31 @@ chat_thread_client = chat_client.create_chat_thread(topic, thread_participants)
thread_id = chat_thread_client.thread_id
```

```Python
# With repeatability_request_id

from azure.communication.chat import ChatThreadParticipant
import uuid

# modify function to implement customer logic
def get_unique_identifier_for_request(**kwargs):
res = None
# implement custom logic here
res = uuid.uuid4()
return res

topic = "test topic"
thread_participants = [ChatThreadParticipant(
user='<user>',
display_name='name',
share_history_time=datetime.utcnow()
)]

chat_thread_client = chat_client.create_chat_thread(topic, thread_participants, repeatability_request_id)
thread_id = chat_thread_client.thread_id
```


### Get a thread

The `get_chat_thread` method retrieves a thread from the service.
Expand Down Expand Up @@ -215,7 +252,7 @@ chat_client.delete_chat_thread(thread_id)
Use `send_message` method to sends a message to a thread identified by threadId.

- Use `content` to provide the chat message content, it is required
- Use `priority` to specify the message priority level, such as 'Normal' or 'High', if not speficied, 'Normal' will be set
- Use `chat_message_type` to provide the chat message type. Possible values include: `ChatMessageType.TEXT`, `ChatMessageType.HTML`, `ChatMessageType.TOPIC_UPDATED`, `ChatMessageType.PARTICIPANT_ADDED`, `ChatMessageType.PARTICIPANT_REMOVED`
- Use `sender_display_name` to specify the display name of the sender, if not specified, empty name will be set

`SendChatMessageResult` is the response returned from sending a message, it contains an id, which is the unique ID of the message.
Expand All @@ -224,10 +261,9 @@ Use `send_message` method to sends a message to a thread identified by threadId.
from azure.communication.chat import ChatMessagePriority

content='hello world'
priority=ChatMessagePriority.NORMAL
sender_display_name='sender name'

send_message_result = chat_thread_client.send_message(content, priority=priority, sender_display_name=sender_display_name)
send_message_result = chat_thread_client.send_message(content, sender_display_name=sender_display_name)
```

### Get a message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@
from ._chat_client import ChatClient
from ._chat_thread_client import ChatThreadClient
from ._generated.models import (
ChatMessagePriority,
SendChatMessageResult,
ChatThreadInfo,
ChatMessageType
)
from ._shared.user_credential import CommunicationUserCredential
from ._models import (
ChatThreadParticipant,
ChatMessage,
ChatThread,
ChatMessageReadReceipt,
ChatMessageContent
)
from ._shared.models import CommunicationUser

__all__ = [
'ChatClient',
'ChatThreadClient',
'ChatMessage',
'ChatMessagePriority',
'ChatMessageContent',
'ChatMessageReadReceipt',
'SendChatMessageResult',
'ChatThread',
'ChatThreadInfo',
'CommunicationUserCredential',
'ChatThreadParticipant',
'CommunicationUser',
'ChatMessageType'
]
__version__ = VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license information.
# --------------------------------------------------------------------------
from typing import TYPE_CHECKING

from uuid import uuid4
try:
from urllib.parse import urlparse
except ImportError:
Expand Down Expand Up @@ -120,6 +120,7 @@ def get_chat_thread_client(
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
Expand All @@ -129,6 +130,13 @@ def create_chat_thread(
: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
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
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
Expand All @@ -146,13 +154,17 @@ def create_chat_thread(
raise ValueError("topic cannot be None.")
if not thread_participants:
raise ValueError("List of ChatThreadParticipant cannot be 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)

create_chat_thread_result = self._client.chat.create_chat_thread(
create_thread_request, **kwargs)
create_chat_thread_request=create_thread_request,
repeatability_request_id=repeatability_request_id,
**kwargs)
if hasattr(create_chat_thread_result, 'errors') and \
create_chat_thread_result.errors is not None:
participants = \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
SendReadReceiptRequest,
SendChatMessageRequest,
UpdateChatMessageRequest,
UpdateChatThreadRequest
UpdateChatThreadRequest,
ChatMessageType
)
from ._models import (
ChatThreadParticipant,
Expand Down Expand Up @@ -248,8 +249,9 @@ def send_message(
:param content: Required. Chat message content.
:type content: str
:keyword priority: Message priority.
:paramtype priority: str or ChatMessagePriority
:param chat_message_type: The chat message type. Possible values include: "text", "html", "participant_added",
"participant_removed", "topic_updated" Default: ChatMessageType.TEXT
:type chat_message_type: str or ~azure.communication.chat.models.ChatMessageType
:keyword str sender_display_name: The display name of the message sender. This property is used to
populate sender name for push notifications.
:keyword callable cls: A custom type or function that will be passed the direct response
Expand All @@ -269,12 +271,21 @@ def send_message(
if not content:
raise ValueError("content cannot be None.")

priority = kwargs.pop("priority", None)
chat_message_type = kwargs.pop("chat_message_type", None)
if chat_message_type is None:
chat_message_type = ChatMessageType.TEXT
elif not isinstance(chat_message_type, ChatMessageType):
try:
chat_message_type = ChatMessageType.__getattr__(chat_message_type) # pylint:disable=protected-access
except Exception:
raise ValueError(
"chat_message_type: {message_type} is not acceptable".format(message_type=chat_message_type))

sender_display_name = kwargs.pop("sender_display_name", None)

create_message_request = SendChatMessageRequest(
content=content,
priority=priority,
type=chat_message_type,
sender_display_name=sender_display_name
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ async def create_chat_thread(
error_map = {
404: ResourceNotFoundError,
409: ResourceExistsError,
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.Error, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2020-11-01-preview3"
Expand Down Expand Up @@ -140,10 +140,10 @@ def list_chat_threads(
error_map = {
404: ResourceNotFoundError,
409: ResourceExistsError,
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.Error, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2020-11-01-preview3"
Expand Down Expand Up @@ -224,10 +224,10 @@ async def get_chat_thread(
error_map = {
404: ResourceNotFoundError,
409: ResourceExistsError,
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.Error, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2020-11-01-preview3"
Expand Down Expand Up @@ -285,10 +285,10 @@ async def delete_chat_thread(
error_map = {
404: ResourceNotFoundError,
409: ResourceExistsError,
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.Error, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.Error, response)),
401: lambda response: ClientAuthenticationError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
403: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
429: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
503: lambda response: HttpResponseError(response=response, model=self._deserialize(_models.CommunicationErrorResponse, response)),
}
error_map.update(kwargs.pop('error_map', {}))
api_version = "2020-11-01-preview3"
Expand Down
Loading

0 comments on commit 78dc2ab

Please sign in to comment.