Skip to content

Commit

Permalink
Merge pull request #603 from bioimage-io/timeout
Browse files Browse the repository at this point in the history
add timeout to all get requests
  • Loading branch information
FynnBe authored May 28, 2024
2 parents 9c8c7c0 + 1d174b0 commit 38cb0af
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bioimageio/spec/_internal/docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_ref_url(
"github.com", "raw.githubusercontent.com"
).replace("/blob/", "/")
try:
code = requests.get(raw_github_file_url).text
code = requests.get(raw_github_file_url, timeout=5).text
except requests.RequestException as e:
warnings.warn(
f"Could not resolve {github_file_url} due to {e}. Please check your"
Expand Down
28 changes: 19 additions & 9 deletions bioimageio/spec/_internal/field_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,27 @@ def validate_gh_user(username: str, hotfix_known_errorenous_names: bool = True)
if username.lower() in KNOWN_INVALID_GH_USERS:
raise ValueError(f"Known invalid GitHub user '{username}'")

r = requests.get(
f"https://api.github.com/users/{username}", auth=settings.github_auth
)
if r.status_code == 403 and r.reason == "rate limit exceeded":
try:
r = requests.get(
f"https://api.github.com/users/{username}",
auth=settings.github_auth,
timeout=3,
)
except requests.exceptions.Timeout:
issue_warning(
"Could not verify GitHub user '{value}' due to GitHub API rate limit",
"Could not verify GitHub user '{value}' due to connection timeout",
value=username,
)
elif r.status_code != 200:
KNOWN_INVALID_GH_USERS.add(username.lower())
raise ValueError(f"Could not find GitHub user '{username}'")
else:
if r.status_code == 403 and r.reason == "rate limit exceeded":
issue_warning(
"Could not verify GitHub user '{value}' due to GitHub API rate limit",
value=username,
)
elif r.status_code != 200:
KNOWN_INVALID_GH_USERS.add(username.lower())
raise ValueError(f"Could not find GitHub user '{username}'")

KNOWN_GH_USERS.add(username.lower())

KNOWN_GH_USERS.add(username.lower())
return username
4 changes: 3 additions & 1 deletion bioimageio/spec/_internal/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ def _get_one_collection(url: str):
if not isinstance(url, str) or "/" not in url:
logger.error("invalid collection url: {}", url)
try:
collection: List[Dict[Any, Any]] = requests.get(url).json().get("collection")
collection: List[Dict[Any, Any]] = (
requests.get(url, timeout=5).json().get("collection")
)
except Exception as e:
logger.error("failed to get {}: {}", url, e)
return ret
Expand Down
2 changes: 1 addition & 1 deletion bioimageio/spec/_internal/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _validate_url(url: Union[str, pydantic.HttpUrl]) -> pydantic.AnyUrl:
)

try:
response = requests.get(val_url, stream=True, timeout=(3, 3))
response = requests.get(val_url, stream=True, timeout=3)
except (
requests.exceptions.ChunkedEncodingError,
requests.exceptions.ContentDecodingError,
Expand Down
2 changes: 1 addition & 1 deletion bioimageio/spec/partner_utils/imjoy/_plugin_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def convert_config_to_rdf(plugin_config, source_url=None) -> dict:

def get_plugin_as_rdf(source_url: str) -> Dict[Any, Any]:
"""Get imjoy plugin config in RDF format."""
req = requests.get(source_url)
req = requests.get(source_url, timeout=5)
source = req.text
plugin_config = parse_imjoy_plugin(source)
rdf = convert_config_to_rdf(plugin_config, source_url)
Expand Down
2 changes: 1 addition & 1 deletion scripts/update_spdx_licenses_zenodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def main(recheck: bool = False):
"https://zenodo.org/api/vocabularies/licenses/"
+ lic["licenseId"].lower()
)
r = requests.get(url)
r = requests.get(url, timeout=5)

if 200 <= r.status_code < 300:
known = True
Expand Down

0 comments on commit 38cb0af

Please sign in to comment.