Skip to content

Commit 92a9956

Browse files
john-dupuygadomski
authored andcommitted
Add a unit test and docs for the feature
1 parent 01f71b1 commit 92a9956

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

docs/concepts.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,25 @@ created automatically by all of the object-specific I/O methods (e.g.
259259
:meth:`pystac.Catalog.from_file`), so most users will not need to instantiate this
260260
class themselves.
261261

262-
If you require custom logic for I/O operations or would like to use a 3rd-party library
263-
for I/O operations (e.g. ``requests``), you can create a sub-class of
264-
:class:`pystac.StacIO` (or :class:`pystac.DefaultStacIO`) and customize the methods as
262+
If you are dealing with a STAC catalog with URIs that require authentication.
263+
It is possible provide auth headers (or any other customer headers) to the
264+
:class:`pystac.stac_io.DefaultStacIO`.
265+
266+
.. code-block:: python
267+
268+
from pystac import Catalog
269+
from pystac import StacIO
270+
271+
stac_io = StacIO.default()
272+
stac_io.headers = {"Authorization": "<some-auth-header>"}
273+
274+
catalog = Catalog.from_file("<URI-requiring-auth>", stac_io=stac_io)
275+
276+
277+
If you require more custom logic for I/O operations or would like to use a
278+
3rd-party library for I/O operations (e.g. ``requests``),
279+
you can create a sub-class of :class:`pystac.StacIO`
280+
(or :class:`pystac.DefaultStacIO`) and customize the methods as
265281
you see fit. You can then pass instances of this custom sub-class into the ``stac_io``
266282
argument of most object-specific I/O methods. You can also use
267283
:meth:`pystac.StacIO.set_default` in your client's ``__init__.py`` file to make this

tests/test_stac_io.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,19 @@ class ReportingStacIO(DefaultStacIO, DuplicateKeyReportingMixin):
9898
str(excinfo.exception),
9999
f'Found duplicate object name "key" in {src_href}',
100100
)
101+
102+
@unittest.mock.patch("pystac.stac_io.urlopen")
103+
def test_headers_stac_io(self, urlopen_mock: unittest.mock.MagicMock) -> None:
104+
stac_io = DefaultStacIO(headers={"Authorization": "api-key fake-api-key-value"})
105+
106+
try:
107+
# note we don't care if this raises an exception, we just want to make
108+
# sure urlopen was called with the appropriate headers
109+
pystac.Catalog.from_file(
110+
"https://example.com/catalog.json", stac_io=stac_io
111+
)
112+
except Exception:
113+
pass
114+
115+
request_obj = urlopen_mock.call_args[0][0]
116+
self.assertEqual(request_obj.headers, stac_io.headers)

0 commit comments

Comments
 (0)