Skip to content

Commit bbaf855

Browse files
committed
now passes timeout through each stage, and tests that said parameter is passed through
1 parent 18ee7cc commit bbaf855

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

presser/presser.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from .exceptions import PresserJavaScriptParseError, PresserURLError, Presser404Error, PresserRequestError, PresserInvalidVineIdError
88

99
class Presser:
10-
def get_data_for_vine_id(self, vine_id):
10+
def get_data_for_vine_id(self, vine_id, timeout=30):
1111
try:
12-
page = requests.get("https://vine.co/v/{}".format(vine_id))
12+
page = requests.get("https://vine.co/v/{}".format(vine_id), timeout=timeout)
1313
except requests.exceptions.RequestException as e:
1414
error_message = "Problem with comminicating with vine page - {}".format(e)
1515
raise PresserRequestError(error_message)
@@ -40,13 +40,13 @@ def get_data_for_vine_id(self, vine_id):
4040
else:
4141
raise PresserURLError("{} could not be accessed {} - {}".format(page.url, page.status_code,page.content))
4242

43-
def get_data_for_vine_from_url(self, url):
43+
def get_data_for_vine_from_url(self, url, timeout=30):
4444
parsed_url = urllib.parse.urlparse(url)
4545
if parsed_url.netloc == "vine.co":
4646
results = re.search('/v/(?P<vine_id>\w+)',parsed_url.path)
4747
if results:
4848
vine_id = results.group("vine_id")
49-
return self.get_data_for_vine_id(vine_id)
49+
return self.get_data_for_vine_id(vine_id, timeout=timeout)
5050
else:
5151
raise PresserInvalidVineIdError("{} does not contain a valid vine id".format(parsed_url.path))
5252
else:

tests/unit.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import responses
33
import requests
44

5-
from mock import patch
5+
from mock import patch, MagicMock
66

77
from presser.presser import Presser
88
from presser.exceptions import Presser404Error, PresserURLError, PresserInvalidVineIdError, PresserJavaScriptParseError, PresserRequestError
@@ -30,7 +30,7 @@ def test_not_a_valid_vine_id(self):
3030
@patch('presser.presser.Presser.get_data_for_vine_id')
3131
def test_vine_id_extraction(self, vine_response):
3232
vine = self.presser.get_data_for_vine_from_url(VINE_URL)
33-
self.presser.get_data_for_vine_id.assert_called_with(VINE_ID)
33+
self.presser.get_data_for_vine_id.assert_called_with(VINE_ID, timeout=30)
3434

3535
@responses.activate
3636
def test_vine_data_extraction(self):
@@ -84,4 +84,10 @@ def test_error_request(self):
8484

8585
@patch("requests.models.Response.ok", False)
8686
def test_page_not_okay(self):
87-
self.assertRaises(PresserURLError, self.presser.get_data_for_vine_from_url, VINE_URL)
87+
self.assertRaises(PresserURLError, self.presser.get_data_for_vine_from_url, VINE_URL)
88+
89+
@patch("requests.get")
90+
def test_timeout_passed_through(self, request_mock):
91+
#Yes this is hacky, BUT responses doesn't record the timeout parameter
92+
self.assertRaises(TypeError, self.presser.get_data_for_vine_from_url, VINE_URL, timeout=5)
93+
requests.get.assert_called_with(VINE_URL, timeout=5)

0 commit comments

Comments
 (0)