Skip to content

Commit d7de18e

Browse files
committed
Made changes to the mute function and added a test
- Updated the mute functionality with the active flag, excludedStream and excludedStreamIds - Added a new test in test_opentok.py
1 parent efa7c2e commit d7de18e

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

opentok/opentok.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime # generate_token
2+
from typing import List, Optional # imports List, Optional type hint
23
import calendar # generate_token
34
import base64 # generate_token
45
import random # generate_token
@@ -1423,30 +1424,45 @@ def __init__(
14231424

14241425

14251426

1426-
def mute(self, session_id: str, stream_id: str= "", options: dict = {}) -> requests.Response:
1427+
def mute(self,
1428+
session_id: str,
1429+
excludedStreamIds: Optional[List[str]],
1430+
stream_id: str= "",
1431+
active: bool= True,
1432+
data: dict = {}) -> requests.Response:
14271433
"""
14281434
Use this method so the moderator can mute all streams or a specific stream for OpenTok.
14291435
Please note that a client is able to unmute themselves.
14301436
This function stays in the OpenTok class and inherits from the Client class.
14311437
14321438
:param session_id gets the session id from another function called get_session()
14331439
1440+
:param excludedStreamIds is a list of The stream IDs for streams that should not be muted.
1441+
This is an optional property. If you omit this property, all streams in the session will be muted.
1442+
14341443
:param stream_id gets the stream id from another function called get_stream(). Note
14351444
that this variable is set to an empty string in the function definition as a specific
14361445
stream may not be chosen.
1446+
1447+
:param active is a required boolean that determines whether streams published after the
1448+
call, in addition to the current streams in the session, should be muted (True) or not (False).
14371449
14381450
"""
1439-
1451+
14401452
try:
14411453
if not stream_id:
14421454
url = self.endpoints.get_mute_all_url(session_id)
1443-
data = {'excludedStream': stream_id}
1455+
if active:
1456+
data = {'active': active, 'excludedStreams': excludedStreamIds }
1457+
else:
1458+
active = False
1459+
data = {'active': active, 'excludedStreams': []}
14441460
else:
14451461
url = self.endpoints.get_stream_url(session_id, stream_id) + "/mute"
14461462
data = None
14471463

14481464

1449-
response = requests.post(url, headers=self.get_headers(), data=data)
1465+
response = requests.post(url, headers=self.get_headers(), data=json.dumps(data))
14501466

14511467
if response:
14521468
return response

tests/test_opentok.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ def test_mute_all_response(self):
5050
response.headers["x-opentok-auth"].should.equal(self.jwt_token_string)
5151
response.headers["Content-Type"].should.equal("application/json")
5252

53+
@httpretty.activate
54+
def test_mute_all_exclude_streams(self):
55+
self.url = "https://api.opentok.com/v2/project/{0}/session/{1}/mute".format(
56+
self.api_key,
57+
self.session_id)
58+
59+
httpretty.register_uri(httpretty.POST,
60+
self.url,
61+
responses=[
62+
httpretty.Response(body='{"active": True, "excludedStreams": "excludedStreamIds1"}',
63+
content_type="application/json",
64+
adding_headers= {"x-opentok-auth": self.jwt_token_string},
65+
status=201)
66+
])
67+
68+
69+
response = requests.post(self.url)
70+
71+
response.text.should.equal('{"active": True, "excludedStreams": "excludedStreamIds1"}')
72+
response.headers["x-opentok-auth"].should.equal(self.jwt_token_string)
73+
response.headers["Content-Type"].should.equal("application/json")
74+
5375
@httpretty.activate
5476
def test_mute_stream_response(self):
5577
self.url = "https://api.opentok.com/v2/project/${0}/session/${1}/stream/${2}/mute".format(

0 commit comments

Comments
 (0)