Skip to content

Commit

Permalink
now passes timeout through each stage, and tests that said parameter …
Browse files Browse the repository at this point in the history
…is passed through
  • Loading branch information
ladyrassilon committed Oct 4, 2014
1 parent 18ee7cc commit bbaf855
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 4 additions & 4 deletions presser/presser.py
Expand Up @@ -7,9 +7,9 @@
from .exceptions import PresserJavaScriptParseError, PresserURLError, Presser404Error, PresserRequestError, PresserInvalidVineIdError

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

def get_data_for_vine_from_url(self, url):
def get_data_for_vine_from_url(self, url, timeout=30):
parsed_url = urllib.parse.urlparse(url)
if parsed_url.netloc == "vine.co":
results = re.search('/v/(?P<vine_id>\w+)',parsed_url.path)
if results:
vine_id = results.group("vine_id")
return self.get_data_for_vine_id(vine_id)
return self.get_data_for_vine_id(vine_id, timeout=timeout)
else:
raise PresserInvalidVineIdError("{} does not contain a valid vine id".format(parsed_url.path))
else:
Expand Down
12 changes: 9 additions & 3 deletions tests/unit.py
Expand Up @@ -2,7 +2,7 @@
import responses
import requests

from mock import patch
from mock import patch, MagicMock

from presser.presser import Presser
from presser.exceptions import Presser404Error, PresserURLError, PresserInvalidVineIdError, PresserJavaScriptParseError, PresserRequestError
Expand Down Expand Up @@ -30,7 +30,7 @@ def test_not_a_valid_vine_id(self):
@patch('presser.presser.Presser.get_data_for_vine_id')
def test_vine_id_extraction(self, vine_response):
vine = self.presser.get_data_for_vine_from_url(VINE_URL)
self.presser.get_data_for_vine_id.assert_called_with(VINE_ID)
self.presser.get_data_for_vine_id.assert_called_with(VINE_ID, timeout=30)

@responses.activate
def test_vine_data_extraction(self):
Expand Down Expand Up @@ -84,4 +84,10 @@ def test_error_request(self):

@patch("requests.models.Response.ok", False)
def test_page_not_okay(self):
self.assertRaises(PresserURLError, self.presser.get_data_for_vine_from_url, VINE_URL)
self.assertRaises(PresserURLError, self.presser.get_data_for_vine_from_url, VINE_URL)

@patch("requests.get")
def test_timeout_passed_through(self, request_mock):
#Yes this is hacky, BUT responses doesn't record the timeout parameter
self.assertRaises(TypeError, self.presser.get_data_for_vine_from_url, VINE_URL, timeout=5)
requests.get.assert_called_with(VINE_URL, timeout=5)

0 comments on commit bbaf855

Please sign in to comment.