Skip to content

Commit

Permalink
Add a mechanism for adding headers to StacIO requests
Browse files Browse the repository at this point in the history
  • Loading branch information
john-dupuy authored and gadomski committed Oct 13, 2022
1 parent 8b2a540 commit 01f71b1
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pystac/stac_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

from urllib.request import urlopen
from urllib.request import Request
from urllib.error import HTTPError

import pystac
Expand All @@ -38,6 +39,9 @@
class StacIO(ABC):
_default_io: Optional[Callable[[], "StacIO"]] = None

def __init__(self, headers: Optional[Dict[str, str]] = None):
self.headers = headers or {}

@abstractmethod
def read_text(self, source: HREF, *args: Any, **kwargs: Any) -> str:
"""Read text from the given URI.
Expand Down Expand Up @@ -289,7 +293,8 @@ def read_text_from_href(self, href: str) -> str:
href_contents: str
if parsed.scheme != "":
try:
with urlopen(href) as f:
req = Request(href, headers=self.headers)
with urlopen(req) as f:
href_contents = f.read().decode("utf-8")
except HTTPError as e:
raise Exception("Could not read uri {}".format(href)) from e
Expand Down

0 comments on commit 01f71b1

Please sign in to comment.