Skip to content

Commit aec0e02

Browse files
committed
Only use requests, not a mix of requests & urlopen
We were: 1. In some cases, directly using requests 2. In some cases, directly using the stdlib's urlopen 3. In some cases, had a method named urlopen that simply passed things through to requests This is unnecessarily confusing, and seems to primarily be done for the benefit of mocking the network calls. However, as described in the recently merged jupyterhub#1390, I don't think mocking is appropriate here as it means we don't actually catch problems. This PR mostly focuses on getting unifying to only using requests directly with as little indirection as possible. If any tests were directly using mocks here, they will be replaced with something that is testing things more directly as appropriate
1 parent b7c1515 commit aec0e02

File tree

9 files changed

+13
-33
lines changed

9 files changed

+13
-33
lines changed

repo2docker/contentproviders/ckan.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _fetch_version(self, api_url):
2525
Borrowed from the Hydroshare provider.
2626
"""
2727
package_show_url = f"{api_url}package_show?id={self.dataset_id}"
28-
resp = self.urlopen(package_show_url).json()
28+
resp = self.session.get(package_show_url).json()
2929
date = resp["result"]["metadata_modified"]
3030
parsed_date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%f")
3131
epoch = parsed_date.replace(tzinfo=timezone(timedelta(0))).timestamp()
@@ -35,8 +35,6 @@ def _fetch_version(self, api_url):
3535
def _request(self, url, **kwargs):
3636
return self.session.get(url, **kwargs)
3737

38-
urlopen = _request
39-
4038
def detect(self, source, ref=None, extra_args=None):
4139
"""Trigger this provider for things that resolve to a CKAN dataset."""
4240
parsed_url = urlparse(source)
@@ -58,7 +56,7 @@ def detect(self, source, ref=None, extra_args=None):
5856
).geturl()
5957

6058
status_show_url = f"{api_url}status_show"
61-
resp = self.urlopen(status_show_url)
59+
resp = self.session.get(status_show_url)
6260
if resp.status_code == 200:
6361

6462
# Activity ID may be present either as a query parameter, activity_id
@@ -97,7 +95,7 @@ def fetch(self, spec, output_dir, yield_output=False):
9795
{"id": dataset_id}
9896
)
9997

100-
resp = self.urlopen(
98+
resp = self.session.get(
10199
fetch_url,
102100
headers={"accept": "application/json"},
103101
)

repo2docker/contentproviders/doi.py

-15
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ def __init__(self):
2727
def _request(self, url, **kwargs):
2828
return self.session.get(url, **kwargs)
2929

30-
urlopen = _request
31-
32-
def _urlopen(self, req, headers=None):
33-
"""A urlopen() helper"""
34-
# someone passed a string, not a request
35-
if not isinstance(req, request.Request):
36-
req = request.Request(req)
37-
38-
req.add_header("User-Agent", f"repo2docker {__version__}")
39-
if headers is not None:
40-
for key, value in headers.items():
41-
req.add_header(key, value)
42-
43-
return request.urlopen(req)
44-
4530
def doi2url(self, doi):
4631
# Transform a DOI to a URL
4732
# If not a doi, assume we have a URL and return

repo2docker/contentproviders/figshare.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def fetch(self, spec, output_dir, yield_output=False):
7474
host = spec["host"]
7575

7676
yield f"Fetching Figshare article {article_id} in version {article_version}.\n"
77-
resp = self.urlopen(
77+
resp = self.session.get(
7878
f'{host["api"]}{article_id}/versions/{article_version}',
7979
headers={"accept": "application/json"},
8080
)

repo2docker/contentproviders/hydroshare.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Hydroshare(DoiProvider):
1515

1616
def _fetch_version(self, host):
1717
"""Fetch resource modified date and convert to epoch"""
18-
json_response = self.urlopen(host["version"].format(self.resource_id)).json()
18+
json_response = self.session.get(host["version"].format(self.resource_id)).json()
1919
date = next(
2020
item for item in json_response["dates"] if item["type"] == "modified"
2121
)["start_date"]
@@ -63,7 +63,7 @@ def fetch(self, spec, output_dir, yield_output=False, timeout=120):
6363
yield f"Downloading {bag_url}.\n"
6464

6565
# bag downloads are prepared on demand and may need some time
66-
conn = self.urlopen(bag_url)
66+
conn = self.session.get(bag_url)
6767
total_wait_time = 0
6868
while (
6969
conn.status_code == 200
@@ -77,7 +77,7 @@ def fetch(self, spec, output_dir, yield_output=False, timeout=120):
7777
raise ContentProviderException(msg)
7878
yield f"Bag is being prepared, requesting again in {wait_time} seconds.\n"
7979
time.sleep(wait_time)
80-
conn = self.urlopen(bag_url)
80+
conn = self.session.get(bag_url)
8181
if conn.status_code != 200:
8282
msg = f"Failed to download bag. status code {conn.status_code}.\n"
8383
yield msg

repo2docker/contentproviders/zenodo.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def fetch(self, spec, output_dir, yield_output=False):
7373
host = spec["host"]
7474

7575
yield f"Fetching Zenodo record {record_id}.\n"
76-
resp = self.urlopen(
76+
resp = self.session.get(
7777
f'{host["api"]}{record_id}',
7878
headers={"accept": "application/json"},
7979
)
@@ -82,7 +82,7 @@ def fetch(self, spec, output_dir, yield_output=False):
8282
if host["files"]:
8383
yield f"Fetching Zenodo record {record_id} files.\n"
8484
files_url = deep_get(record, host["files"])
85-
resp = self.urlopen(
85+
resp = self.session.get(
8686
files_url,
8787
headers={"accept": "application/json"},
8888
)

setup.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ def finalize_options(self):
2626

2727
def run(self):
2828
import json
29-
from urllib.request import urlopen
29+
import requests
3030

31-
resp = urlopen(self.url, timeout=5)
32-
resp_body = resp.read()
33-
data = json.loads(resp_body.decode("utf-8"))
31+
resp = requests.get(self.url)
32+
data = resp.json()
3433
if "installations" not in data:
3534
raise ValueError("Malformed installation map.")
3635

tests/unit/contentproviders/test_doi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_url_headers(requests_mock):
2424
doi = DoiProvider()
2525

2626
headers = {"test1": "value1", "Test2": "value2"}
27-
result = doi.urlopen("https://mybinder.org", headers=headers)
27+
result = doi.session.get("https://mybinder.org", headers=headers)
2828
assert "test1" in result.request.headers
2929
assert "Test2" in result.request.headers
3030
assert result.request.headers["User-Agent"] == f"repo2docker {__version__}"

tests/unit/contentproviders/test_figshare.py

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from io import BytesIO
66
from tempfile import NamedTemporaryFile, TemporaryDirectory
77
from unittest.mock import patch
8-
from urllib.request import Request, urlopen
98
from zipfile import ZipFile
109

1110
import pytest

tests/unit/contentproviders/test_zenodo.py

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from io import BytesIO
66
from tempfile import NamedTemporaryFile, TemporaryDirectory
77
from unittest.mock import patch
8-
from urllib.request import Request, urlopen
98
from zipfile import ZipFile
109

1110
import pytest

0 commit comments

Comments
 (0)