Skip to content

Commit 1638b9c

Browse files
committed
Allow to provide HTTP headers, Configure whether to open URLs when validating assets
1 parent 1395e49 commit 1638b9c

File tree

4 files changed

+87
-13
lines changed

4 files changed

+87
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/)
66

77
## Unreleased
88

9+
### Added
10+
11+
- Allow to provide HTTP headers ([#114](https://github.com/stac-utils/stac-check/pull/114))
12+
- Configure whether to open URLs when validating assets ([#114](https://github.com/stac-utils/stac-check/pull/114))
13+
914
### Changed
1015

1116
- No longer use the deprecated pkg-resources package.

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Options:
2828
argument to get full recursion. Ignored if
2929
`recursive == False`.
3030
-r, --recursive Recursively validate all related stac objects.
31+
--no-assets-urls Disables the opening of href links when validating assets
32+
(enabled by default).
33+
--header KEY VALUE HTTP header to include in the requests. Can be used
34+
multiple times.
3135
--help Show this message and exit. Show this message and exit.
3236
```
3337
---
@@ -62,7 +66,7 @@ stac-check: STAC spec validation and linting tool
6266
6367
Please upgrade from version 0.9.0 to version 1.0.0!
6468
65-
Validator: stac-validator 3.1.0
69+
Validator: stac-validator 3.5.0
6670
6771
6872
Recursive: Validate all assets in a collection or catalog
@@ -102,7 +106,7 @@ Error Message: Expecting value: line 1 column 1 (char 0)
102106

103107
Please upgrade from version 0.9.0 to version 1.0.0!
104108

105-
Validator: stac-validator 2.3.0
109+
Validator: stac-validator 3.5.0
106110

107111
Valid ITEM: True
108112

@@ -130,7 +134,7 @@ This object has 4 links
130134

131135
Thanks for using STAC version 1.0.0!
132136

133-
Validator: stac-validator 2.3.0
137+
Validator: stac-validator 3.5.0
134138

135139
Valid ITEM: True
136140

@@ -165,7 +169,7 @@ This object has 4 links
165169

166170
Thanks for using STAC version 1.0.0!
167171

168-
Validator: stac-validator 2.3.0
172+
Validator: stac-validator 3.5.0
169173

170174
Valid ITEM: True
171175

@@ -207,7 +211,7 @@ This object has 4 links
207211

208212
Please upgrade from version 0.9.0 to version 1.0.0!
209213

210-
Validator: stac-validator 2.3.0
214+
Validator: stac-validator 3.5.0
211215

212216
Valid : False
213217

@@ -224,6 +228,31 @@ Validation error message:
224228

225229
This object has 5 links
226230
</pre>
231+
232+
``` stac-check https://stac-catalog.eu/collections/sentinel-s2-l2a/items/item1 --assets --no-assets-urls --header x-api-key $MY_API_KEY --header foo bar```
233+
<pre>
234+
<b>stac-check: STAC spec validation and linting tool</b>
235+
236+
Thanks for using STAC version 1.0.0!
237+
238+
Validator: stac-validator 3.5.0
239+
240+
Valid ITEM: True
241+
242+
Schemas validated:
243+
https://stac-extensions.github.io/timestamps/v1.1.0/schema.json
244+
https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json
245+
246+
STAC Best Practices:
247+
A STAC collection should contain a summaries field
248+
It is recommended to store information like eo:bands in summaries
249+
250+
251+
No ASSET format errors!
252+
253+
No ASSET request errors!
254+
This object has 4 links
255+
</pre>
227256
---
228257
### Create local docs in the /docs folder
229258
`$ pdoc --html --output-dir pdoc stac_check --force`

stac_check/cli.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,29 @@ def cli_message(linter: Linter) -> None:
178178
@click.option(
179179
"-l", "--links", is_flag=True, help="Validate links for format and response."
180180
)
181+
@click.option(
182+
"--no-assets-urls",
183+
is_flag=True,
184+
help="Disables the opening of href links when validating assets (enabled by default).",
185+
)
186+
@click.option(
187+
"--header",
188+
type=(str, str),
189+
multiple=True,
190+
help="HTTP header to include in the requests. Can be used multiple times.",
191+
)
181192
@click.command()
182193
@click.argument("file")
183194
@click.version_option(version=importlib.metadata.distribution("stac-check").version)
184-
def main(file, recursive, max_depth, assets, links):
195+
def main(file, recursive, max_depth, assets, links, no_assets_urls, header):
185196
linter = Linter(
186-
file, assets=assets, links=links, recursive=recursive, max_depth=max_depth
197+
file,
198+
assets=assets,
199+
links=links,
200+
recursive=recursive,
201+
max_depth=max_depth,
202+
assets_open_urls=not no_assets_urls,
203+
headers=dict(header),
187204
)
188205
intro_message(linter)
189206
if recursive > 0:

stac_check/lint.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import importlib.resources
33
import json
44
import os
5-
from dataclasses import dataclass
5+
from dataclasses import dataclass, field
66
from typing import Any, Dict, List, Optional, Union
77

88
import requests
@@ -25,6 +25,8 @@ class Linter:
2525
links (bool, optional): A boolean value indicating whether to validate links. Defaults to False.
2626
recursive (bool, optional): A boolean value indicating whether to perform recursive validation. Defaults to False.
2727
max_depth (Optional[int], optional): An optional integer indicating the maximum depth to validate recursively. Defaults to None.
28+
assets_open_urls (bool): Whether to open assets URLs when validating assets. Defaults to True.
29+
headers (dict): HTTP headers to include in the requests.
2830
2931
Attributes:
3032
data (dict): A dictionary representing the STAC JSON file.
@@ -126,6 +128,8 @@ def check_summaries(self) -> bool:
126128
links: bool = False
127129
recursive: bool = False
128130
max_depth: Optional[int] = None
131+
assets_open_urls: bool = True
132+
headers: dict = field(default_factory=dict)
129133

130134
def __post_init__(self):
131135
self.data = self.load_data(self.item)
@@ -236,7 +240,7 @@ def load_data(self, file: Union[str, Dict]) -> Dict:
236240

237241
if isinstance(file, str):
238242
if is_valid_url(file):
239-
resp = requests.get(file)
243+
resp = requests.get(file, headers=self.headers)
240244
data = resp.json()
241245
else:
242246
with open(file) as json_file:
@@ -260,10 +264,18 @@ def validate_file(self, file: Union[str, dict]) -> Dict[str, Any]:
260264
ValueError: If `file` is not a valid file path or STAC dictionary.
261265
"""
262266
if isinstance(file, str):
263-
stac = StacValidate(file, links=self.links, assets=self.assets)
267+
stac = StacValidate(
268+
file,
269+
links=self.links,
270+
assets=self.assets,
271+
assets_open_urls=self.assets_open_urls,
272+
headers=self.headers,
273+
)
264274
stac.run()
265275
elif isinstance(file, dict):
266-
stac = StacValidate()
276+
stac = StacValidate(
277+
assets_open_urls=self.assets_open_urls, headers=self.headers
278+
)
267279
stac.validate_dict(file)
268280
else:
269281
raise ValueError("Input must be a file path or STAC dictionary.")
@@ -284,10 +296,21 @@ def recursive_validation(self, file: Union[str, Dict[str, Any]]) -> str:
284296
"""
285297
if self.recursive:
286298
if isinstance(file, str):
287-
stac = StacValidate(file, recursive=True, max_depth=self.max_depth)
299+
stac = StacValidate(
300+
file,
301+
recursive=True,
302+
max_depth=self.max_depth,
303+
assets_open_urls=self.assets_open_urls,
304+
headers=self.headers,
305+
)
288306
stac.run()
289307
else:
290-
stac = StacValidate(recursive=True, max_depth=self.max_depth)
308+
stac = StacValidate(
309+
recursive=True,
310+
max_depth=self.max_depth,
311+
assets_open_urls=self.assets_open_urls,
312+
headers=self.headers,
313+
)
291314
stac.validate_dict(file)
292315
return stac.message
293316
else:

0 commit comments

Comments
 (0)