Skip to content

Commit

Permalink
Allow to provide HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
vprivat-ads committed Dec 2, 2024
1 parent 06ab639 commit 8029ac7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 6 additions & 3 deletions stac_validator/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ssl
from typing import Dict
from urllib.parse import urlparse
from urllib.request import urlopen
from urllib.request import Request, urlopen

import requests # type: ignore

Expand Down Expand Up @@ -153,6 +153,7 @@ def link_request(
link: Dict,
initial_message: Dict,
open_urls: bool = True,
headers: Dict = {}
) -> None:
"""Makes a request to a URL and appends it to the relevant field of the initial message.
Expand All @@ -161,6 +162,7 @@ def link_request(
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
headers: HTTP headers to include in the request
Returns:
None
Expand All @@ -169,11 +171,12 @@ def link_request(
if is_url(link["href"]):
try:
if open_urls:
request = Request(link["href"], headers=headers)
if "s3" in link["href"]:
context = ssl._create_unverified_context()
response = urlopen(link["href"], context=context)
response = urlopen(request, context=context)
else:
response = urlopen(link["href"])
response = urlopen(request)
status_code = response.getcode()
if status_code == 200:
initial_message["request_valid"].append(link["href"])
Expand Down
7 changes: 5 additions & 2 deletions stac_validator/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class StacValidate:
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.
headers (dict): HTTP headers to include in the requests.
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 @@ -56,6 +57,7 @@ def __init__(
links: bool = False,
assets: bool = False,
assets_open_urls: bool = True,
headers: dict = {},
extensions: bool = False,
custom: str = "",
verbose: bool = False,
Expand All @@ -70,6 +72,7 @@ def __init__(
self.links = links
self.assets = assets
self.assets_open_urls = assets_open_urls
self.headers: Dict = headers
self.recursive = recursive
self.max_depth = max_depth
self.extensions = extensions
Expand Down Expand Up @@ -125,7 +128,7 @@ def assets_validator(self) -> Dict:
assets = self.stac_content.get("assets")
if assets:
for asset in assets.values():
link_request(asset, initial_message, self.assets_open_urls)
link_request(asset, initial_message, self.assets_open_urls, self.headers)
return initial_message

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

return initial_message

Expand Down

0 comments on commit 8029ac7

Please sign in to comment.