Skip to content

Commit

Permalink
Configure whether to open URLs when validating assets
Browse files Browse the repository at this point in the history
  • Loading branch information
vprivat-ads committed Nov 29, 2024
1 parent 6c2b6ae commit 06ab639
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
19 changes: 11 additions & 8 deletions stac_validator/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,31 @@ def set_schema_addr(version: str, stac_type: str) -> str:
def link_request(
link: Dict,
initial_message: Dict,
open_urls: bool = True,
) -> None:
"""Makes a request to a URL and appends it to the relevant field of the initial message.
Args:
link: A dictionary containing a "href" key which is a string representing a URL.
initial_message: A dictionary containing lists for "request_valid", "request_invalid",
"format_valid", and "format_invalid" URLs.
open_urls: Whether to open link href URL
Returns:
None
"""
if is_url(link["href"]):
try:
if "s3" in link["href"]:
context = ssl._create_unverified_context()
response = urlopen(link["href"], context=context)
else:
response = urlopen(link["href"])
status_code = response.getcode()
if status_code == 200:
initial_message["request_valid"].append(link["href"])
if open_urls:
if "s3" in link["href"]:
context = ssl._create_unverified_context()
response = urlopen(link["href"], context=context)
else:
response = urlopen(link["href"])
status_code = response.getcode()
if status_code == 200:
initial_message["request_valid"].append(link["href"])
except Exception:
initial_message["request_invalid"].append(link["href"])
initial_message["format_valid"].append(link["href"])
Expand Down
5 changes: 4 additions & 1 deletion stac_validator/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class StacValidate:
core (bool): Whether to only validate the core STAC object (without extensions).
links (bool): Whether to additionally validate links (only works in default mode).
assets (bool): Whether to additionally validate assets (only works in default mode).
assets_open_urls (bool): Whether to open assets URLs when validating assets.
extensions (bool): Whether to only validate STAC object extensions.
custom (str): The local filepath or remote URL of a custom JSON schema to validate the STAC object.
verbose (bool): Whether to enable verbose output in recursive mode.
Expand All @@ -54,6 +55,7 @@ def __init__(
core: bool = False,
links: bool = False,
assets: bool = False,
assets_open_urls: bool = True,
extensions: bool = False,
custom: str = "",
verbose: bool = False,
Expand All @@ -67,6 +69,7 @@ def __init__(
self.schema = custom
self.links = links
self.assets = assets
self.assets_open_urls = assets_open_urls
self.recursive = recursive
self.max_depth = max_depth
self.extensions = extensions
Expand Down Expand Up @@ -122,7 +125,7 @@ def assets_validator(self) -> Dict:
assets = self.stac_content.get("assets")
if assets:
for asset in assets.values():
link_request(asset, initial_message)
link_request(asset, initial_message, self.assets_open_urls)
return initial_message

def links_validator(self) -> Dict:
Expand Down

0 comments on commit 06ab639

Please sign in to comment.