Skip to content

Commit c93e074

Browse files
committed
Don't clip to source if there is a configured clip tool
Also temporarily pin click to < 8.1.4
1 parent 6019e4b commit c93e074

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

planet/subscription_request.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def build_request(name: str,
5050
notifications: Optional[Mapping] = None,
5151
tools: Optional[List[Mapping]] = None,
5252
clip_to_source=False) -> dict:
53-
"""Prepare a subscription request.
53+
"""Construct a Subscriptions API request.
5454
5555
The return value can be passed to
5656
[planet.clients.subscriptions.SubscriptionsClient.create_subscription][].
@@ -76,6 +76,10 @@ def build_request(name: str,
7676
A Python dict representation of a Subscriptions API request for
7777
a new subscription.
7878
79+
Raises:
80+
ClientError when a valid Subscriptions API request can't be
81+
constructed.
82+
7983
Examples:
8084
```python
8185
>>> from datetime import datetime
@@ -117,18 +121,22 @@ def build_request(name: str,
117121

118122
# If clip_to_source is True a clip configuration will be added
119123
# to the list of requested tools unless an existing clip tool
120-
# exists. NOTE: the next version of the Subscription API will
121-
# remove the clip tool option and always clip to the source
122-
# geometry. Thus this is a preview of the next API version's
123-
# default behavior.
124-
if clip_to_source and not any(
125-
tool.get('type', None) == 'clip' for tool in tool_list):
126-
tool_list.append({
127-
'type': 'clip',
128-
'parameters': {
129-
'aoi': source['parameters']['geometry']
130-
}
131-
})
124+
# exists. In that case an exception is raised. NOTE: the next
125+
# version of the Subscription API will remove the clip tool
126+
# option and always clip to the source geometry. Thus this is a
127+
# preview of the next API version's default behavior.
128+
if clip_to_source:
129+
if any(tool.get('type', None) == 'clip' for tool in tool_list):
130+
raise ClientError(
131+
"clip_to_source option conflicts with a configured clip tool."
132+
)
133+
else:
134+
tool_list.append({
135+
'type': 'clip',
136+
'parameters': {
137+
'aoi': source['parameters']['geometry']
138+
}
139+
})
132140

133141
details['tools'] = tool_list
134142

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
continue
2525

2626
install_requires = [
27-
'click>=8.0.0',
27+
# click 8.1.4 breaks our mypy check, see
28+
# https://github.com/pallets/click/issues/2558.
29+
'click>8.0,<8.1.4',
2830
'geojson',
2931
'httpx>=0.23.0',
3032
'jsonschema',

tests/unit/test_subscription_request.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def test_build_request_success(geom_geojson):
6565
assert res == expected
6666

6767

68-
def test_build_request_clip_to_source(geom_geojson):
68+
def test_build_request_clip_to_source_success(geom_geojson):
69+
"""Without a clip tool we can clip to source."""
6970
source = {
7071
"type": "catalog",
7172
"parameters": {
@@ -77,7 +78,7 @@ def test_build_request_clip_to_source(geom_geojson):
7778
"asset_types": ["ortho_analytic_4b"]
7879
}
7980
}
80-
res = subscription_request.build_request(
81+
req = subscription_request.build_request(
8182
'test',
8283
source=source,
8384
delivery={},
@@ -86,8 +87,35 @@ def test_build_request_clip_to_source(geom_geojson):
8687
}],
8788
clip_to_source=True,
8889
)
89-
assert res["tools"][1]["type"] == "clip"
90-
assert res["tools"][1]["parameters"]["aoi"] == geom_geojson
90+
assert req["tools"][1]["type"] == "clip"
91+
assert req["tools"][1]["parameters"]["aoi"] == geom_geojson
92+
93+
94+
def test_build_request_clip_to_source_failure(geom_geojson):
95+
"""With a clip tool we can not clip to source."""
96+
source = {
97+
"type": "catalog",
98+
"parameters": {
99+
"geometry": geom_geojson,
100+
"start_time": "2021-03-01T00:00:00Z",
101+
"end_time": "2023-11-01T00:00:00Z",
102+
"rrule": "FREQ=MONTHLY;BYMONTH=3,4,5,6,7,8,9,10",
103+
"item_types": ["PSScene"],
104+
"asset_types": ["ortho_analytic_4b"]
105+
}
106+
}
107+
with pytest.raises(exceptions.ClientError):
108+
subscription_request.build_request(
109+
'test',
110+
source=source,
111+
delivery={},
112+
tools=[{
113+
'type': 'clip'
114+
}, {
115+
'type': 'hammer'
116+
}],
117+
clip_to_source=True,
118+
)
91119

92120

93121
def test_catalog_source_success(geom_geojson):

0 commit comments

Comments
 (0)