Skip to content

Commit c89d731

Browse files
committed
WIP add timeout option
1 parent f4b39aa commit c89d731

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

opentok/opentok.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ class OpenTok(object):
4848
TOKEN_SENTINEL = 'T1=='
4949
"""For internal use."""
5050

51-
def __init__(self, api_key, api_secret, api_url='https://api.opentok.com'):
51+
def __init__(self, api_key, api_secret, api_url='https://api.opentok.com', timeout=None):
5252
self.api_key = str(api_key)
5353
self.api_secret = api_secret
5454
self.api_url = api_url
55+
self.timeout = timeout
5556

5657
def generate_token(self, session_id, role=Roles.publisher, expire_time=None, data=None):
5758
"""
@@ -216,7 +217,7 @@ def create_session(self, location=None, media_mode=MediaModes.relayed):
216217
options[u('location')] = location
217218

218219
try:
219-
response = requests.post(self.session_url(), data=options, headers=self.headers())
220+
response = requests.post(self.session_url(), data=options, headers=self.headers(), timeout=self.timeout)
220221
response.encoding = 'utf-8'
221222

222223
if response.status_code == 403:
@@ -284,7 +285,7 @@ def start_archive(self, session_id, **kwargs):
284285
"""
285286
payload = {'name': kwargs.get('name'), 'sessionId': session_id}
286287

287-
response = requests.post(self.archive_url(), data=json.dumps(payload), headers=self.archive_headers())
288+
response = requests.post(self.archive_url(), data=json.dumps(payload), headers=self.archive_headers(), timeout=self.timeout)
288289

289290
if response.status_code < 300:
290291
return Archive(self, response.json())
@@ -310,7 +311,7 @@ def stop_archive(self, archive_id):
310311
311312
:rtype: The Archive object corresponding to the archive being stopped.
312313
"""
313-
response = requests.post(self.archive_url(archive_id) + '/stop', headers=self.archive_headers())
314+
response = requests.post(self.archive_url(archive_id) + '/stop', headers=self.archive_headers(), timeout=self.timeout)
314315

315316
if response.status_code < 300:
316317
return Archive(self, response.json())
@@ -333,7 +334,7 @@ def delete_archive(self, archive_id):
333334
334335
:param String archive_id: The archive ID of the archive to be deleted.
335336
"""
336-
response = requests.delete(self.archive_url(archive_id), headers=self.archive_headers())
337+
response = requests.delete(self.archive_url(archive_id), headers=self.archive_headers(), timeout=self.timeout)
337338

338339
if response.status_code < 300:
339340
pass
@@ -351,7 +352,7 @@ def get_archive(self, archive_id):
351352
352353
:rtype: The Archive object.
353354
"""
354-
response = requests.get(self.archive_url(archive_id), headers=self.archive_headers())
355+
response = requests.get(self.archive_url(archive_id), headers=self.archive_headers(), timeout=self.timeout)
355356

356357
if response.status_code < 300:
357358
return Archive(self, response.json())
@@ -380,7 +381,7 @@ def get_archives(self, offset=None, count=None):
380381
if count is not None:
381382
params['count'] = count
382383

383-
response = requests.get(self.archive_url() + "?" + urlencode(params), headers=self.archive_headers())
384+
response = requests.get(self.archive_url() + "?" + urlencode(params), headers=self.archive_headers(), timeout=self.timeout)
384385

385386
if response.status_code < 300:
386387
return ArchiveList(self, response.json())

tests/test_http_options.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
from six import text_type, u, b, PY2, PY3
3+
from six.moves.urllib.parse import parse_qs
4+
from nose.tools import raises
5+
from sure import expect
6+
import httpretty
7+
8+
from opentok import OpenTok, Session, MediaModes, OpenTokException, __version__
9+
10+
class OpenTokSessionCreationTest(unittest.TestCase):
11+
def setUp(self):
12+
self.api_key = u('123456')
13+
self.api_secret = u('1234567890abcdef1234567890abcdef1234567890')
14+
httpretty.enable()
15+
httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'),
16+
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>'),
17+
status=200,
18+
content_type=u('text/xml'))
19+
20+
def tearDown(self):
21+
httpretty.disable()
22+
23+
def test_timeout(self):
24+
# TODO: tell httpretty to timeout
25+
opentok = OpenTok(self.api_key, self.api_secret, timeout=5)
26+
27+
session = opentok.create_session()
28+
29+
# TODO: expect an exception

tests/test_initialization.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ def test_initialization_with_api_url(self):
2525
assert isinstance(opentok, OpenTok)
2626

2727
def test_initialization_with_numeric_api_key(self):
28-
opentok = OpenTok(123456, self.api_secret, self.api_url)
28+
opentok = OpenTok(123456, self.api_secret)
29+
assert isinstance(opentok, OpenTok)
30+
31+
def test_initialization_with_timeout(self):
32+
opentok = OpenTok(self.api_key, self.api_secret, timeout=5)
2933
assert isinstance(opentok, OpenTok)
3034

3135
if __name__ == '__main__':

0 commit comments

Comments
 (0)