Skip to content

Commit 8029ac7

Browse files
committed
Allow to provide HTTP headers
1 parent 06ab639 commit 8029ac7

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

stac_validator/utilities.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import ssl
44
from typing import Dict
55
from urllib.parse import urlparse
6-
from urllib.request import urlopen
6+
from urllib.request import Request, urlopen
77

88
import requests # type: ignore
99

@@ -153,6 +153,7 @@ def link_request(
153153
link: Dict,
154154
initial_message: Dict,
155155
open_urls: bool = True,
156+
headers: Dict = {}
156157
) -> None:
157158
"""Makes a request to a URL and appends it to the relevant field of the initial message.
158159
@@ -161,6 +162,7 @@ def link_request(
161162
initial_message: A dictionary containing lists for "request_valid", "request_invalid",
162163
"format_valid", and "format_invalid" URLs.
163164
open_urls: Whether to open link href URL
165+
headers: HTTP headers to include in the request
164166
165167
Returns:
166168
None
@@ -169,11 +171,12 @@ def link_request(
169171
if is_url(link["href"]):
170172
try:
171173
if open_urls:
174+
request = Request(link["href"], headers=headers)
172175
if "s3" in link["href"]:
173176
context = ssl._create_unverified_context()
174-
response = urlopen(link["href"], context=context)
177+
response = urlopen(request, context=context)
175178
else:
176-
response = urlopen(link["href"])
179+
response = urlopen(request)
177180
status_code = response.getcode()
178181
if status_code == 200:
179182
initial_message["request_valid"].append(link["href"])

stac_validator/validate.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class StacValidate:
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).
3636
assets_open_urls (bool): Whether to open assets URLs when validating assets.
37+
headers (dict): HTTP headers to include in the requests.
3738
extensions (bool): Whether to only validate STAC object extensions.
3839
custom (str): The local filepath or remote URL of a custom JSON schema to validate the STAC object.
3940
verbose (bool): Whether to enable verbose output in recursive mode.
@@ -56,6 +57,7 @@ def __init__(
5657
links: bool = False,
5758
assets: bool = False,
5859
assets_open_urls: bool = True,
60+
headers: dict = {},
5961
extensions: bool = False,
6062
custom: str = "",
6163
verbose: bool = False,
@@ -70,6 +72,7 @@ def __init__(
7072
self.links = links
7173
self.assets = assets
7274
self.assets_open_urls = assets_open_urls
75+
self.headers: Dict = headers
7376
self.recursive = recursive
7477
self.max_depth = max_depth
7578
self.extensions = extensions
@@ -125,7 +128,7 @@ def assets_validator(self) -> Dict:
125128
assets = self.stac_content.get("assets")
126129
if assets:
127130
for asset in assets.values():
128-
link_request(asset, initial_message, self.assets_open_urls)
131+
link_request(asset, initial_message, self.assets_open_urls, self.headers)
129132
return initial_message
130133

131134
def links_validator(self) -> Dict:
@@ -145,7 +148,7 @@ def links_validator(self) -> Dict:
145148
for link in self.stac_content["links"]:
146149
if not is_valid_url(link["href"]):
147150
link["href"] = root_url + link["href"][1:]
148-
link_request(link, initial_message)
151+
link_request(link, initial_message, True, self.headers)
149152

150153
return initial_message
151154

0 commit comments

Comments
 (0)