Skip to content

Commit bda8759

Browse files
committed
fix broadcast streams methods and tests, fix formatting
1 parent 3c3d354 commit bda8759

File tree

4 files changed

+176
-203
lines changed

4 files changed

+176
-203
lines changed

opentok/endpoints.py

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

10-
1110
def __init__(self, api_url, api_key):
1211
self.api_url = api_url
1312
self.api_key = api_key
@@ -57,7 +56,7 @@ def signaling_url(self, session_id, connection_id=None):
5756
return self.get_signaling_url(session_id, connection_id)
5857

5958
def get_stream_url(self, session_id, stream_id=None):
60-
""" this method returns the url to get streams information """
59+
"""this method returns the url to get streams information"""
6160
url = (
6261
self.api_url
6362
+ "/v2/project/"
@@ -79,7 +78,7 @@ def broadcast_url(self, broadcast_id=None, stop=False, layout=False):
7978
return self.get_broadcast_url(broadcast_id, stop, layout)
8079

8180
def force_disconnect_url(self, session_id, connection_id):
82-
""" this method returns the force disconnect url endpoint """
81+
"""this method returns the force disconnect url endpoint"""
8382
url = (
8483
self.api_url
8584
+ "/v2/project/"
@@ -92,7 +91,7 @@ def force_disconnect_url(self, session_id, connection_id):
9291
return url
9392

9493
def set_archive_layout_url(self, archive_id):
95-
""" this method returns the url to set the archive layout """
94+
"""this method returns the url to set the archive layout"""
9695
url = (
9796
self.api_url
9897
+ "/v2/project/"
@@ -104,12 +103,12 @@ def set_archive_layout_url(self, archive_id):
104103
return url
105104

106105
def dial_url(self):
107-
""" this method returns the url to initialize a SIP call """
106+
"""this method returns the url to initialize a SIP call"""
108107
url = self.api_url + "/v2/project/" + self.api_key + "/dial"
109108
return url
110109

111110
def set_stream_class_lists_url(self, session_id):
112-
""" this method returns the url to set the stream class list """
111+
"""this method returns the url to set the stream class list"""
113112
url = (
114113
self.api_url
115114
+ "/v2/project/"
@@ -121,7 +120,7 @@ def set_stream_class_lists_url(self, session_id):
121120
return url
122121

123122
def get_broadcast_url(self, broadcast_id=None, stop=False, layout=False):
124-
""" this method returns urls for working with broadcast """
123+
"""this method returns urls for working with broadcast"""
125124
url = self.api_url + "/v2/project/" + self.api_key + "/broadcast"
126125

127126
if broadcast_id:
@@ -130,28 +129,27 @@ def get_broadcast_url(self, broadcast_id=None, stop=False, layout=False):
130129
url = url + "/stop"
131130
if layout:
132131
url = url + "/layout"
133-
132+
134133
return url
135134

136135
def get_mute_all_url(self, session_id):
137-
""" this method returns the urls for muting every stream in a session """
136+
"""this method returns the urls for muting every stream in a session"""
138137
url = (
139-
self.api_url
140-
+ "/v2/project/"
138+
self.api_url
139+
+ "/v2/project/"
141140
+ self.api_key
142141
+ "/session/"
143142
+ session_id
144143
+ "/mute"
145-
146144
)
147145

148146
return url
149147

150148
def get_dtmf_all_url(self, session_id):
151-
""" this method returns the url for Play DTMF to all clients in the session """
149+
"""this method returns the url for Play DTMF to all clients in the session"""
152150
url = (
153151
self.api_url
154-
+ "/v2/project/"
152+
+ "/v2/project/"
155153
+ self.api_key
156154
+ "/session/"
157155
+ session_id
@@ -161,7 +159,7 @@ def get_dtmf_all_url(self, session_id):
161159
return url
162160

163161
def get_dtmf_specific_url(self, session_id, connection_id):
164-
""" this method returns the url for Play DTMF to a specific client connection"""
162+
"""this method returns the url for Play DTMF to a specific client connection"""
165163
url = (
166164
self.api_url
167165
+ "/v2/project/"
@@ -176,7 +174,7 @@ def get_dtmf_specific_url(self, session_id, connection_id):
176174
return url
177175

178176
def get_archive_stream(self, archive_id=None):
179-
""" this method returns urls for working with streamModes in archives """
177+
"""this method returns urls for working with streamModes in archives"""
180178
url = (
181179
self.api_url
182180
+ "/v2/project/"
@@ -189,10 +187,10 @@ def get_archive_stream(self, archive_id=None):
189187
return url
190188

191189
def get_broadcast_stream(self, broadcast_id=None):
192-
""" this method returns urls for working with streamModes in broadcasts """
190+
"""this method returns urls for working with streamModes in broadcasts"""
193191
url = (
194192
self.api_url
195-
+ "/v2/partner/"
193+
+ "/v2/project/"
196194
+ self.api_key
197195
+ "/broadcast/"
198196
+ broadcast_id
@@ -202,7 +200,7 @@ def get_broadcast_stream(self, broadcast_id=None):
202200
return url
203201

204202
def get_render_url(self, render_id: str = None):
205-
"Returns URLs for working with the Render API."""
203+
"Returns URLs for working with the Render API." ""
206204
url = self.api_url + "/v2/project/" + self.api_key + "/render"
207205
if render_id:
208206
url += "/" + render_id
@@ -212,5 +210,5 @@ def get_render_url(self, render_id: str = None):
212210
def get_audio_connector_url(self):
213211
"""Returns URLs for working with the Audio Connector API."""
214212
url = self.api_url + "/v2/project/" + self.api_key + "/connect"
215-
213+
216214
return url

opentok/opentok.py

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def add_archive_stream(
886886
elif response.status_code == 409:
887887
raise ArchiveError(response.json().get("message"))
888888
else:
889-
raise RequestError("An unexpected error occurred", response.status_code)
889+
raise RequestError("An unexpected error occurred.", response.status_code)
890890

891891
def remove_archive_stream(
892892
self, archive_id: str, stream_id: str
@@ -933,7 +933,7 @@ def remove_archive_stream(
933933
elif response.status_code == 409:
934934
raise ArchiveError(response.json().get("message"))
935935
else:
936-
raise RequestError("An unexpected error occurred", response.status_code)
936+
raise RequestError("An unexpected error occurred.", response.status_code)
937937

938938
def send_signal(self, session_id, payload, connection_id=None):
939939
"""
@@ -966,26 +966,27 @@ def send_signal(self, session_id, payload, connection_id=None):
966966
timeout=self.timeout,
967967
)
968968

969-
if response.status_code == 204:
970-
pass
971-
elif response.status_code == 400:
972-
raise SignalingError(
973-
"One of the signal properties - data, type, sessionId or connectionId - is invalid."
974-
)
975-
elif response.status_code == 403:
976-
raise AuthError(
977-
"You are not authorized to send the signal. Check your authentication credentials."
978-
)
979-
elif response.status_code == 404:
980-
raise SignalingError(
981-
"The client specified by the connectionId property is not connected to the session."
982-
)
983-
elif response.status_code == 413:
984-
raise SignalingError(
985-
"The type string exceeds the maximum length (128 bytes), or the data string exceeds the maximum size (8 kB)."
986-
)
969+
if response:
970+
if response.status_code == 204:
971+
return None
972+
elif response.status_code == 400:
973+
raise SignalingError(
974+
"One of the signal properties - data, type, sessionId or connectionId - is invalid."
975+
)
976+
elif response.status_code == 403:
977+
raise AuthError(
978+
"You are not authorized to send the signal. Check your authentication credentials."
979+
)
980+
elif response.status_code == 404:
981+
raise SignalingError(
982+
"The client specified by the connectionId property is not connected to the session."
983+
)
984+
elif response.status_code == 413:
985+
raise SignalingError(
986+
"The type string exceeds the maximum length (128 bytes), or the data string exceeds the maximum size (8 kB)."
987+
)
987988
else:
988-
raise RequestError("An unexpected error occurred", response.status_code)
989+
raise RequestError("An unexpected error occurred.", response.status_code)
989990

990991
def signal(self, session_id, payload, connection_id=None):
991992
warnings.warn(
@@ -1561,24 +1562,25 @@ def add_broadcast_stream(
15611562
)
15621563

15631564
if response:
1564-
return Broadcast(response.json())
1565-
elif response.status_code == 400:
1566-
raise BroadcastError(
1567-
"Invalid request. This response may indicate that data in your request data is "
1568-
"invalid JSON. It may also indicate that you passed in invalid layout options. "
1569-
"Or you have exceeded the limit of five simultaneous RTMP streams for an OpenTok "
1570-
"session. Or you specified and invalid resolution."
1571-
)
1572-
elif response.status_code == 403:
1573-
raise AuthError("Authentication error.")
1574-
elif response.status_code == 405:
1575-
raise BroadcastStreamModeError(
1576-
"Your broadcast is configured with a streamMode that does not support stream manipulation."
1577-
)
1578-
elif response.status_code == 409:
1579-
raise BroadcastError("The broadcast has already started for the session.")
1565+
if response.status_code == 204:
1566+
return None
1567+
elif response.status_code == 400:
1568+
raise BroadcastError(
1569+
"Invalid request. This response may indicate that data in your request data is "
1570+
"invalid JSON. It may also indicate that you passed in invalid layout options. "
1571+
"Or you have exceeded the limit of five simultaneous RTMP streams for an OpenTok "
1572+
"session. Or you specified and invalid resolution."
1573+
)
1574+
elif response.status_code == 403:
1575+
raise AuthError("Authentication error.")
1576+
elif response.status_code == 405:
1577+
raise BroadcastStreamModeError(
1578+
"Your broadcast is configured with a streamMode that does not support stream manipulation."
1579+
)
1580+
elif response.status_code == 409:
1581+
raise BroadcastError("The broadcast has already started for the session.")
15801582
else:
1581-
raise RequestError("OpenTok server error.", response.status_code)
1583+
raise RequestError("An unexpected error occurred.", response.status_code)
15821584

15831585
def remove_broadcast_stream(
15841586
self, broadcast_id: str, stream_id: str
@@ -1603,22 +1605,23 @@ def remove_broadcast_stream(
16031605
)
16041606

16051607
if response:
1606-
return Broadcast(response.json())
1607-
elif response.status_code == 400:
1608-
raise BroadcastError(
1609-
"Invalid request. This response may indicate that data in your request data is "
1610-
"invalid JSON. It may also indicate that you passed in invalid layout options. "
1611-
"Or you have exceeded the limit of five simultaneous RTMP streams for an OpenTok "
1612-
"session. Or you specified and invalid resolution."
1613-
)
1614-
elif response.status_code == 403:
1615-
raise AuthError("Authentication error.")
1616-
elif response.status_code == 405:
1617-
raise BroadcastStreamModeError(
1618-
"Your broadcast is configured with a streamMode that does not support stream manipulation."
1619-
)
1620-
elif response.status_code == 409:
1621-
raise BroadcastError("The broadcast has already started for the session.")
1608+
if response.status_code == 204:
1609+
return None
1610+
elif response.status_code == 400:
1611+
raise BroadcastError(
1612+
"Invalid request. This response may indicate that data in your request data is "
1613+
"invalid JSON. It may also indicate that you passed in invalid layout options. "
1614+
"Or you have exceeded the limit of five simultaneous RTMP streams for an OpenTok "
1615+
"session. Or you specified and invalid resolution."
1616+
)
1617+
elif response.status_code == 403:
1618+
raise AuthError("Authentication error.")
1619+
elif response.status_code == 405:
1620+
raise BroadcastStreamModeError(
1621+
"Your broadcast is configured with a streamMode that does not support stream manipulation."
1622+
)
1623+
elif response.status_code == 409:
1624+
raise BroadcastError("The broadcast has already started for the session.")
16221625
else:
16231626
raise RequestError("OpenTok server error.", response.status_code)
16241627

tests/test_archive.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
import httpretty
55
from sure import expect
66
import textwrap
7-
import json
87
import datetime
9-
import requests
108
import pytz
119
from .validate_jwt import validate_jwt_header
1210

13-
from opentok import Client, Archive, __version__, OutputModes, StreamModes
11+
from opentok import Client, Archive, __version__, OutputModes
1412

1513

1614
class OpenTokArchiveTest(unittest.TestCase):
@@ -105,18 +103,26 @@ def test_stop_archive(self):
105103
def test_add_archive_stream(self):
106104
archive_id = u("ARCHIVEID")
107105
url = f"https://api.opentok.com/v2/project/{self.api_key}/archive/{archive_id}/streams"
108-
httpretty.register_uri(httpretty.PATCH, url, responses=[httpretty.Response(body=u(""), status=204)])
109-
110-
response = self.opentok.add_archive_stream(archive_id=archive_id, stream_id=self.stream1)
106+
httpretty.register_uri(
107+
httpretty.PATCH, url, responses=[httpretty.Response(body=u(""), status=204)]
108+
)
109+
110+
response = self.opentok.add_archive_stream(
111+
archive_id=archive_id, stream_id=self.stream1
112+
)
111113
assert response == None
112114

113115
@httpretty.activate
114116
def test_remove_archive_stream(self):
115117
archive_id = u("ARCHIVEID")
116118
url = f"https://api.opentok.com/v2/project/{self.api_key}/archive/{archive_id}/streams"
117-
httpretty.register_uri(httpretty.PATCH, url, responses=[httpretty.Response(body=u(""), status=204)])
118-
119-
response = self.opentok.remove_archive_stream(archive_id=archive_id, stream_id=self.stream1)
119+
httpretty.register_uri(
120+
httpretty.PATCH, url, responses=[httpretty.Response(body=u(""), status=204)]
121+
)
122+
123+
response = self.opentok.remove_archive_stream(
124+
archive_id=archive_id, stream_id=self.stream1
125+
)
120126
assert response == None
121127

122128
@httpretty.activate

0 commit comments

Comments
 (0)