Skip to content

Commit c7721cc

Browse files
authored
Merge pull request #859 from planetlabs/subscription-request-cli
Implement subscription request helper cli commands
2 parents e90f3e2 + 26e30c1 commit c7721cc

File tree

3 files changed

+155
-27
lines changed

3 files changed

+155
-27
lines changed

design-docs/CLI-Subscriptions.md

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ planet subscriptions request [OPTIONS]
184184
185185
Generate a subscriptions request.
186186
187-
This command provides support for building the subscription request JSON used to create or
187+
This command provides support for building the subscription request JSON used to create or
188188
update a subscription. It outputs the subscription request.
189189
190190
Options:
@@ -207,7 +207,10 @@ Options:
207207
### Usage Examples
208208

209209
```
210-
planet subscription request --source source.json --clip geom.json --delivery delivery.json | planet subscriptions create -
210+
planet subscription request \
211+
--name test \
212+
--source source.json \
213+
--delivery delivery.json | planet subscriptions create -
211214
```
212215

213216
## Request-catalog
@@ -222,27 +225,26 @@ Generate a subscriptions request source JSON for a catalog.
222225
Options:
223226
--asset-types TEXT One or more comma-separated asset types. Required.
224227
--item-types TEXT One or more comma-separated item-types. Required.
225-
--geom JSON geometry of the area of interest of the subscription that will be used to determine matches.
228+
--geometry JSON geometry of the area of interest of the subscription that will be used to determine matches.
226229
Can be a json string, filename, or '-' for stdin.
227230
--start-time DATETIME Start date and time to begin subscription.
228231
--end-time DATETIME Date and time to end the subscription.
229-
--rrule TEXT iCalendar recurrance rule to specify recurrances.
230-
--filter JSON A search filter can be specified a json string,
231-
filename, or '-' for stdin.
232+
--rrule TEXT iCalendar recurrance rule to specify recurrances.
233+
--filter JSON A search filter can be specified a json string,
234+
filename, or '-' for stdin.
232235
--pretty Format JSON output.
233236
--help Show this message and exit.
234237
```
235238

236239
### Usage Examples
237240

238241
```
239-
planet subscriptions request \
240-
--source $(planet subscriptions request-catalog
241-
--item-type PSScene
242-
--asset-types ortho_analytic_8b_sr,ortho_udm2
243-
--geom aoi.json
244-
--start-time 05/01/2022)
245-
--delivery delivery.json | planet subscriptions create -
242+
planet subscriptions request-catalog \
243+
--item-types PSScene \
244+
--asset-types ortho_analytic_8b_sr,ortho_udm2 \
245+
--geometry aoi.geojson \
246+
--start-time 2022-01-01) \
247+
--delivery delivery.json > source.json
246248
```
247249

248250
## Request-other
@@ -257,7 +259,7 @@ Generate a subscriptions request source JSON for another product.
257259
Options:
258260
--type Type.
259261
--id Id.
260-
--geom JSON geometry of the area of interest of the subscription that will be used to determine matches.
262+
--geometry JSON geometry of the area of interest of the subscription that will be used to determine matches.
261263
Can be a json string, filename, or '-' for stdin.
262264
--start-time DATETIME Start date and time to begin subscription.
263265
--end-time DATETIME Date and time to end the subscription.
@@ -266,18 +268,6 @@ Options:
266268
--help Show this message and exit.
267269
```
268270

269-
### Usage Examples
270-
271-
```
272-
planet subscriptions request \
273-
--source $(planet subscriptions request-other
274-
--type othertype
275-
--id otherid
276-
--geom aoi.json
277-
--start-time 05/01/2022)
278-
--delivery delivery.json | planet subscriptions create -
279-
```
280-
281271

282272
## Update
283273

planet/cli/subscriptions.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .options import limit, pretty
1010
from .session import CliSession
1111
from planet.clients.subscriptions import SubscriptionsClient
12+
from .. import subscription_request
1213

1314

1415
@asynccontextmanager
@@ -157,3 +158,84 @@ async def list_subscription_results_cmd(ctx,
157158
status=status,
158159
limit=limit):
159160
echo_json(result, pretty)
161+
162+
163+
@subscriptions.command()
164+
@click.option('--name',
165+
required=True,
166+
type=str,
167+
help='Subscription name. Does not need to be unique.')
168+
@click.option('--source',
169+
required=True,
170+
type=types.JSON(),
171+
help='Source JSON. Can be a string, filename, or - for stdin.')
172+
@click.option('--delivery',
173+
required=True,
174+
type=types.JSON(),
175+
help='Delivery JSON. Can be a string, filename, or - for stdin.')
176+
@click.option(
177+
'--notifications',
178+
type=types.JSON(),
179+
help='Notifications JSON. Can be a string, filename, or - for stdin.')
180+
@click.option('--tools',
181+
type=types.JSON(),
182+
help='Toolchain JSON. Can be a string, filename, or - for stdin.'
183+
)
184+
@pretty
185+
def request(name, source, delivery, notifications, tools, pretty):
186+
"""Generate a subscriptions request."""
187+
res = subscription_request.build_request(name,
188+
source,
189+
delivery,
190+
notifications=notifications,
191+
tools=tools)
192+
echo_json(res, pretty)
193+
194+
195+
@subscriptions.command()
196+
@click.option('--item-types',
197+
required=True,
198+
type=types.CommaSeparatedString(),
199+
help='One or more comma-separated item types.')
200+
@click.option('--asset-types',
201+
required=True,
202+
type=types.CommaSeparatedString(),
203+
help='One or more comma-separated asset types.')
204+
@click.option(
205+
'--geometry',
206+
required=True,
207+
type=types.JSON(),
208+
help="""Geometry of the area of interest of the subscription that will be
209+
used to determine matches. Can be a string, filename, or - for stdin.""")
210+
@click.option('--start-time',
211+
required=True,
212+
type=types.DateTime(),
213+
help='Date and time to begin subscription.')
214+
@click.option('--end-time',
215+
type=types.DateTime(),
216+
help='Date and time to end subscription.')
217+
@click.option('--rrule',
218+
type=str,
219+
help='iCalendar recurrance rule to specify recurrances.')
220+
@click.option('--filter',
221+
type=types.JSON(),
222+
help='Search filter. Can be a string, filename, or - for stdin.'
223+
)
224+
@pretty
225+
def request_catalog(item_types,
226+
asset_types,
227+
geometry,
228+
start_time,
229+
end_time,
230+
rrule,
231+
filter,
232+
pretty):
233+
"""Generate a subscriptions request catalog source description."""
234+
res = subscription_request.catalog_source(item_types,
235+
asset_types,
236+
geometry,
237+
start_time,
238+
end_time=end_time,
239+
rrule=rrule,
240+
filter=filter)
241+
echo_json(res, pretty)

tests/integration/test_subscriptions_cli.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
TODO: tests for 3 options of the planet-subscriptions-results command.
1313
1414
"""
15-
1615
import json
1716

1817
from click.testing import CliRunner
@@ -250,3 +249,60 @@ def test_subscriptions_results_success(invoke, options, expected_count):
250249

251250
assert result.exit_code == 0 # success.
252251
assert result.output.count('"id"') == expected_count
252+
253+
254+
def test_request_base_success(invoke, geom_geojson):
255+
"""Request command succeeds"""
256+
source = json.dumps({
257+
"type": "catalog",
258+
"parameters": {
259+
"geometry": geom_geojson,
260+
"start_time": "2021-03-01T00:00:00Z",
261+
"end_time": "2023-11-01T00:00:00Z",
262+
"rrule": "FREQ=MONTHLY;BYMONTH=3,4,5,6,7,8,9,10",
263+
"item_types": ["PSScene"],
264+
"asset_types": ["ortho_analytic_4b"]
265+
}
266+
})
267+
delivery = json.dumps({
268+
"type": "amazon_s3",
269+
"parameters": {
270+
"aws_access_key_id": "keyid",
271+
"aws_secret_access_key": "accesskey",
272+
"bucket": "bucket",
273+
"aws_region": "region"
274+
}
275+
})
276+
277+
result = invoke([
278+
'request',
279+
'--name=test',
280+
f'--source={source}',
281+
f'--delivery={delivery}'
282+
])
283+
284+
assert source in result.output
285+
assert result.exit_code == 0 # success.
286+
287+
288+
def test_request_catalog_success(invoke, geom_geojson):
289+
"""Request-catalog command succeeds"""
290+
source = {
291+
"type": "catalog",
292+
"parameters": {
293+
"geometry": geom_geojson,
294+
"start_time": "2021-03-01T00:00:00Z",
295+
"item_types": ["PSScene"],
296+
"asset_types": ["ortho_analytic_4b"]
297+
}
298+
}
299+
300+
result = invoke([
301+
'request-catalog',
302+
'--item-types=PSScene',
303+
'--asset-types=ortho_analytic_4b',
304+
f"--geometry={json.dumps(geom_geojson)}",
305+
'--start-time=2021-03-01T00:00:00'
306+
])
307+
assert json.loads(result.output) == source
308+
assert result.exit_code == 0 # success.

0 commit comments

Comments
 (0)