File: rooms_client_sample.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (218 lines) | stat: -rw-r--r-- 8,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# coding=utf-8
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import os
import sys

from datetime import datetime, timedelta
from azure.core.exceptions import HttpResponseError
from azure.communication.identity import CommunicationIdentityClient
from azure.communication.rooms import ParticipantRole, RoomsClient, RoomParticipant

sys.path.append("..")


class RoomsSample(object):

    def setUp(self):
        # [START auth_from_connection_string]
        self.connection_string = os.getenv("COMMUNICATION_CONNECTION_STRING_ROOMS")

        self.rooms_client = RoomsClient.from_connection_string(self.connection_string)
        # [END auth_from_connection_string]
        self.identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
        self.rooms = []
        self.participant_1 = RoomParticipant(
            communication_identifier=self.identity_client.create_user(), role=ParticipantRole.PRESENTER
        )
        self.participant_2 = RoomParticipant(
            communication_identifier=self.identity_client.create_user(), role=ParticipantRole.CONSUMER
        )

    def tearDown(self):
        self.delete_room_all_rooms()

    def create_single_room(self):

        valid_from = datetime.now()
        valid_until = valid_from + timedelta(weeks=4)
        participants = [self.participant_1]

        try:
            create_room_response = self.rooms_client.create_room(
                valid_from=valid_from, valid_until=valid_until, participants=participants
            )
            self.printRoom(response=create_room_response)

            # all created room to a list
            self.rooms.append(create_room_response.id)

        except HttpResponseError as ex:
            print(ex)

    def create_single_room_with_default_attributes(self):
        try:
            create_room_response = self.rooms_client.create_room()
            self.printRoom(response=create_room_response)
            # all created room to a list
            self.rooms.append(create_room_response.id)

        except HttpResponseError as ex:
            print(ex)

    # Starting in 1.1.0b1 release,create_room function also takes pstn_dial_out_enabled as parameter
    def create_room_with_pstn_attribute(self):

        valid_from = datetime.now()
        valid_until = valid_from + timedelta(weeks=4)
        participants = [self.participant_1]
        pstn_dial_out_enabled = True

        try:
            create_room_response = self.rooms_client.create_room(
                valid_from=valid_from,
                valid_until=valid_until,
                participants=participants,
                pstn_dial_out_enabled=pstn_dial_out_enabled,
            )
            self.printRoom(response=create_room_response)

            # all created room to a list
            self.rooms.append(create_room_response.id)

        except HttpResponseError as ex:
            print(ex)

    def update_single_room(self, room_id):
        # set attributes you want to change
        valid_from = datetime.now()
        valid_until = valid_from + timedelta(weeks=7)

        try:
            update_room_response = self.rooms_client.update_room(
                room_id=room_id, valid_from=valid_from, valid_until=valid_until
            )
            self.printRoom(response=update_room_response)
        except HttpResponseError as ex:
            print(ex)

    # Starting in 1.1.0b1 release,update_room function also takes pstn_dial_out_enabled as parameter
    def update_room_with_pstn_attribute(self, room_id):
        # set attributes you want to change
        valid_from = datetime.now()
        valid_until = valid_from + timedelta(weeks=7)
        pstn_dial_out_enabled = True

        try:
            update_room_response = self.rooms_client.update_room(
                room_id=room_id,
                valid_from=valid_from,
                valid_until=valid_until,
                pstn_dial_out_enabled=pstn_dial_out_enabled,
            )
            self.printRoom(response=update_room_response)
        except HttpResponseError as ex:
            print(ex)

    def add_or_update_participants(self, room_id):
        self.participant_1.role = ParticipantRole.ATTENDEE
        participants = [
            self.participant_1,  # Update participant_1 role from Presenter to Attendee
            self.participant_2,  # Add participant_2 to room
        ]

        try:
            self.rooms_client.add_or_update_participants(room_id=room_id, participants=participants)
        except HttpResponseError as ex:
            print(ex)

    # Starting in 1.2.0 release, we are introducing a new role called Collaborator
    def add_or_update_participants_to_collaborator(self, room_id):
        self.participant_1.role = ParticipantRole.COLLABORATOR
        participants = [
            self.participant_1,  # Update participant_1 role from Attendee to Collaborator
        ]

        try:
            self.rooms_client.add_or_update_participants(room_id=room_id, participants=participants)
        except HttpResponseError as ex:
            print(ex)

    def list_participants(self, room_id):
        try:
            get_participants_response = self.rooms_client.list_participants(room_id=room_id)
            print("participants: \n", self.convert_participant_list_to_string(get_participants_response))
        except HttpResponseError as ex:
            print(ex)

    def remove_participants(self, room_id):
        participants = [self.participant_1.communication_identifier]

        try:
            self.rooms_client.remove_participants(room_id=room_id, participants=participants)
        except HttpResponseError as ex:
            print(ex)

    def delete_room_all_rooms(self):
        for room in self.rooms:
            print("deleting: ", room)
            self.rooms_client.delete_room(room_id=room)

    def get_room(self, room_id):

        try:
            get_room_response = self.rooms_client.get_room(room_id=room_id)
            self.printRoom(response=get_room_response)

        except HttpResponseError as ex:
            print(ex)

    def printRoom(self, response):
        print("room_id: ", response.id)
        print("created_at: ", response.created_at)
        print("valid_from: ", response.valid_from)
        print("valid_until: ", response.valid_until)

    def convert_participant_list_to_string(self, participants):
        result = ""
        for p in participants:
            result += "id: {}\n role: {}\n".format(p.communication_identifier.properties["id"], p.role)
        return result


if __name__ == "__main__":
    sample = RoomsSample()
    sample.setUp()
    sample.create_single_room()
    sample.create_single_room_with_default_attributes()
    if len(sample.rooms) > 0:
        sample.get_room(room_id=sample.rooms[0])
        sample.update_single_room(room_id=sample.rooms[0])
        sample.add_or_update_participants(room_id=sample.rooms[0])
        sample.list_participants(room_id=sample.rooms[0])
        sample.remove_participants(room_id=sample.rooms[0])
        sample.get_room(room_id=sample.rooms[0])
    sample.tearDown()