Skip to content

Commit 01f71b1

Browse files
john-dupuygadomski
authored andcommitted
Add a mechanism for adding headers to StacIO requests
1 parent 8b2a540 commit 01f71b1

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

pystac/stac_io.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313

1414
from urllib.request import urlopen
15+
from urllib.request import Request
1516
from urllib.error import HTTPError
1617

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

42+
def __init__(self, headers: Optional[Dict[str, str]] = None):
43+
self.headers = headers or {}
44+
4145
@abstractmethod
4246
def read_text(self, source: HREF, *args: Any, **kwargs: Any) -> str:
4347
"""Read text from the given URI.
@@ -289,7 +293,8 @@ def read_text_from_href(self, href: str) -> str:
289293
href_contents: str
290294
if parsed.scheme != "":
291295
try:
292-
with urlopen(href) as f:
296+
req = Request(href, headers=self.headers)
297+
with urlopen(req) as f:
293298
href_contents = f.read().decode("utf-8")
294299
except HTTPError as e:
295300
raise Exception("Could not read uri {}".format(href)) from e

0 commit comments

Comments
 (0)