Skip to content

Commit bedeeaf

Browse files
committed
Changed Force Mute function
- Split the ForceMute into two functions: mute_all and mute_stream - added return statements for the urls in the endpoints.py file - added a new test for the single stream
1 parent 3fe1c8c commit bedeeaf

File tree

3 files changed

+83
-38
lines changed

3 files changed

+83
-38
lines changed

opentok/endpoints.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def get_broadcast_url(self, broadcast_id=None, stop=False, layout=False):
130130
url = url + "/stop"
131131
if layout:
132132
url = url + "/layout"
133+
133134
return url
134135

135136
def get_mute_all_url(self, session_id):
@@ -144,6 +145,8 @@ def get_mute_all_url(self, session_id):
144145

145146
)
146147

148+
return url
149+
147150
def get_dtmf_all_url(self, session_id):
148151
""" this method returns the url for Play DTMF to all clients in the session """
149152
url = (
@@ -155,6 +158,8 @@ def get_dtmf_all_url(self, session_id):
155158
+ "/play-dtmf"
156159
)
157160

161+
return url
162+
158163
def get_dtmf_specific_url(self, session_id, connection_id):
159164
""" this method returns the url for Play DTMF to a specific client connection"""
160165
url = (
@@ -168,6 +173,8 @@ def get_dtmf_specific_url(self, session_id, connection_id):
168173
+ "/play-dtmf"
169174
)
170175

176+
return url
177+
171178
def get_archive_stream(self, archive_id=None):
172179
""" this method returns urls for working with streamModes in archives """
173180
url = (
@@ -179,6 +186,8 @@ def get_archive_stream(self, archive_id=None):
179186
+ "/streams"
180187
)
181188

189+
return url
190+
182191
def get_broadcast_stream(self, broadcast_id=None):
183192
""" this method returns urls for working with streamModes in broadcasts """
184193
url = (
@@ -189,3 +198,5 @@ def get_broadcast_stream(self, broadcast_id=None):
189198
+ broadcast_id
190199
+ "/streams"
191200
)
201+
202+
return url

opentok/opentok.py

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,47 +1641,43 @@ def __init__(
16411641
app_version=app_version
16421642
)
16431643

1644+
def mute_all(self,
1645+
session_id: str,
1646+
excludedStreamIds: Optional[List[str]],
1647+
active: bool= True,
1648+
options: dict = {}) -> requests.Response:
16441649

1645-
1646-
def mute(self,
1647-
session_id: str,
1648-
excludedStreamIds: Optional[List[str]],
1649-
stream_id: str= "",
1650-
active: bool= True,
1651-
data: dict = {}) -> requests.Response:
16521650
"""
1653-
Use this method so the moderator can mute all streams or a specific stream for OpenTok.
1654-
Please note that a client is able to unmute themselves.
1651+
Use this method so the moderator can mute all streams for OpenTok.
1652+
1653+
A moderator can exclude streams Id's from being muted. Please note that a
1654+
client is able to unmute themselves.
1655+
16551656
This function stays in the OpenTok class and inherits from the Client class.
16561657
1657-
:param session_id gets the session id from another function called get_session()
1658+
:param session_id gets the session id
16581659
1659-
:param excludedStreamIds is a list of The stream IDs for streams that should not be muted.
1660+
:param excludedStreamIds is a list of the stream IDs for streams that should not be muted.
16601661
This is an optional property. If you omit this property, all streams in the session will be muted.
16611662
1662-
:param stream_id gets the stream id from another function called get_stream(). Note
1663-
that this variable is set to an empty string in the function definition as a specific
1664-
stream may not be chosen.
1665-
16661663
:param active is a required boolean that determines whether streams published after the
16671664
call, in addition to the current streams in the session, should be muted (True) or not (False).
1668-
1665+
1666+
:param options is an empty dictonary representing the payload of the request,
1667+
whose contents will get populated inside the function
16691668
"""
1670-
1669+
1670+
url = self.endpoints.get_mute_all_url(session_id)
1671+
16711672
try:
1672-
if not stream_id:
1673-
url = self.endpoints.get_mute_all_url(session_id)
1673+
if excludedStreamIds:
16741674
if active:
1675-
data = {'active': active, 'excludedStreams': excludedStreamIds }
1676-
else:
1677-
active = False
1678-
data = {'active': active, 'excludedStreams': []}
1675+
options = {'active': active, 'excludedStreams': excludedStreamIds }
16791676
else:
1680-
url = self.endpoints.get_stream_url(session_id, stream_id) + "/mute"
1681-
data = None
1682-
1683-
1684-
response = requests.post(url, headers=self.get_headers(), data=json.dumps(data))
1677+
active = False
1678+
options = {'active': active, 'excludedStreams': []}
1679+
1680+
response = requests.post(url, headers=self.get_headers(), data=json.dumps(options))
16851681

16861682
if response:
16871683
return response
@@ -1693,9 +1689,44 @@ def mute(self,
16931689
raise NotFoundError("Mute not found")
16941690
except Exception as e:
16951691
raise OpenTokException(
1696-
("There was an error thrown by the OpenTok SDK, please check that your session_id {0} and stream_id (if exists) {1} are valid").format(
1692+
("There was an error thrown by the OpenTok SDK, please check that your session_id {0} and excludedStreamIds (if exists) {1} are valid").format(
1693+
session_id, excludedStreamIds))
1694+
1695+
1696+
1697+
def mute_stream(self, session_id: str, stream_id: str, options: dict = {}) -> requests.Response:
1698+
"""
1699+
Use this method so the moderator can mute a single stream for OpenTok.
1700+
This function stays in the OpenTok class and inherits from the Client class.
1701+
1702+
:param session_id gets the session id from another function called get_session()
1703+
1704+
:param stream_id gets a single stream id
1705+
1706+
:param options is an empty dictonary representing the payload of the request,
1707+
whose contents will get populated inside the function
1708+
"""
1709+
1710+
try:
1711+
if stream_id:
1712+
url = self.endpoints.get_stream_url(session_id, stream_id) + "/mute"
1713+
1714+
response = requests.post(url, headers=self.get_headers(), data=json.dumps(options))
1715+
1716+
if response:
1717+
return response
1718+
elif response.status_code == 400:
1719+
raise GetStreamError("Invalid request. This response may indicate that data in your request data is invalid JSON. Or it may indicate that you do not pass in a session ID or you passed in an invalid stream ID.")
1720+
elif response.status_code == 403:
1721+
raise AuthError("Failed to create session, invalid credentials")
1722+
elif response.status_code == 404:
1723+
raise NotFoundError("Mute not found")
1724+
except Exception as e:
1725+
raise OpenTokException(
1726+
("There was an error thrown by the OpenTok SDK, please check that your session_id {0} and stream_id {1} are valid").format(
16971727
session_id, stream_id))
16981728

1729+
16991730
def play_dtmf(self, session_id: str, connection_id: str, digits: str, options: dict = {}) -> requests.Response:
17001731
"""
17011732
Plays a DTMF string into a session or to a specific connection

tests/test_opentok.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ def setUp(self):
2828
self.jwt_token_string = ''.join(random.choice(token[:100]))
2929
self.stream_id_1 = "Stream1"
3030

31+
32+
3133
@httpretty.activate
32-
def test_mute_all_response(self):
34+
def test_mute_all_exclude_streams(self):
3335
self.url = "https://api.opentok.com/v2/project/{0}/session/{1}/mute".format(
3436
self.api_key,
3537
self.session_id)
3638

3739
httpretty.register_uri(httpretty.POST,
3840
self.url,
3941
responses=[
40-
httpretty.Response(body="Testing text matches inside of the JSON file",
42+
httpretty.Response(body='{"active": True, "excludedStreams": "excludedStreamIds1"}',
4143
content_type="application/json",
4244
adding_headers= {"x-opentok-auth": self.jwt_token_string},
4345
status=201)
@@ -46,21 +48,21 @@ def test_mute_all_response(self):
4648

4749
response = requests.post(self.url)
4850

49-
response.status_code.should.equal(201)
50-
response.text.should.equal("Testing text matches inside of the JSON file")
51+
response.text.should.equal('{"active": True, "excludedStreams": "excludedStreamIds1"}')
5152
response.headers["x-opentok-auth"].should.equal(self.jwt_token_string)
5253
response.headers["Content-Type"].should.equal("application/json")
5354

5455
@httpretty.activate
55-
def test_mute_all_exclude_streams(self):
56-
self.url = "https://api.opentok.com/v2/project/{0}/session/{1}/mute".format(
56+
def test_mute_single_stream(self):
57+
self.url = "https://api.opentok.com/v2/project/{0}/session/{1}/stream/{2}/mute".format(
5758
self.api_key,
58-
self.session_id)
59+
self.session_id,
60+
self.stream_id_1)
5961

6062
httpretty.register_uri(httpretty.POST,
6163
self.url,
6264
responses=[
63-
httpretty.Response(body='{"active": True, "excludedStreams": "excludedStreamIds1"}',
65+
httpretty.Response(body='{"session_id": "12345", "stream_id": "abcde"}',
6466
content_type="application/json",
6567
adding_headers= {"x-opentok-auth": self.jwt_token_string},
6668
status=201)
@@ -69,10 +71,11 @@ def test_mute_all_exclude_streams(self):
6971

7072
response = requests.post(self.url)
7173

72-
response.text.should.equal('{"active": True, "excludedStreams": "excludedStreamIds1"}')
74+
response.text.should.equal('{"session_id": "12345", "stream_id": "abcde"}')
7375
response.headers["x-opentok-auth"].should.equal(self.jwt_token_string)
7476
response.headers["Content-Type"].should.equal("application/json")
7577

78+
7679
@httpretty.activate
7780
def test_mute_stream_response(self):
7881
self.url = "https://api.opentok.com/v2/project/${0}/session/${1}/stream/${2}/mute".format(

0 commit comments

Comments
 (0)