Skip to content

Commit 4be28e2

Browse files
committed
Merge remote-tracking branch 'base/f.default-timeout' into default-timeout
2 parents 866f50e + c89d731 commit 4be28e2

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
@@ -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: 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
@@ -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)