Skip to content

Commit a94cfdd

Browse files
authored
Merge pull request #222 from opentok/auto-archive-improvements
add auto archive improvements
2 parents c6f2588 + 6ba27eb commit a94cfdd

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

README.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ keyword parameters for this method:
6262
* ``archive_mode`` which specifies whether the session will be automatically archived (``always``)
6363
or not (``manual``).
6464

65+
* ``archive_name`` which indicates the archive name for all the archives in auto archived session.
66+
A session that begins with archive mode 'always' will be using this archive name for all archives of that session.
67+
Passing 'archive_name' with archive mode 'manual' will cause an error response.
68+
69+
* ``archive_resolution`` which indicates the archive resolution for all the archives in auto archived session.
70+
Valid values are '640x480', '480x640', '1280x720', '720x1280', '1920x1080' and '1080x1920'.
71+
A session that begins with archive mode 'always' will be using this resolution for all archives of that session.
72+
Passing 'archive_resolution' with archive mode 'manual' will cause an error response.
73+
6574
* ``e2ee`` which is a boolean. This specifies whether to enable
6675
`end-to-end encryption <https://tokbox.com/developer/guides/end-to-end-encryption/>`_
6776
for the OpenTok session.
@@ -82,6 +91,14 @@ store (such as a database).
8291
# An automatically archived session:
8392
session = opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always)
8493
94+
# An automatically archived session with the archive name and resolution specified:
95+
session = opentok.create_session(
96+
media_mode=MediaModes.routed,
97+
archive_mode=ArchiveModes.always,
98+
archive_name='my_archive',
99+
archive_resolution='1920x1080'
100+
)
101+
85102
# A session with a location hint
86103
session = opentok.create_session(location=u'12.34.56.78')
87104

opentok/opentok.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ class ArchiveModes(Enum):
9090
"""The session will be automatically archived."""
9191

9292

93+
valid_archive_resolutions = {
94+
"640x480",
95+
"480x640",
96+
"1280x720",
97+
"720x1280",
98+
"1920x1080",
99+
"1080x1920",
100+
}
101+
93102
logger = logging.getLogger("opentok")
94103

95104

@@ -312,6 +321,8 @@ def create_session(
312321
location=None,
313322
media_mode=MediaModes.relayed,
314323
archive_mode=ArchiveModes.manual,
324+
archive_name=None,
325+
archive_resolution=None,
315326
e2ee=False,
316327
):
317328
"""
@@ -367,6 +378,16 @@ def create_session(
367378
(either automatically or not), you must set the media_mode parameter to
368379
MediaModes.routed.
369380
381+
:param String archive_name: Indicates the archive name for all the archives in auto archived session.
382+
A session that begins with archive mode 'always' will be using this archive name for all archives of that session.
383+
Passing 'archive_name' with archive mode 'manual' will cause an error response.
384+
385+
:param String archive_resolution:
386+
Indicates the archive resolution for all the archives in auto archived session.
387+
Valid values are '640x480', '480x640', '1280x720', '720x1280', '1920x1080' and '1080x1920'.
388+
A session that begins with archive mode 'always' will be using this resolution for all archives of that session.
389+
Passing 'archive_resolution' with archive mode 'manual' will cause an error response.
390+
370391
:param String location: An IP address that the OpenTok servers will use to
371392
situate the session in its global network. If you do not set a location hint,
372393
the OpenTok servers will be based on the first client connecting to the session.
@@ -397,8 +418,34 @@ def create_session(
397418
"A session with always archive mode must also have the routed media mode."
398419
)
399420
)
421+
422+
if archive_name is not None:
423+
if archive_mode == ArchiveModes.manual:
424+
raise OpenTokException(
425+
"You cannot specify a value for archive_name with archive mode: manual."
426+
)
427+
if not 1 <= len(archive_name) <= 80:
428+
raise OpenTokException(
429+
"archive_name must be between 1 and 80 characters in length."
430+
)
431+
432+
if archive_resolution is not None:
433+
if archive_mode == ArchiveModes.manual:
434+
raise OpenTokException(
435+
"You cannot specify a value for archive_resolution with archive mode: manual."
436+
)
437+
if archive_resolution not in valid_archive_resolutions:
438+
raise OpenTokException(
439+
f"archive_resolution must be one of the allowed values: {valid_archive_resolutions}."
440+
)
441+
400442
options[u("p2p.preference")] = media_mode.value
401443
options[u("archiveMode")] = archive_mode.value
444+
if archive_name is not None:
445+
options[("archiveName")] = archive_name
446+
if archive_resolution is not None:
447+
options[("archiveResolution")] = archive_resolution
448+
402449
if location:
403450
# validate IP address
404451
try:
@@ -452,7 +499,7 @@ def create_session(
452499

453500
session_id = (
454501
dom.getElementsByTagName("session_id")[0].childNodes[0].nodeValue
455-
)
502+
)
456503
return Session(
457504
self,
458505
session_id,

tests/test_session_creation.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ def test_create_always_archive_mode_session(self):
229229
)
230230

231231
session = self.opentok.create_session(
232-
media_mode=MediaModes.routed, archive_mode=ArchiveModes.always
232+
media_mode=MediaModes.routed,
233+
archive_mode=ArchiveModes.always,
234+
archive_name="test_opentok_archive",
235+
archive_resolution="1920x1080",
233236
)
234237

235238
validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")])
@@ -239,6 +242,8 @@ def test_create_always_archive_mode_session(self):
239242
body = parse_qs(httpretty.last_request().body)
240243
expect(body).to(have_key(b("p2p.preference"), [b("disabled")]))
241244
expect(body).to(have_key(b("archiveMode"), [b("always")]))
245+
expect(body).to(have_key(b("archiveName"), [b("test_opentok_archive")]))
246+
expect(body).to(have_key(b("archiveResolution"), [b("1920x1080")]))
242247
expect(session).to(be_a(Session))
243248
expect(session).to(
244249
have_property(
@@ -252,24 +257,44 @@ def test_create_always_archive_mode_session(self):
252257
expect(session).to(have_property(u("archive_mode"), ArchiveModes.always))
253258
expect(session).to(have_property(u("e2ee"), False))
254259

255-
@httpretty.activate
256260
def test_complains_about_always_archive_mode_and_relayed_session(self):
257-
httpretty.register_uri(
258-
httpretty.POST,
259-
u("https://api.opentok.com/session/create"),
260-
body=u(
261-
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'
262-
),
263-
status=200,
264-
content_type=u("text/xml"),
265-
)
266261
self.assertRaises(
267262
OpenTokException,
268263
self.opentok.create_session,
269264
media_mode=MediaModes.relayed,
270265
archive_mode=ArchiveModes.always,
271266
)
272267

268+
def test_auto_archive_errors(self):
269+
self.assertRaises(
270+
OpenTokException,
271+
self.opentok.create_session,
272+
media_mode=MediaModes.routed,
273+
archive_mode=ArchiveModes.manual,
274+
archive_name="my_archive",
275+
)
276+
self.assertRaises(
277+
OpenTokException,
278+
self.opentok.create_session,
279+
media_mode=MediaModes.routed,
280+
archive_mode=ArchiveModes.manual,
281+
archive_resolution="640x480",
282+
)
283+
self.assertRaises(
284+
OpenTokException,
285+
self.opentok.create_session,
286+
media_mode=MediaModes.routed,
287+
archive_mode=ArchiveModes.always,
288+
archive_name="my_incredibly_long_name_that_is_definitely_going_to_be_over_the_80_character_limit_we_currently_impose",
289+
)
290+
self.assertRaises(
291+
OpenTokException,
292+
self.opentok.create_session,
293+
media_mode=MediaModes.routed,
294+
archive_mode=ArchiveModes.always,
295+
archive_resolution="10x10",
296+
)
297+
273298
@httpretty.activate
274299
def test_create_session_with_e2ee(self):
275300
httpretty.register_uri(
@@ -285,7 +310,7 @@ def test_create_session_with_e2ee(self):
285310
session = self.opentok.create_session(e2ee=True)
286311

287312
body = parse_qs(httpretty.last_request().body)
288-
expect(body).to(have_key(b("e2ee"), [b'True']))
313+
expect(body).to(have_key(b("e2ee"), [b"True"]))
289314
expect(session).to(be_a(Session))
290315
expect(session).to(
291316
have_property(

0 commit comments

Comments
 (0)