diff --git a/.travis.yml b/.travis.yml index 295532d..dd89035 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ -sudo: false language: python python: - "2.7" @@ -8,4 +7,4 @@ python: - "3.8" install: "pip install ." script: "nosetests" -dist: bionic \ No newline at end of file +dist: bionic diff --git a/sickle/app.py b/sickle/app.py index 6fa3b1b..86e3599 100644 --- a/sickle/app.py +++ b/sickle/app.py @@ -111,6 +111,7 @@ def __init__(self, endpoint, self.class_mapping = class_mapping or DEFAULT_CLASS_MAP self.encoding = encoding self.request_args = request_args + self.session = requests.Session() def harvest(self, **kwargs): # pragma: no cover """Make HTTP requests to the OAI server. @@ -134,8 +135,8 @@ def harvest(self, **kwargs): # pragma: no cover def _request(self, kwargs): if self.http_method == 'GET': - return requests.get(self.endpoint, params=kwargs, **self.request_args) - return requests.post(self.endpoint, data=kwargs, **self.request_args) + return self.session.get(self.endpoint, params=kwargs, **self.request_args) + return self.session.post(self.endpoint, data=kwargs, **self.request_args) def ListRecords(self, ignore_deleted=False, **kwargs): """Issue a ListRecords request. diff --git a/sickle/tests/test_harvesting.py b/sickle/tests/test_harvesting.py index b0c2886..80d09ae 100644 --- a/sickle/tests/test_harvesting.py +++ b/sickle/tests/test_harvesting.py @@ -10,6 +10,7 @@ from lxml import etree from nose.tools import raises +from requests import Session import mock from sickle import Sickle @@ -238,7 +239,7 @@ class TestCaseWrongEncoding(unittest.TestCase): def __init__(self, methodName='runTest'): super(TestCaseWrongEncoding, self).__init__(methodName) - self.patch = mock.patch('sickle.app.requests.get', mock_get) + self.patch = mock.patch.object(Session, 'get', mock_get) def setUp(self): self.patch.start() diff --git a/sickle/tests/test_sickle.py b/sickle/tests/test_sickle.py index 62b75f5..5802352 100644 --- a/sickle/tests/test_sickle.py +++ b/sickle/tests/test_sickle.py @@ -11,6 +11,7 @@ from mock import patch, Mock from nose.tools import raises from requests import HTTPError +from requests import Session from sickle import Sickle @@ -33,7 +34,7 @@ def test_invalid_iterator(self): def test_pass_request_args(self): mock_response = Mock(text=u'', content='', status_code=200) mock_get = Mock(return_value=mock_response) - with patch('sickle.app.requests.get', mock_get): + with patch.object(Session, 'get', mock_get): sickle = Sickle('url', timeout=10, proxies=dict(), auth=('user', 'password')) sickle.ListRecords() @@ -45,7 +46,7 @@ def test_pass_request_args(self): def test_override_encoding(self): mock_response = Mock(text='', content='', status_code=200) mock_get = Mock(return_value=mock_response) - with patch('sickle.app.requests.get', mock_get): + with patch.object(Session, 'get', mock_get): sickle = Sickle('url', encoding='encoding') sickle.ListSets() mock_get.assert_called_once_with('url', @@ -56,7 +57,7 @@ def test_no_retry(self): headers={'retry-after': '10'}, raise_for_status=Mock(side_effect=HTTPError)) mock_get = Mock(return_value=mock_response) - with patch('sickle.app.requests.get', mock_get): + with patch.object(Session, 'get', mock_get): sickle = Sickle('url') try: sickle.ListRecords() @@ -71,7 +72,7 @@ def test_retry_on_503(self): mock_get = Mock(return_value=mock_response) sleep_mock = Mock() with patch('time.sleep', sleep_mock): - with patch('sickle.app.requests.get', mock_get): + with patch.object(Session, 'get', mock_get): sickle = Sickle('url', max_retries=3, default_retry_after=0) try: sickle.ListRecords() @@ -87,7 +88,7 @@ def test_retry_on_custom_code(self): mock_response = Mock(status_code=500, raise_for_status=Mock(side_effect=HTTPError)) mock_get = Mock(return_value=mock_response) - with patch('sickle.app.requests.get', mock_get): + with patch.object(Session, 'get', mock_get): sickle = Sickle('url', max_retries=3, default_retry_after=0, retry_status_codes=(503, 500)) try: sickle.ListRecords()