Skip to content

Commit 8bf21d3

Browse files
committed
Hide exposed import to requests
Closes #144
1 parent 487e5da commit 8bf21d3

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ xml = ["lxml"]
7777
pandas = ["pandas"]
7878
aws = ["boto3"]
7979
ratelimit = ["ratelimit", "requests"]
80-
bs4 = ["bs4"]
80+
bs4 = ["bs4", "requests"]
8181
yaml = ["pyyaml"]
8282
pydantic = ["pydantic"]
8383
cli = ["click"]

src/pystow/utils/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from urllib.parse import urlparse
3333
from uuid import uuid4
3434

35-
import requests
3635
from tqdm.auto import tqdm
3736

3837
from .download import (
@@ -88,7 +87,14 @@
8887
stream_write_pydantic_jsonl,
8988
write_pydantic_jsonl,
9089
)
91-
from .safe_open import is_url, open_inner_zipfile, open_url, safe_open, safe_open_dict_reader
90+
from .safe_open import (
91+
is_url,
92+
open_inner_zipfile,
93+
open_url,
94+
safe_open,
95+
safe_open_dict_reader,
96+
safe_open_json,
97+
)
9298
from ..constants import README_TEXT, TimeoutHint
9399

94100
if TYPE_CHECKING:
@@ -168,6 +174,7 @@
168174
"safe_open",
169175
"safe_open_dict_reader",
170176
"safe_open_dict_writer",
177+
"safe_open_json",
171178
"safe_open_reader",
172179
"safe_open_writer",
173180
"safe_tarfile_open",
@@ -707,8 +714,8 @@ def get_commit(org: str, repo: str, provider: str = "git") -> str:
707714
lines = (line.strip().split("\t") for line in output.decode("utf8").splitlines())
708715
rv = next(line[0] for line in lines if line[1] == "HEAD")
709716
elif provider == "github":
710-
res = requests.get(f"https://api.github.com/repos/{org}/{repo}/branches/master", timeout=15)
711-
res_json = res.json()
717+
url = f"https://api.github.com/repos/{org}/{repo}/branches/master"
718+
res_json = safe_open_json(url)
712719
rv = res_json["commit"]["sha"]
713720
else:
714721
raise ValueError(f"invalid implementation: {provider}")
@@ -855,6 +862,7 @@ def get_soup(
855862
856863
:returns: A BeautifulSoup object
857864
"""
865+
import requests
858866
from bs4 import BeautifulSoup
859867

860868
headers = {}

src/pystow/utils/safe_open.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import csv
77
import gzip
88
import io
9+
import json
910
import typing
1011
import urllib.request
1112
import zipfile
@@ -32,6 +33,7 @@
3233
"open_url",
3334
"safe_open",
3435
"safe_open_dict_reader",
36+
"safe_open_json",
3537
]
3638

3739

@@ -139,6 +141,12 @@ def safe_open( # noqa:C901
139141
raise TypeError(f"unsupported type for opening: {type(path)} - {path}")
140142

141143

144+
def safe_open_json(path_or_url: str) -> Any:
145+
"""Safely open a file and parse as JSON."""
146+
with safe_open(path_or_url, representation="text", operation="read") as file:
147+
return json.load(file)
148+
149+
142150
# docstr-coverage:excused `overload`
143151
@typing.overload
144152
@contextlib.contextmanager

tests/test_utils/test_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
read_zipfile_xml,
5050
safe_open,
5151
safe_open_dict_reader,
52+
safe_open_json,
5253
safe_open_reader,
5354
safe_open_writer,
5455
tarfile_writestr,
@@ -517,6 +518,13 @@ def test_open_url(self) -> None:
517518
) as file:
518519
self.assertIn("sleep 5", file.read().decode("utf-8"))
519520

521+
def test_get_json(self) -> None:
522+
"""Test getting JSON."""
523+
url = "https://zenodo.org/records/15826754/files/configuration.json?download=1"
524+
x = safe_open_json(url)
525+
self.assertIsInstance(x, dict)
526+
self.assertIn("name", x)
527+
520528

521529
class TestDownload(unittest.TestCase):
522530
"""Tests for downloading."""

0 commit comments

Comments
 (0)