Skip to content

Commit f2eea1b

Browse files
committed
Lazy-import requests and scrapinghub in utility functions
1 parent ac644ea commit f2eea1b

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

shub/utils.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,8 @@
1919
from six.moves.urllib.parse import urljoin
2020

2121
import click
22-
import requests
2322
import yaml
2423

25-
from scrapinghub import Connection, APIError
26-
27-
try:
28-
from scrapinghub import HubstorageClient
29-
except ImportError:
30-
# scrapinghub < 1.9.0
31-
from hubstorage import HubstorageClient
32-
3324
import shub
3425
from shub.compat import to_native_str
3526
from shub.exceptions import (BadParameterException, InvalidAuthException,
@@ -77,6 +68,7 @@ def create_default_setup_py(**kwargs):
7768

7869

7970
def make_deploy_request(url, data, files, auth, verbose, keep_log):
71+
import requests
8072
last_logs = deque(maxlen=LAST_N_LOGS)
8173
try:
8274
rsp = requests.post(url=url, auth=auth, data=data, files=files,
@@ -400,6 +392,11 @@ def get_job_specs(job):
400392

401393

402394
def get_job(job):
395+
try:
396+
from scrapinghub import HubstorageClient
397+
except ImportError:
398+
# scrapinghub < 1.9.0
399+
from hubstorage import HubstorageClient
403400
jobid, apikey = get_job_specs(job)
404401
hsc = HubstorageClient(auth=apikey)
405402
job = hsc.get_job(jobid)
@@ -554,6 +551,7 @@ def latest_github_release(force_update=False, timeout=1., cache=None):
554551
# saved
555552
if release_data.get('_shub_last_update', 0) == today:
556553
return release_data
554+
import requests
557555
release_data = requests.get(REQ_URL, timeout=timeout).json()
558556
release_data['_shub_last_update'] = today
559557
try:
@@ -646,6 +644,7 @@ def has_project_access(project, endpoint, apikey):
646644
"""Check whether an API key has access to a given project. May raise
647645
InvalidAuthException if the API key is invalid (but not if it is valid but
648646
lacks access to the project)"""
647+
from scrapinghub import Connection, APIError
649648
conn = Connection(apikey, url=endpoint)
650649
try:
651650
return project in conn.project_ids()

tests/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_get_job_specs_validates_jobid(self):
148148
with self.assertRaises(BadParameterException):
149149
utils.get_job_specs(job_id)
150150

151-
@patch('shub.utils.HubstorageClient', autospec=True)
151+
@patch('scrapinghub.HubstorageClient', autospec=True)
152152
def test_get_job(self, mock_HSC):
153153
class MockJob(object):
154154
metadata = {'some': 'val'}
@@ -271,7 +271,7 @@ def jri_result(follow, tail=None):
271271
job.resource.stats.return_value = {'totals': {'input_values': 1000}}
272272
self.assertEqual(jri_result(True, tail=3), [])
273273

274-
@patch('shub.utils.requests.get', autospec=True)
274+
@patch('requests.get', autospec=True)
275275
def test_latest_github_release(self, mock_get):
276276
with self.runner.isolated_filesystem():
277277
mock_get.return_value.json.return_value = {'key': 'value'}
@@ -463,7 +463,7 @@ def call_update_yaml_dict():
463463
result = runner.invoke(call_update_yaml_dict)
464464
assert 'deprecated' in result.output
465465

466-
@patch('shub.utils.Connection')
466+
@patch('scrapinghub.Connection')
467467
def test_has_project_access(self, mock_conn):
468468
mock_conn.return_value.project_ids.side_effect = APIError(
469469
'Authentication failed')

0 commit comments

Comments
 (0)