File tree Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -259,9 +259,25 @@ created automatically by all of the object-specific I/O methods (e.g.
259
259
:meth: `pystac.Catalog.from_file `), so most users will not need to instantiate this
260
260
class themselves.
261
261
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
265
281
you see fit. You can then pass instances of this custom sub-class into the ``stac_io ``
266
282
argument of most object-specific I/O methods. You can also use
267
283
:meth: `pystac.StacIO.set_default ` in your client's ``__init__.py `` file to make this
Original file line number Diff line number Diff line change @@ -98,3 +98,19 @@ class ReportingStacIO(DefaultStacIO, DuplicateKeyReportingMixin):
98
98
str (excinfo .exception ),
99
99
f'Found duplicate object name "key" in { src_href } ' ,
100
100
)
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 )
You can’t perform that action at this time.
0 commit comments