Skip to content

Commit 3656b6d

Browse files
author
Tom Augspurger
authored
API Updates (#19)
* API Updates * Added `sign_*` to the public API, for users wishing to bypass the overhead of `sign` * Restored `sign_assets` with a deprecation * Added a changelog
1 parent d928a1e commit 3656b6d

4 files changed

Lines changed: 67 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 0.3.0
2+
3+
## New Features
4+
5+
* `sign` now works on strings, `pystac.Item`, `pystac.Asset`, `pystac.ItemCollection`, and `pystac_client.ItemSearch` instances.
6+
* Added top-level methods `sign_item`, `sign_asset`, and `sign_item_collection` to directly sign objects of those types.
7+
8+
## Deprecations
9+
10+
* `sign_assets` is deprecated. Use `sign_item` instead.
11+
12+
## Bug Fixes
13+
14+
* `sign_item` now handles items with assets containing links to files outside of blob storage by returning the asset unchanged.

planetary_computer/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
"""Planetary Computer Python SDK"""
22
# flake8:noqa
33

4-
from planetary_computer.sas import sign # type:ignore
5-
from planetary_computer.settings import set_subscription_key # type:ignore
4+
from planetary_computer.sas import (
5+
sign,
6+
sign_url,
7+
sign_item,
8+
sign_assets,
9+
sign_asset,
10+
sign_item_collection,
11+
)
12+
from planetary_computer.settings import set_subscription_key
13+
14+
15+
__all__ = [
16+
"set_subscription_key",
17+
"sign_asset",
18+
"sign_assets",
19+
"sign_item_collection",
20+
"sign_item",
21+
"sign_url",
22+
"sign",
23+
]

planetary_computer/sas.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import datetime, timezone
22
from typing import Any, Dict
3+
import warnings
34

45
from functools import singledispatch
56
from urllib.parse import urlparse
@@ -72,7 +73,7 @@ def sign(obj: Any) -> Any:
7273

7374

7475
@sign.register(str)
75-
def _sign_url(url: str) -> str:
76+
def sign_url(url: str) -> str:
7677
"""Sign a URL with a Shared Access (SAS) Token, which allows for read access.
7778
7879
Args:
@@ -111,7 +112,7 @@ def _sign_url(url: str) -> str:
111112

112113

113114
@sign.register(Item)
114-
def _sign_item(item: Item) -> Item:
115+
def sign_item(item: Item) -> Item:
115116
"""Sign all assets within a PySTAC item
116117
117118
Args:
@@ -130,7 +131,7 @@ def _sign_item(item: Item) -> Item:
130131

131132

132133
@sign.register(Asset)
133-
def _sign_asset(asset: Asset) -> Asset:
134+
def sign_asset(asset: Asset) -> Asset:
134135
"""Sign a PySTAC asset
135136
136137
Args:
@@ -145,8 +146,18 @@ def _sign_asset(asset: Asset) -> Asset:
145146
return signed_asset
146147

147148

149+
def sign_assets(item: Item) -> Item:
150+
warnings.warn(
151+
"'sign_assets' is deprecated and will be removed in a future version. Use "
152+
"'sign_item' instead.",
153+
FutureWarning,
154+
stacklevel=2,
155+
)
156+
return sign_item(item)
157+
158+
148159
@sign.register(ItemCollection)
149-
def _sign_item_collection(item_collection: ItemCollection) -> ItemCollection:
160+
def sign_item_collection(item_collection: ItemCollection) -> ItemCollection:
150161
"""Sign a PySTAC item collection
151162
152163
Args:

tests/test_signing.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,21 @@ def test_search_and_sign(self) -> None:
104104
self.assertEqual(len(list(signed_item_collection)), 1)
105105
for signed_item in signed_item_collection:
106106
self.verify_signed_urls_in_item(signed_item)
107+
108+
def test_sign_assets_deprecated(self) -> None:
109+
item = get_sample_item()
110+
with self.assertWarns(FutureWarning):
111+
pc.sign_assets(item)
112+
113+
def test_public_api(self) -> None:
114+
item = get_sample_item()
115+
116+
self.assertEqual(type(pc.sign(item)), type(pc.sign_item(item)))
117+
self.assertEqual(
118+
type(pc.sign(item.assets["image"])),
119+
type(pc.sign_asset(item.assets["image"])),
120+
)
121+
self.assertEqual(
122+
type(pc.sign(item.assets["image"].href)),
123+
type(pc.sign_url(item.assets["image"].href)),
124+
)

0 commit comments

Comments
 (0)