Skip to content

Commit 7778e82

Browse files
committed
Add support to archiveMode
1 parent 476f27b commit 7778e82

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

opentok/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .opentok import OpenTok, Roles, MediaModes
1+
from .opentok import OpenTok, Roles, MediaModes, ArchiveModes
22
from .session import Session
33
from .archives import Archive, ArchiveList, OutputModes
44
from .exceptions import OpenTokException

opentok/opentok.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class MediaModes(Enum):
4141
cannot send and receive each others' streams, due to firewalls on the clients' networks,
4242
their streams will be relayed using the OpenTok TURN Server."""
4343

44+
class ArchiveModes(Enum):
45+
"""List of valid settings for the archiveMode parameter of the OpenTok.createSession() method."""
46+
manual = u('manual')
47+
"""The session will be manually archived."""
48+
always = u('always')
49+
"""The session will be automatically archived."""
50+
4451
class OpenTok(object):
4552
"""Use this SDK to create tokens and interface with the server-side portion
4653
of the Opentok API.
@@ -149,7 +156,7 @@ def generate_token(self, session_id, role=Roles.publisher, expire_time=None, dat
149156
)
150157
return token
151158

152-
def create_session(self, location=None, media_mode=MediaModes.relayed):
159+
def create_session(self, location=None, media_mode=MediaModes.relayed, archive_mode=ArchiveModes.manual):
153160
"""
154161
Creates a new OpenTok session and returns the session ID, which uniquely identifies
155162
the session.
@@ -196,6 +203,13 @@ def create_session(self, location=None, media_mode=MediaModes.relayed):
196203
* The OpenTok Media Router supports the archiving feature, which lets
197204
you record, save, and retrieve OpenTok sessions (see http://tokbox.com/platform/archiving).
198205
206+
:param String archiveMode: Whether the session is automatically archived
207+
(ArchiveMode.always) or not (ArchiveMode.manual). By default,
208+
the setting is ArchiveMode.manual, and you must call the
209+
start_archive() method of the OpenTok object to start archiving. To archive the session
210+
(either automatically or not), you must set the mediaMode parameter to
211+
MediaMode.routed.
212+
199213
:param String location: An IP address that the OpenTok servers will use to
200214
situate the session in its global network. If you do not set a location hint,
201215
the OpenTok servers will be based on the first client connecting to the session.
@@ -207,7 +221,12 @@ def create_session(self, location=None, media_mode=MediaModes.relayed):
207221
options = {}
208222
if not isinstance(media_mode, MediaModes):
209223
raise OpenTokException(u('Cannot create session, {0} is not a valid media mode').format(role))
224+
if not isinstance(archive_mode, ArchiveModes):
225+
raise OpenTokException(u('Cannot create session, {0} is not a valid archive mode').format(role))
226+
if archive_mode == ArchiveModes.always and media_mode != MediaModes.routed:
227+
raise OpenTokException(u('A session with always archive mode must also have the routed media mode.'))
210228
options[u('p2p.preference')] = media_mode.value
229+
options[u('archiveMode')] = archive_mode.value
211230
if location:
212231
# validate IP address
213232
try:
@@ -235,7 +254,7 @@ def create_session(self, location=None, media_mode=MediaModes.relayed):
235254
raise AuthError('Failed to create session (code=%s): %s' % (error.attributes['code'].value, error.firstChild.attributes['message'].value))
236255

237256
session_id = dom.getElementsByTagName('session_id')[0].childNodes[0].nodeValue
238-
return Session(self, session_id, location=location, media_mode=media_mode)
257+
return Session(self, session_id, location=location, media_mode=media_mode, archive_mode=archive_mode)
239258
except Exception as e:
240259
raise OpenTokException('Failed to generate session: %s' % str(e))
241260

tests/test_session_creation.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sure import expect
66
import httpretty
77

8-
from opentok import OpenTok, Session, MediaModes, OpenTokException, __version__
8+
from opentok import OpenTok, Session, MediaModes, ArchiveModes, OpenTokException, __version__
99

1010
class OpenTokSessionCreationTest(unittest.TestCase):
1111
def setUp(self):
@@ -24,7 +24,7 @@ def test_create_default_session(self):
2424

2525
expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret)
2626
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
27-
expect(httpretty.last_request().body).to.equal(b('p2p.preference=enabled'))
27+
expect(httpretty.last_request().body).to.equal(b('archiveMode=manual&p2p.preference=enabled'))
2828
expect(session).to.be.a(Session)
2929
expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg'))
3030
expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.relayed)
@@ -41,7 +41,7 @@ def test_create_routed_session(self):
4141

4242
expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret)
4343
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
44-
expect(httpretty.last_request().body).to.equal(b('p2p.preference=disabled'))
44+
expect(httpretty.last_request().body).to.equal(b('archiveMode=manual&p2p.preference=disabled'))
4545
expect(session).to.be.a(Session)
4646
expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg'))
4747
expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed)
@@ -87,5 +87,47 @@ def test_create_routed_session_with_location_hint(self):
8787
expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed)
8888
expect(session).to.have.property(u('location')).being.equal(u('12.34.56.78'))
8989

90+
@httpretty.activate
91+
def test_create_manual_archive_mode_session(self):
92+
httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'),
93+
body=u('<?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>'),
94+
status=200,
95+
content_type=u('text/xml'))
96+
97+
session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.manual)
98+
99+
expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret)
100+
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
101+
expect(httpretty.last_request().body).to.equal(b('archiveMode=manual&p2p.preference=disabled'))
102+
expect(session).to.be.a(Session)
103+
expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg'))
104+
expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed)
105+
expect(session).to.have.property(u('archive_mode')).being.equal(ArchiveModes.manual)
106+
107+
@httpretty.activate
108+
def test_create_always_archive_mode_session(self):
109+
httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'),
110+
body=u('<?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>'),
111+
status=200,
112+
content_type=u('text/xml'))
113+
114+
session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always)
115+
116+
expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret)
117+
expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__)
118+
expect(httpretty.last_request().body).to.equal(b('archiveMode=always&p2p.preference=disabled'))
119+
expect(session).to.be.a(Session)
120+
expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg'))
121+
expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed)
122+
expect(session).to.have.property(u('archive_mode')).being.equal(ArchiveModes.always)
123+
124+
@httpretty.activate
125+
def test_complains_about_always_archive_mode_and_relayed_session(self):
126+
httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'),
127+
body=u('<?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>'),
128+
status=200,
129+
content_type=u('text/xml'))
130+
self.assertRaises(OpenTokException, self.opentok.create_session, media_mode=MediaModes.relayed, archive_mode=ArchiveModes.always)
131+
90132
# TODO: all the cases that throw exceptions
91133
# TODO: custom api_url requests

0 commit comments

Comments
 (0)