Skip to content

Commit d855de3

Browse files
geekchickdragonmantank
authored andcommitted
Added mute/muteall functionality
1 parent 2267c3c commit d855de3

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed

opentok/endpoints.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Endpoints(object):
77
Class that provides the endpoint urls
88
"""
99

10+
1011
def __init__(self, api_url, api_key):
1112
self.api_url = api_url
1213
self.api_key = api_key
@@ -130,3 +131,15 @@ def get_broadcast_url(self, broadcast_id=None, stop=False, layout=False):
130131
if layout:
131132
url = url + "/layout"
132133
return url
134+
135+
def get_mute_all_url(self, session_id):
136+
""" this method returns the urls for muting every stream in a session """
137+
url = (
138+
self.api_url
139+
+ "/v2/project/"
140+
+ self.api_key
141+
+ "/session/"
142+
+ session_id
143+
+ "/mute"
144+
145+
)

opentok/opentok.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import random # _create_jwt_auth_header
1515
import logging # logging
1616
import warnings # Native. Used for notifying deprecations
17+
import os
1718

1819

1920
# compat
@@ -81,8 +82,6 @@ class ArchiveModes(Enum):
8182

8283
logger = logging.getLogger("opentok")
8384

84-
85-
8685
class Client(object):
8786

8887
"""Use this SDK to create tokens and interface with the server-side portion
@@ -402,6 +401,7 @@ def create_session(
402401
proxies=self.proxies,
403402
timeout=self.timeout,
404403
)
404+
405405
response.encoding = "utf-8"
406406

407407
if response.status_code == 403:
@@ -1343,6 +1343,8 @@ def _create_jwt_auth_header(self):
13431343
}
13441344

13451345
return jwt.encode(payload, self.api_secret, algorithm="HS256")
1346+
1347+
13461348

13471349
class OpenTok(Client):
13481350
def __init__(
@@ -1365,3 +1367,45 @@ def __init__(
13651367
timeout=timeout,
13661368
app_version=app_version
13671369
)
1370+
1371+
1372+
1373+
1374+
def mute(self, session_id: str, stream_id: str= "", options: dict = {}) -> requests.Response:
1375+
"""
1376+
Use this method so the moderator can mute all streams or a specific stream for OpenTok.
1377+
Please note that a client is able to unmute themselves.
1378+
This function stays in the OpenTok class and inherits from the Client class.
1379+
1380+
:param session_id gets the session id from another function called get_session()
1381+
1382+
:param stream_id gets the stream id from another function called get_stream(). Note
1383+
that this variable is set to an empty string in the function definition as a specific
1384+
stream may not be chosen.
1385+
1386+
"""
1387+
1388+
try:
1389+
if not stream_id:
1390+
url = self.endpoints.get_mute_all_url(session_id)
1391+
data = {'excludedStream': stream_id}
1392+
else:
1393+
url = self.endpoints.get_stream_url(session_id, stream_id) + "/mute"
1394+
data = None
1395+
1396+
1397+
response = requests.post(url, headers=self.get_headers(), data=data)
1398+
1399+
if response:
1400+
return response
1401+
elif response.status_code == 400:
1402+
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.")
1403+
elif response.status_code == 403:
1404+
raise AuthError("Failed to create session, invalid credentials")
1405+
elif response.status_code == 404:
1406+
raise NotFoundError("Mute not found")
1407+
except Exception as e:
1408+
raise OpenTokException(
1409+
("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(
1410+
session_id, stream_id))
1411+

test_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ httpretty
33
expects
44
wheel
55
twine
6-
bump2version
6+
bump2version
7+
sure

tests/test_opentok.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import unittest
2+
from requests.models import Response
3+
4+
from six import text_type, u, b, PY2, PY3
5+
from nose.tools import raises
6+
7+
import requests
8+
import httpretty
9+
from sure import expect
10+
import random
11+
import string
12+
13+
import json
14+
15+
import opentok
16+
from opentok import Client, OpenTok
17+
18+
19+
class OpenTokTest(unittest.TestCase):
20+
def setUp(self):
21+
self.api_key = "123456"
22+
self.api_secret = "1234567890abcdef1234567890abcdef1234567890"
23+
self.session_id = "SESSIONID"
24+
self.opentok = Client(self.api_key, self.api_secret)
25+
token = string.ascii_letters+string.digits
26+
self.jwt_token_string = ''.join(random.choice(token[:100]))
27+
self.stream_id_1 = "Stream1"
28+
29+
@httpretty.activate
30+
def test_mute_all_response(self):
31+
self.url = "https://api.opentok.com/v2/project/{0}/session/{1}/mute".format(
32+
self.api_key,
33+
self.session_id)
34+
35+
httpretty.register_uri(httpretty.POST,
36+
self.url,
37+
responses=[
38+
httpretty.Response(body="Testing text matches inside of the JSON file",
39+
content_type="application/json",
40+
adding_headers= {"x-opentok-auth": self.jwt_token_string},
41+
status=201)
42+
])
43+
44+
45+
response = requests.post(self.url)
46+
47+
response.status_code.should.equal(201)
48+
response.text.should.equal("Testing text matches inside of the JSON file")
49+
response.headers["x-opentok-auth"].should.equal(self.jwt_token_string)
50+
response.headers["Content-Type"].should.equal("application/json")
51+
52+
@httpretty.activate
53+
def test_mute_stream_response(self):
54+
self.url = "https://api.opentok.com/v2/project/${0}/session/${1}/stream/${2}/mute".format(
55+
self.api_key,
56+
self.session_id,
57+
self.stream_id_1)
58+
59+
httpretty.register_uri(httpretty.POST,
60+
self.url,
61+
responses=[
62+
httpretty.Response(body="Testing body of the JSON file",
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.status_code.should.equal(201)
72+
response.text.should.equal("Testing body of the JSON file")
73+
response.headers["x-opentok-auth"].should.equal(self.jwt_token_string)
74+
response.headers["Content-Type"].should.equal("application/json")
75+

0 commit comments

Comments
 (0)