Skip to content

Commit c876d42

Browse files
authored
Merge pull request #117 from tylergould/default-timeout
Default timeout
2 parents 1638aa9 + 86a2c12 commit c876d42

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

opentok/opentok.py

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

62-
def __init__(self, api_key, api_secret, api_url='https://api.opentok.com'):
62+
def __init__(self, api_key, api_secret, api_url='https://api.opentok.com', timeout=None):
6363
self.api_key = str(api_key)
6464
self.api_secret = api_secret
6565
self.api_url = api_url
66+
self.timeout = timeout
6667
self._proxies = None
6768

6869
@property
@@ -263,7 +264,7 @@ def create_session(self, location=None, media_mode=MediaModes.relayed, archive_m
263264
options[u('location')] = location
264265

265266
try:
266-
response = requests.post(self.session_url(), data=options, headers=self.headers(), proxies=self.proxies)
267+
response = requests.post(self.session_url(), data=options, headers=self.headers(), proxies=self.proxies, timeout=self.timeout)
267268
response.encoding = 'utf-8'
268269

269270
if response.status_code == 403:
@@ -351,7 +352,7 @@ def start_archive(self, session_id, has_audio=True, has_video=True, name=None, o
351352
'outputMode': output_mode.value
352353
}
353354

354-
response = requests.post(self.archive_url(), data=json.dumps(payload), headers=self.archive_headers(), proxies=self.proxies)
355+
response = requests.post(self.archive_url(), data=json.dumps(payload), headers=self.archive_headers(), proxies=self.proxies, timeout=self.timeout)
355356

356357
if response.status_code < 300:
357358
return Archive(self, response.json())
@@ -377,7 +378,7 @@ def stop_archive(self, archive_id):
377378
378379
:rtype: The Archive object corresponding to the archive being stopped.
379380
"""
380-
response = requests.post(self.archive_url(archive_id) + '/stop', headers=self.archive_headers(), proxies=self.proxies)
381+
response = requests.post(self.archive_url(archive_id) + '/stop', headers=self.archive_headers(), proxies=self.proxies, timeout=self.timeout)
381382

382383
if response.status_code < 300:
383384
return Archive(self, response.json())
@@ -400,7 +401,7 @@ def delete_archive(self, archive_id):
400401
401402
:param String archive_id: The archive ID of the archive to be deleted.
402403
"""
403-
response = requests.delete(self.archive_url(archive_id), headers=self.archive_headers(), proxies=self.proxies)
404+
response = requests.delete(self.archive_url(archive_id), headers=self.archive_headers(), proxies=self.proxies, timeout=self.timeout)
404405

405406
if response.status_code < 300:
406407
pass
@@ -418,7 +419,7 @@ def get_archive(self, archive_id):
418419
419420
:rtype: The Archive object.
420421
"""
421-
response = requests.get(self.archive_url(archive_id), headers=self.archive_headers(), proxies=self.proxies)
422+
response = requests.get(self.archive_url(archive_id), headers=self.archive_headers(), proxies=self.proxies, timeout=self.timeout)
422423

423424
if response.status_code < 300:
424425
return Archive(self, response.json())
@@ -447,7 +448,7 @@ def get_archives(self, offset=None, count=None):
447448
if count is not None:
448449
params['count'] = count
449450

450-
response = requests.get(self.archive_url() + "?" + urlencode(params), headers=self.archive_headers(), proxies=self.proxies)
451+
response = requests.get(self.archive_url() + "?" + urlencode(params), headers=self.archive_headers(), proxies=self.proxies, timeout=self.timeout)
451452

452453
if response.status_code < 300:
453454
return ArchiveList(self, response.json())

tests/test_http_options.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from six import u
3+
from nose.tools import raises
4+
import httpretty
5+
import requests
6+
7+
from opentok import OpenTok, OpenTokException
8+
9+
def _raise_timeout(*args):
10+
raise requests.Timeout('Timeout occurred')
11+
12+
class OpenTokSessionCreationTest(unittest.TestCase):
13+
def setUp(self):
14+
self.api_key = u('123456')
15+
self.api_secret = u('1234567890abcdef1234567890abcdef1234567890')
16+
httpretty.enable()
17+
httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'),
18+
body=_raise_timeout,
19+
status=200,
20+
content_type=u('text/xml'))
21+
22+
def tearDown(self):
23+
httpretty.disable()
24+
25+
@raises(OpenTokException)
26+
def test_timeout(self):
27+
opentok = OpenTok(self.api_key, self.api_secret, timeout=1)
28+
opentok.create_session()

tests/test_initialization.py

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

3333
def test_initialization_with_numeric_api_key(self):
34-
opentok = OpenTok(123456, self.api_secret, self.api_url)
34+
opentok = OpenTok(123456, self.api_secret)
35+
assert isinstance(opentok, OpenTok)
36+
37+
def test_initialization_with_timeout(self):
38+
opentok = OpenTok(self.api_key, self.api_secret, timeout=5)
3539
assert isinstance(opentok, OpenTok)
3640

3741
if __name__ == '__main__':

0 commit comments

Comments
 (0)