Skip to content

Commit 388beb9

Browse files
Rsarkar/communication chat preview5 (Azure#17325)
* Rename user_identifier suffix (Azure#17178) * make CommunicationUserIdentifierSerializer private * CommunicationTokenCredential + AccessToken fix - Initialize CommunicationTokenCredential with token and not with CommunicationTokenRefreshOptions - Convert AccessToken.expires_on to int as compared to datetime * add token_refresher to CommunicationTokenCredential * update docstring * Identifier fields renaming _ credential import fixes in sample code * preview5 models generated - CommunicationError renamed to ChatError * rename ChatThread to ChatThreadProperties * get_chat_thread renamed to get_properties - Rename method - Move get_properties from ChatClient to ChatThreadClient - Change method signature - remove parameter thread_id - Modify unit and e2e tests - move tests from chat_thread_* to chat_thread_client_* - Update README.md * Rename ChatThreadInfo to ChatThreadItem * Remove add_participant * Rename repeatability_request_id to idempotency_token * Change send_message return type from str to SendChatMessageResult * Changes made due to rename of CommunicationError -> ChatError - Changes made due to flattening of CreateChatThreadResult and AddChatParticipantsResult * test recording added * Address PR comments * pylint fixes
1 parent ef231f1 commit 388beb9

File tree

90 files changed

+3472
-6566
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3472
-6566
lines changed

sdk/communication/azure-communication-chat/CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
# Release History
22

33
## 1.0.0b6 (Unreleased)
4-
4+
### Breaking Changes
5+
- Renamed `ChatThread` to `ChatThreadProperties`.
6+
- Renamed `get_chat_thread` to `get_properties`.
7+
- Moved `get_properties` under `ChatThreadClient`.
8+
- Renamed `ChatThreadInfo` to `ChatThreadItem`.
9+
- Removed `ChatThreadClient.add_participant` method.
10+
- Renamed `repeatability_request_id` to `idempotency_token`.
11+
- Changed return type of `send_message` to `SendChatMessageResult`.
12+
- Replaced `CommunicationError` with `ChatError`.
13+
- Refactored `CommunicationTokenCredential` constructor to accept `token` instead of `CommunicationTokenRefreshOptions`.
514

615
## 1.0.0b5 (2021-03-09)
716
### Breaking Changes

sdk/communication/azure-communication-chat/README.md

Lines changed: 36 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ it with this token. It is because the initiator of the create request must be in
4444
This will allow you to create, get, list or delete chat threads.
4545

4646
```python
47-
from azure.communication.chat import ChatClient
48-
from azure.communication.identity._shared.user_credential import CommunicationTokenCredential
49-
from azure.communication.identity._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
47+
from azure.communication.chat import ChatClient, CommunicationTokenCredential
5048

5149
# Your unique Azure Communication service endpoint
5250
endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com"
53-
refresh_options = CommunicationTokenRefreshOptions(token)
54-
chat_client = ChatClient(endpoint, CommunicationTokenCredential(refresh_options))
51+
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
5552
```
5653

5754
## Create Chat Thread Client
@@ -67,15 +64,15 @@ chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_resul
6764
```
6865

6966
Additionally, the client can also direct so that the request is repeatable; that is, if the client makes the
70-
request multiple times with the same Repeatability-Request-ID and it will get back an appropriate response without
71-
the server executing the request multiple times. The value of the Repeatability-Request-ID is an opaque string
67+
request multiple times with the same Idempotency-Token and it will get back an appropriate response without
68+
the server executing the request multiple times. The value of the Idempotency-Token is an opaque string
7269
representing a client-generated, globally unique for all time, identifier for the request.
7370

7471
```python
7572
create_chat_thread_result = chat_client.create_chat_thread(
7673
topic,
7774
thread_participants=thread_participants,
78-
repeatability_request_id=repeatability_request_id
75+
idempotency_token=idempotency_token
7976
)
8077
chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id)
8178
```
@@ -96,11 +93,10 @@ Once you initialized a `ChatClient` class, you can do the following chat operati
9693

9794
## Create, get, update, and delete threads
9895

99-
Perform CRD(Create-Read-Delete) operations on thread participants
96+
Perform CRD(Create-Read-Delete) operations on threads
10097

10198
```Python
10299
create_chat_thread(topic, **kwargs)
103-
get_chat_thread(thread_id, **kwargs)
104100
list_chat_threads(**kwargs)
105101
delete_chat_thread(thread_id, **kwargs)
106102
```
@@ -115,6 +111,11 @@ Perform Update operation on thread topic
115111
update_topic(topic, **kwargs)
116112
```
117113

114+
## Get Chat thread properties
115+
```python
116+
get_properties(**kwargs)
117+
```
118+
118119
## Send, get, update, and delete messages
119120

120121
Perform CRUD(Create-Read-Update-Delete) operations on messages
@@ -133,7 +134,6 @@ Perform CRD(Create-Read-Delete) operations on thread participants
133134

134135
```Python
135136
list_participants(**kwargs)
136-
add_participant(thread_participant, **kwargs)
137137
add_participants(thread_participants, **kwargs)
138138
remove_participant(participant_id, **kwargs)
139139
```
@@ -177,22 +177,22 @@ Use the `create_chat_thread` method to create a chat thread.
177177
<!-- [User Access Tokens](#user-access-tokens) -->
178178
- `display_name`, optional, is the display name for the thread participant.
179179
- `share_history_time`, optional, time from which the chat history is shared with the participant.
180-
- Use `repeatability_request_id`, optional, to specify the unique identifier for the request.
180+
- Use `idempotency_token`, optional, to specify the unique identifier for the request.
181181

182182

183183
`CreateChatThreadResult` is the result returned from creating a thread, you can use it to fetch the `id` of
184184
the chat thread that got created. This `id` can then be used to fetch a `ChatThreadClient` object using
185185
the `get_chat_thread_client` method. `ChatThreadClient` can be used to perform other chat operations to this chat thread.
186186

187187
```Python
188-
# Without repeatability_request_id and thread_participants
188+
# Without idempotency_token and thread_participants
189189
topic = "test topic"
190190
create_chat_thread_result = chat_client.create_chat_thread(topic)
191191
chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id)
192192
```
193193

194194
```Python
195-
# With repeatability_request_id and thread_participants
195+
# With idempotency_token and thread_participants
196196
from azure.communication.identity import CommunicationIdentityClient
197197
from azure.communication.chat import ChatThreadParticipant
198198
import uuid
@@ -219,13 +219,13 @@ thread_participants = [ChatThreadParticipant(
219219
share_history_time=datetime.utcnow()
220220
)]
221221

222-
# obtains repeatability_request_id using some customer logic
223-
repeatability_request_id = get_unique_identifier_for_request()
222+
# obtains idempotency_token using some customer logic
223+
idempotency_token = get_unique_identifier_for_request()
224224

225225
create_chat_thread_result = chat_client.create_chat_thread(
226226
topic,
227227
thread_participants=thread_participants,
228-
repeatability_request_id=repeatability_request_id)
228+
idempotency_token=idempotency_token)
229229
thread_id = create_chat_thread_result.chat_thread.id
230230

231231
# fetch ChatThreadClient
@@ -240,16 +240,17 @@ def decide_to_retry(error, **kwargs):
240240
return True
241241

242242
retry = [thread_participant for thread_participant, error in create_chat_thread_result.errors if decide_to_retry(error)]
243-
chat_thread_client.add_participants(retry)
243+
if len(retry) > 0:
244+
chat_thread_client.add_participants(retry)
244245
```
245246

246247

247248
### Get a thread
248249

249-
Use `get_chat_thread` method retrieves a `ChatThread` from the service; `thread_id` is the unique ID of the thread.
250-
- Use `thread_id`, required, to specify the unique ID of the thread.
250+
Use `get_properties` method retrieves a `ChatThreadProperties` from the service; `thread_id` is the unique ID of the thread.
251+
251252
```Python
252-
chat_thread = chat_client.get_chat_thread(thread_id=thread_id)
253+
chat_thread_properties = chat_thread_client.get_properties()
253254
```
254255

255256
### List chat threads
@@ -258,7 +259,7 @@ Use `list_chat_threads` method retrieves the list of created chat threads
258259
- Use `results_per_page`, optional, The maximum number of messages to be returned per page.
259260
- Use `start_time`, optional, The start time where the range query.
260261

261-
An iterator of `[ChatThreadInfo]` is the response returned from listing threads
262+
An iterator of `[ChatThreadItem]` is the response returned from listing threads
262263

263264
```python
264265
from datetime import datetime, timedelta
@@ -267,10 +268,10 @@ import pytz
267268
start_time = datetime.utcnow() - timedelta(days=2)
268269
start_time = start_time.replace(tzinfo=pytz.utc)
269270

270-
chat_thread_infos = chat_client.list_chat_threads(results_per_page=5, start_time=start_time)
271-
for chat_thread_info_page in chat_thread_infos.by_page():
272-
for chat_thread_info in chat_thread_info_page:
273-
print(chat_thread_info)
271+
chat_threads = chat_client.list_chat_threads(results_per_page=5, start_time=start_time)
272+
for chat_thread_item_page in chat_threads.by_page():
273+
for chat_thread_item in chat_thread_item_page:
274+
print("thread id:", chat_thread_item.id)
274275
```
275276

276277
### Update a thread topic
@@ -279,10 +280,10 @@ Use `update_topic` method to update a thread's properties. `topic` is used to de
279280
- Use `topic` to give thread a new topic;
280281

281282
```python
282-
topic="new topic"
283+
topic = "new topic"
283284
chat_thread_client.update_topic(topic=topic)
284285

285-
chat_thread = chat_client.get_chat_thread(thread_id)
286+
chat_thread = chat_client.get_properties(thread_id)
286287

287288
assert chat_thread.topic == topic
288289
```
@@ -322,15 +323,17 @@ sender_display_name='sender name'
322323
chat_message_type = ChatMessageType.TEXT
323324

324325
# without specifying sender_display_name and chat_message_type
325-
send_message_result_id = chat_thread_client.send_message(content)
326+
send_message_result = chat_thread_client.send_message(content)
327+
send_message_result_id = send_message_result.id
326328
print("Message sent: id: ", send_message_result_id)
327329

328330
# specifying sender_display_name and chat_message_type
329-
send_message_result_w_type_id = chat_thread_client.send_message(
331+
send_message_result_w_type = chat_thread_client.send_message(
330332
content,
331333
sender_display_name=sender_display_name,
332334
chat_message_type=chat_message_type # equivalent to chat_message_type = 'text'
333335
)
336+
send_message_result_w_type_id = send_message_result_w_type.id
334337
print("Message sent: id: ", send_message_result_w_type_id)
335338
```
336339

@@ -407,55 +410,6 @@ for chat_thread_participant_page in chat_thread_participants.by_page():
407410
print("ChatThreadParticipant: ", chat_thread_participant)
408411
```
409412

410-
### Add single thread participant
411-
Use `add_participant` method to add a single thread participants to the thread.
412-
413-
- Use `thread_participant`, required, to specify the `ChatThreadParticipant` to be added to the thread;
414-
- `user`, required, it is the `CommunicationUserIdentifier` you created by CommunicationIdentityClient.create_user() from User Access Tokens
415-
<!-- [User Access Tokens](#user-access-tokens) -->
416-
- `display_name`, optional, is the display name for the thread participant.
417-
- `share_history_time`, optional, time from which the chat history is shared with the participant.
418-
419-
When participant is successfully added, no error is thrown. In case of an error encountered while adding participant,
420-
a `RuntimeError` is thrown
421-
```python
422-
from azure.communication.identity import CommunicationIdentityClient
423-
from azure.communication.chat import ChatThreadParticipant
424-
from datetime import datetime
425-
426-
# create an user
427-
identity_client = CommunicationIdentityClient.from_connection_string('<connection_string>')
428-
new_user = identity_client.create_user()
429-
430-
# # conversely, you can also add an existing user to a chat thread; provided the user_id is known
431-
# from azure.communication.identity import CommunicationUserIdentifier
432-
#
433-
# user_id = 'some user id'
434-
# user_display_name = "Wilma Flinstone"
435-
# new_user = CommunicationUserIdentifier(user_id)
436-
# participant = ChatThreadParticipant(
437-
# user=new_user,
438-
# display_name=user_display_name,
439-
# share_history_time=datetime.utcnow())
440-
441-
def decide_to_retry(error, **kwargs):
442-
"""
443-
Insert some custom logic to decide if retry is applicable based on error
444-
"""
445-
return True
446-
447-
participant = ChatThreadParticipant(
448-
user=new_user,
449-
display_name='Fred Flinstone',
450-
share_history_time=datetime.utcnow())
451-
452-
try:
453-
chat_thread_client.add_participant(thread_participant=participant)
454-
except RuntimeError as e:
455-
if e is not None and decide_to_retry(error=e):
456-
chat_thread_client.add_participant(thread_participant=participant)
457-
458-
```
459413
### Add thread participants
460414

461415
Use `add_participants` method to add thread participants to the thread.
@@ -466,7 +420,7 @@ Use `add_participants` method to add thread participants to the thread.
466420
- `display_name`, optional, is the display name for the thread participant.
467421
- `share_history_time`, optional, time from which the chat history is shared with the participant.
468422

469-
A `list(tuple(ChatThreadParticipant, CommunicationError))` is returned. When participant is successfully added,
423+
A `list(tuple(ChatThreadParticipant, ChatError))` is returned. When participant is successfully added,
470424
an empty list is expected. In case of an error encountered while adding participant, the list is populated
471425
with the failed participants along with the error that was encountered.
472426

@@ -547,7 +501,8 @@ Use `send_read_receipt` method to post a read receipt event to a thread, on beha
547501
- Use `message_id` to specify the id of the message whose read receipt is to be sent
548502
```python
549503
content='hello world'
550-
send_message_result_id = chat_thread_client.send_message(content)
504+
send_message_result = chat_thread_client.send_message(content)
505+
send_message_result_id = send_message_result.id
551506
chat_thread_client.send_read_receipt(message_id=send_message_result_id)
552507
```
553508

sdk/communication/azure-communication-chat/azure/communication/chat/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,35 @@
33
from ._chat_thread_client import ChatThreadClient
44
from ._generated.models import (
55
SendChatMessageResult,
6-
ChatThreadInfo,
6+
ChatThreadItem,
77
ChatMessageType,
8-
CommunicationError
8+
ChatError
99
)
1010

1111
from ._models import (
1212
ChatThreadParticipant,
1313
ChatMessage,
14-
ChatThread,
14+
ChatThreadProperties,
1515
ChatMessageReadReceipt,
1616
ChatMessageContent,
1717
CreateChatThreadResult
1818
)
1919

20+
from ._shared.user_credential import CommunicationTokenCredential
21+
2022
__all__ = [
2123
'ChatClient',
2224
'ChatThreadClient',
2325
'ChatMessage',
2426
'ChatMessageContent',
2527
'ChatMessageReadReceipt',
2628
'SendChatMessageResult',
27-
'ChatThread',
28-
'ChatThreadInfo',
29+
'ChatThreadProperties',
30+
'ChatThreadItem',
2931
'ChatThreadParticipant',
3032
'ChatMessageType',
3133
'CreateChatThreadResult',
32-
'CommunicationError'
34+
'ChatError',
35+
'CommunicationTokenCredential'
3336
]
3437
__version__ = VERSION

0 commit comments

Comments
 (0)