Skip to content

Commit 06ab639

Browse files
committed
Configure whether to open URLs when validating assets
1 parent 6c2b6ae commit 06ab639

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

stac_validator/utilities.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,31 @@ def set_schema_addr(version: str, stac_type: str) -> str:
152152
def link_request(
153153
link: Dict,
154154
initial_message: Dict,
155+
open_urls: bool = True,
155156
) -> None:
156157
"""Makes a request to a URL and appends it to the relevant field of the initial message.
157158
158159
Args:
159160
link: A dictionary containing a "href" key which is a string representing a URL.
160161
initial_message: A dictionary containing lists for "request_valid", "request_invalid",
161162
"format_valid", and "format_invalid" URLs.
163+
open_urls: Whether to open link href URL
162164
163165
Returns:
164166
None
165167
166168
"""
167169
if is_url(link["href"]):
168170
try:
169-
if "s3" in link["href"]:
170-
context = ssl._create_unverified_context()
171-
response = urlopen(link["href"], context=context)
172-
else:
173-
response = urlopen(link["href"])
174-
status_code = response.getcode()
175-
if status_code == 200:
176-
initial_message["request_valid"].append(link["href"])
171+
if open_urls:
172+
if "s3" in link["href"]:
173+
context = ssl._create_unverified_context()
174+
response = urlopen(link["href"], context=context)
175+
else:
176+
response = urlopen(link["href"])
177+
status_code = response.getcode()
178+
if status_code == 200:
179+
initial_message["request_valid"].append(link["href"])
177180
except Exception:
178181
initial_message["request_invalid"].append(link["href"])
179182
initial_message["format_valid"].append(link["href"])

stac_validator/validate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class StacValidate:
3333
core (bool): Whether to only validate the core STAC object (without extensions).
3434
links (bool): Whether to additionally validate links (only works in default mode).
3535
assets (bool): Whether to additionally validate assets (only works in default mode).
36+
assets_open_urls (bool): Whether to open assets URLs when validating assets.
3637
extensions (bool): Whether to only validate STAC object extensions.
3738
custom (str): The local filepath or remote URL of a custom JSON schema to validate the STAC object.
3839
verbose (bool): Whether to enable verbose output in recursive mode.
@@ -54,6 +55,7 @@ def __init__(
5455
core: bool = False,
5556
links: bool = False,
5657
assets: bool = False,
58+
assets_open_urls: bool = True,
5759
extensions: bool = False,
5860
custom: str = "",
5961
verbose: bool = False,
@@ -67,6 +69,7 @@ def __init__(
6769
self.schema = custom
6870
self.links = links
6971
self.assets = assets
72+
self.assets_open_urls = assets_open_urls
7073
self.recursive = recursive
7174
self.max_depth = max_depth
7275
self.extensions = extensions
@@ -122,7 +125,7 @@ def assets_validator(self) -> Dict:
122125
assets = self.stac_content.get("assets")
123126
if assets:
124127
for asset in assets.values():
125-
link_request(asset, initial_message)
128+
link_request(asset, initial_message, self.assets_open_urls)
126129
return initial_message
127130

128131
def links_validator(self) -> Dict:

0 commit comments

Comments
 (0)