Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mechanism for adding headers to StacIO requests #889

Merged
merged 4 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

- Adds custom `header` support to `DefaultStacIO` ([#889](https://github.com/stac-utils/pystac/pull/889))

### Removed

### Changed
Expand Down
22 changes: 19 additions & 3 deletions docs/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,25 @@ created automatically by all of the object-specific I/O methods (e.g.
:meth:`pystac.Catalog.from_file`), so most users will not need to instantiate this
class themselves.

If you require custom logic for I/O operations or would like to use a 3rd-party library
for I/O operations (e.g. ``requests``), you can create a sub-class of
:class:`pystac.StacIO` (or :class:`pystac.DefaultStacIO`) and customize the methods as
If you are dealing with a STAC catalog with URIs that require authentication.
It is possible provide auth headers (or any other customer headers) to the
:class:`pystac.stac_io.DefaultStacIO`.

.. code-block:: python

from pystac import Catalog
from pystac import StacIO

stac_io = StacIO.default()
stac_io.headers = {"Authorization": "<some-auth-header>"}

catalog = Catalog.from_file("<URI-requiring-auth>", stac_io=stac_io)


If you require more custom logic for I/O operations or would like to use a
3rd-party library for I/O operations (e.g. ``requests``),
you can create a sub-class of :class:`pystac.StacIO`
(or :class:`pystac.DefaultStacIO`) and customize the methods as
you see fit. You can then pass instances of this custom sub-class into the ``stac_io``
argument of most object-specific I/O methods. You can also use
:meth:`pystac.StacIO.set_default` in your client's ``__init__.py`` file to make this
Expand Down
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
16 changes: 16 additions & 0 deletions tests/test_stac_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import unittest
import tempfile
Expand Down Expand Up @@ -98,3 +99,18 @@ class ReportingStacIO(DefaultStacIO, DuplicateKeyReportingMixin):
str(excinfo.exception),
f'Found duplicate object name "key" in {src_href}',
)

@unittest.mock.patch("pystac.stac_io.urlopen")
def test_headers_stac_io(self, urlopen_mock: unittest.mock.MagicMock) -> None:
stac_io = DefaultStacIO(headers={"Authorization": "api-key fake-api-key-value"})

catalog = pystac.Catalog("an-id", "a description").to_dict()
# required until https://github.com/stac-utils/pystac/pull/896 is merged
catalog["links"] = []
urlopen_mock.return_value.__enter__.return_value.read.return_value = json.dumps(
catalog
).encode("utf-8")
pystac.Catalog.from_file("https://example.com/catalog.json", stac_io=stac_io)

request_obj = urlopen_mock.call_args[0][0]
self.assertEqual(request_obj.headers, stac_io.headers)