Skip to content

Commit 3ff2d91

Browse files
Merge pull request #236211 from minnieliu/peiliu/updatePythonRoomsQuickStart
Update Python Rooms Quickstart according to new SDK
2 parents e3f16d9 + b44d24f commit 3ff2d91

File tree

2 files changed

+95
-72
lines changed

2 files changed

+95
-72
lines changed

articles/communication-services/quickstarts/rooms/includes/rooms-quickstart-net.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,4 @@ Deleted room with id: 99445276259151407
270270

271271
## Reference documentation
272272

273-
Read about the full set of capabilities of Azure Communication Services re [.NET SDK reference](/dotnet/api/azure.communication.rooms) or [REST API reference](/rest/api/communication/rooms).
273+
Read about the full set of capabilities of Azure Communication Services rooms from the [.NET SDK reference](/dotnet/api/azure.communication.rooms) or [REST API reference](/rest/api/communication/rooms).

articles/communication-services/quickstarts/rooms/includes/rooms-quickstart-python.md

Lines changed: 94 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
title: include file
33
description: include file
44
services: azure-communication-services
5-
author: radubulboaca
6-
manager: mariusu
5+
author: peiliu
6+
manager: alexokun
77

88
ms.service: azure-communication-services
99
ms.subservice: azure-communication-services
10-
ms.date: 01/26/2022
10+
ms.date: 04/27/2023
1111
ms.topic: include
1212
ms.custom: include file
13-
ms.author: radubulboaca
13+
ms.author: peiliu
1414
---
1515

1616
## Prerequisites
@@ -35,7 +35,7 @@ mkdir acs-rooms-quickstart && cd acs-rooms-quickstart
3535

3636
### Install the packages
3737

38-
You'll need to use the Azure Communication Rooms client library for Python [version 1.0.0b2](https://pypi.org/project/azure-communication-rooms/) or above.
38+
You'll need to use the Azure Communication Rooms client library for Python [version 1.0.0b3](https://pypi.org/project/azure-communication-rooms/) or above.
3939

4040
From a console prompt, navigate to the directory containing the rooms.py file, then execute the following command:
4141

@@ -55,11 +55,12 @@ from datetime import datetime, timedelta
5555
from azure.communication.rooms import (
5656
RoomsClient,
5757
RoomParticipant,
58-
RoleType,
59-
RoomJoinPolicy
58+
ParticipantRole
59+
)
60+
from azure.communication.identity import (
61+
CommunicationIdentityClient,
62+
CommunicationUserIdentifier
6063
)
61-
from azure.communication.identity import CommunicationUserIdentifier
62-
6364
class RoomsQuickstart(object):
6465
print("Azure Communication Services - Rooms Quickstart")
6566
#room method implementations goes here
@@ -74,96 +75,126 @@ Create a new `RoomsClient` object that will be used to create new `rooms` and ma
7475

7576
```python
7677
#Find your Communication Services resource in the Azure portal
77-
self.connection_string = os.getenv("COMMUNICATION_CONNECTION_STRING") or <COMMUNICATION_SAMPLES_CONNECTION_STRING>
78+
self.connection_string = '<connection_string>'
7879
self.rooms_client = RoomsClient.from_connection_string(self.connection_string)
7980
```
8081

8182
## Create a room
8283

83-
Create a new `room` with default properties using the code snippet below:
84+
Create a new `room` with default properties using the code snippet below. When defining participants, if a role is not specified, then it will be set to `Attendee` as default.
8485

8586
```python
86-
# Create a Room
87+
# Create a room
8788
valid_from = datetime.now()
8889
valid_until = valid_from + relativedelta(months=+1)
8990

9091
# Create identities for users
91-
identity_client = CommunicationIdentityClient.from_connection_string(connection_string)
92-
user1 = identity_client.create_user_and_token(scopes=[CommunicationTokenScope.VOIP])
93-
user2 = identity_client.create_user_and_token(scopes=["voip"])
92+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
93+
user1 = identity_client.create_user()
94+
user2 = identity_client.create_user()
95+
user3 = identity_client.create_user()
9496

95-
participants = []
96-
participants.append(RoomParticipant(CommunicationUserIdentifier(user1.properties['id'])))
97+
participant_1 = RoomParticipant(communication_identifier=self.user1, role=ParticipantRole.PRESENTER)
98+
participant_2 = RoomParticipant(communication_identifier=self.user2, role=ParticipantRole.CONSUMER)
99+
participants = [participant_1, participant_2]
97100

98101
try:
99-
create_room_response = self.rooms_client.create_room(valid_from=valid_from, valid_until=valid_until, participants=participants)
100-
print("\nCreated a room with id: " + create_room_response.id)
102+
create_room = self.rooms_client.create_room(
103+
valid_from=valid_from,
104+
valid_until=valid_until,
105+
participants=participants
106+
)
107+
print("\nCreated a room with id: " + create_room.id)
101108
except HttpResponseError as ex:
102109
print(ex)
103110
```
104111

105-
Since `rooms` are server-side entities, you may want to keep track of and persist the `roomId` in the storage medium of choice. You can reference the `roomId` to view or update the properties of a `room` object.
112+
Since `rooms` are server-side entities, you may want to keep track of and persist the `room.id` in the storage medium of choice. You can reference the `id` to view or update the properties of a `room` object.
106113

107114
## Get properties of an existing room
108115

109-
Retrieve the details of an existing `room` by referencing the `roomId`:
116+
Retrieve the details of an existing `room` by referencing the `id`:
110117

111118
```python
112119
# Retrieves the room with corresponding ID
120+
room_id = create_room.id
113121
try:
114-
get_room_response = self.rooms_client.get_room(room_id=room_id)
115-
print("\nRetrieved room with id: ", get_room_response.id);
116-
122+
get_room = self.rooms_client.get_room(room_id=room_id)
123+
print("\nRetrieved room with id: ", get_room.id)
117124
except HttpResponseError as ex:
118125
print(ex)
119126
```
120127

121128
## Update the lifetime of a room
122129

123-
The lifetime of a `room` can be modified by issuing an update request for the `ValidFrom` and `ValidUntil` parameters. A room can be valid for a maximum of six months.
130+
The lifetime of a `room` can be modified by issuing an update request for the `valid_from` and `valid_until` parameters. A room can be valid for a maximum of six months.
124131

125132
```python
126-
# set attributes you want to change
133+
# Update the lifetime of a room
127134
valid_from = datetime.now()
128135
valid_until = valid_from + relativedelta(months=+1,days=+20)
129136

130137
try:
131-
update_room_response = self.rooms_client.update_room(room_id=room_id, valid_from=valid_from, valid_until=valid_until)
132-
self.printRoom(response=update_room_response)
138+
updated_room = self.rooms_client.update_room(room_id=room_id, valid_from=valid_from, valid_until=valid_until)
139+
print("\nUpdated room with validFrom: " + updated_room.valid_from + " and validUntil: " + updated_room.valid_until)
133140
except HttpResponseError as ex:
134141
print(ex)
135-
```
142+
```
136143

137-
## Add new participants
144+
## List all active rooms
138145

139-
To add new participants to a `room`, use the `add_participants` method exposed on the client.
146+
To retrieve all active rooms created under your resource, use the `list_rooms` method exposed on the client.
140147

141148
```python
149+
# List all active rooms
150+
try:
151+
rooms = self.rooms_client.list_rooms()
152+
count = 0
153+
for room in rooms:
154+
if count == 1:
155+
break
156+
print("\nPrinting the first room in list"
157+
"\nRoom Id: " + room.id +
158+
"\nCreated date time: " + str(room.created_at) +
159+
"\nValid From: " + str(room.valid_from) + "\nValid Until: " + str(room.valid_until))
160+
count += 1
161+
except HttpResponseError as ex:
162+
print(ex)
163+
```
164+
165+
## Add or update participants
142166

143-
# Add Participants
167+
To add new participants or update existing participants in a `room`, use the `add_or_update_participants` method exposed on the client.
168+
169+
```python
170+
# Add or update participants in a room
144171
try:
172+
# Update existing user2 from consumer to attendee
145173
participants = []
146-
participants.append(RoomParticipant(CommunicationUserIdentifier(user2.properties['id']), RoleType.ATTENDEE))
147-
self.rooms_client.add_participants(room_id, participants)
148-
print("\nAdded participants to room")
174+
participants.append(RoomParticipant(self.user2, ParticipantRole.ATTENDEE))
149175

150-
except Exception as ex:
151-
print('Error in adding participants to room.', ex)
176+
# Add new participant user3
177+
participants.append(RoomParticipant(self.user3, ParticipantRole.CONSUMER))
178+
self.rooms_client.add_or_update_participants(room_id=room_id, participants=participants)
179+
print("\Add or update participants in room")
152180

181+
except Exception as ex:
182+
print('Error in adding or updating participants to room.', ex)
153183
```
154184

155185
Participants that have been added to a `room` become eligible to join calls.
156186

157-
## Get list of participants
187+
## List participants in a room
158188

159-
Retrieve the list of participants for an existing `room` by referencing the `roomId`:
189+
Retrieve the list of participants for an existing `room` by referencing the `room_id`:
160190

161191
```python
162-
163192
# Get list of participants in room
164193
try:
165-
participants = self.rooms_client.get_participants(room_id)
166-
print("\nRetrieved participants for room: ", participants)
194+
participants = self.rooms_client.list_participants(room_id)
195+
print('\nParticipants in Room Id :', room_id)
196+
for p in participants:
197+
print(p.communication_identifier.properties['id'], p.role)
167198
except HttpResponseError as ex:
168199
print(ex)
169200

@@ -174,40 +205,38 @@ except HttpResponseError as ex:
174205
To remove a participant from a `room` and revoke their access, use the `remove_participants` method.
175206

176207
```python
177-
178208
# Remove Participants
179-
participants = [CommunicationUserIdentifier(user2.properties['id'])]
180-
181209
try:
182-
remove_participants_response = self.rooms_client.remove_participants(room_id=room_id, communication_identifiers=participants)
210+
participants = [user2]
211+
self.rooms_client.remove_participants(room_id=room_id, participants=participants)
183212
print("\nRemoved participants from room")
213+
184214
except HttpResponseError as ex:
185215
print(ex)
186216

187217
```
188218

189219
## Delete room
190-
If you wish to disband an existing `room`, you may issue an explicit delete request. All `rooms` and their associated resources are automatically deleted at the end of their validity plus a grace period.
220+
If you wish to disband an existing `room`, you may issue an explicit delete request. All `rooms` and their associated resources are automatically deleted at the end of their validity plus a grace period.
191221

192222
```python
193-
194223
# Delete Room
224+
195225
self.rooms_client.delete_room(room_id=room)
196226
print("\nDeleted room with id: " + room)
197227

198228
```
199229

200230
## Run the code
201231

202-
To run the code, make sure you are on the directory where your `index.js` file is.
232+
To run the code, make sure you are on the directory where your `rooms-quickstart.py` file is.
203233

204234
```console
205235

206236
python rooms-quickstart.py
207237

208238
```
209239

210-
211240
The expected output describes each completed action:
212241

213242
```console
@@ -218,30 +247,24 @@ Created a room with id: 99445276259151407
218247

219248
Retrieved room with id: 99445276259151407
220249

221-
Updated room with validFrom: 2023-05-11T22:11:46.784Z and validUntil: 2023-05-11T22:16:46.784Z
222-
223-
Added participants to room
224-
225-
Retrieved participants for room: [
226-
{
227-
id: {
228-
kind: 'communicationUser',
229-
communicationUserId: '8:acs:b6aada1f-0b1d-47ac-866f-91aae00a1d01_00000018-ac89-7c76-35f3-343a0d00e901'
230-
},
231-
role: 'Attendee'
232-
},
233-
{
234-
id: {
235-
kind: 'communicationUser',
236-
communicationUserId: '8:acs:b6aada1f-0b1d-47ac-866f-91aae00a1d01_00000018-ac89-7ccc-35f3-343a0d00e902'
237-
},
238-
role: 'Consumer'
239-
}
240-
]
250+
Updated room with validFrom: 2023-05-03T00:00:00+00:00 and validUntil: 2023-06-23T00:00:00+00:00
251+
252+
Printing the first room in list
253+
Room Id: 99445276259151407
254+
Created date time: 2023-05-03T00:00:00+00:00
255+
Valid From: 2023-05-03T00:00:00+00:00
256+
Valid Until: 2023-06-23T00:00:00+00:00
257+
258+
Add or update participants in room
259+
260+
Participants in Room Id : 99445276259151407
261+
8:acs:42a0ff0c-356d-4487-a288-ad0aad95d504_00000018-ef00-6042-a166-563a0d0051c1 Presenter
262+
8:acs:42a0ff0c-356d-4487-a288-ad0aad95d504_00000018-ef00-6136-a166-563a0d0051c2 Consumer
263+
8:acs:42a0ff0c-356d-4487-a288-ad0aad95d504_00000018-ef00-61fd-a166-563a0d0051c3 Attendee
241264

242265
Removed participants from room
243266

244-
Deleted room with id: 99445276259151407
267+
Deleted room with id: 99445276259151407
245268

246269
```
247270

0 commit comments

Comments
 (0)