Skip to content

Get supported bundles for a given item_type #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9ff679f
Added function to get supported bundles for a given item_type
kevinlacaille Aug 24, 2022
165888c
Now validates supported product bundles with item_type.
kevinlacaille Sep 21, 2022
c2161a6
Now validates supported product bundles with item_type.
kevinlacaille Sep 21, 2022
5b1b0ec
Moved validation of supported bundles into validate_bundle
kevinlacaille Sep 21, 2022
01a6036
Moved validation of supported bundles into validate_bundle
kevinlacaille Sep 21, 2022
755e1ee
Added tests for new validation bundle function.
kevinlacaille Sep 21, 2022
5390410
Reformatted tests for validate_bundle and validate_item_type for new …
kevinlacaille Sep 21, 2022
75e6b28
linting
kevinlacaille Sep 21, 2022
800cbc7
testing
kevinlacaille Sep 22, 2022
dd38950
linting again
kevinlacaille Sep 22, 2022
ac9662e
Allow get_product_bundle to accept item_type
kevinlacaille Sep 22, 2022
6df0acd
Reordered options.
kevinlacaille Sep 22, 2022
e5a8a4a
Made item type case insensitive.
kevinlacaille Sep 22, 2022
4de98d6
Added functionality to show valid bundles with item type in CLI
kevinlacaille Sep 22, 2022
4150665
Reformatted error message to match click.Choice. Reordered inputs.
kevinlacaille Sep 22, 2022
b34c74b
Fixed silly return none bug
kevinlacaille Sep 22, 2022
ef3856a
Renamed data CLI callback function tests.
kevinlacaille Sep 22, 2022
4312c3a
Fixed assersion message
kevinlacaille Sep 22, 2022
ec3f864
Added tests for invalid and incompatible bundles.
kevinlacaille Sep 22, 2022
b9e6387
Added a new test for product bundle with item type.
kevinlacaille Sep 22, 2022
a682566
Added test for stash_item_type
kevinlacaille Sep 22, 2022
dd86962
Added tests for bunde_cb.
kevinlacaille Sep 23, 2022
532ffc0
Merge branch 'main' into compatible_bundle_with_item_type_error_2
kevinlacaille Sep 23, 2022
f739198
More limited assertion on ctx.obj
kevinlacaille Sep 23, 2022
f5b497d
Updated docstring and function name for test_bundle test
kevinlacaille Sep 23, 2022
0e4d9d0
Updated docstring and function name for test_bundle test
kevinlacaille Sep 23, 2022
03409b0
More explicit with bundle_cb tests.
kevinlacaille Sep 23, 2022
283af9a
Removed unused tests.
kevinlacaille Sep 27, 2022
21751ca
Changed item type and bundle to arguments and removed unused function…
kevinlacaille Sep 27, 2022
66a5ace
Changed test for new arguments.
kevinlacaille Sep 27, 2022
64816bb
Merge branch 'main' into compatible_bundle_with_item_type_error_2
kevinlacaille Sep 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions planet/cli/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,26 +225,22 @@ async def create(ctx, request: str, pretty):
@click.pass_context
@translate_exceptions
@coro
@click.argument('item_type',
metavar='ITEM_TYPE',
type=click.Choice(planet.specs.get_item_types(),
case_sensitive=False))
@click.argument('bundle',
metavar='BUNDLE',
type=click.Choice(planet.specs.get_product_bundles(),
case_sensitive=False))
@click.option('--name',
required=True,
help='Order name. Does not need to be unique.',
type=click.STRING)
@click.option(
'--bundle',
multiple=False,
required=True,
help='Product bundle.',
type=click.Choice(planet.specs.get_product_bundles(),
case_sensitive=False),
)
@click.option('--id',
help='One or more comma-separated item IDs.',
type=types.CommaSeparatedString(),
required=True)
@click.option('--item-type',
required=True,
help='Specify an item type',
type=click.STRING)
@click.option('--clip',
type=types.JSON(),
help="""Clip feature GeoJSON. Can be a json string, filename,
Expand All @@ -271,12 +267,12 @@ async def create(ctx, request: str, pretty):
format. Not specifying either defaults to including it (--stac).""")
@pretty
async def request(ctx,
name,
item_type,
bundle,
name,
id,
clip,
tools,
item_type,
email,
cloudconfig,
stac,
Expand Down
9 changes: 5 additions & 4 deletions planet/order_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,13 @@ def product(item_ids: List[str],
are not valid bundles or if item_type is not valid for the given
bundle or fallback bundle.
'''
validated_product_bundle = specs.validate_bundle(product_bundle)
item_type = specs.validate_item_type(item_type, validated_product_bundle)
item_type = specs.validate_item_type(item_type)
validated_product_bundle = specs.validate_bundle(item_type, product_bundle)

if fallback_bundle is not None:
validated_fallback_bundle = specs.validate_bundle(fallback_bundle)
specs.validate_item_type(item_type, validated_fallback_bundle)
item_type = specs.validate_item_type(item_type)
validated_fallback_bundle = specs.validate_bundle(
item_type, fallback_bundle)
validated_product_bundle = ','.join(
[validated_product_bundle, validated_fallback_bundle])

Expand Down
55 changes: 40 additions & 15 deletions planet/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ def __str__(self):
return f'{self.field_name} - \'{self.value}\' is not one of {self.opts}.'


def validate_bundle(bundle):
supported = get_product_bundles()
return _validate_field(bundle, supported, 'product_bundle')
def validate_bundle(item_type, bundle):
all_product_bundles = get_product_bundles()
validate_supported_bundles(item_type, bundle, all_product_bundles)
return _validate_field(bundle, all_product_bundles, 'product_bundle')


def validate_item_type(item_type, bundle):
validated_bundle = validate_bundle(bundle)
supported = get_item_types(validated_bundle)
return _validate_field(item_type, supported, 'item_type')
def validate_item_type(item_type):
supported_item_types = get_item_types()
return _validate_field(item_type, supported_item_types, 'item_type')


def validate_order_type(order_type):
Expand Down Expand Up @@ -92,6 +92,25 @@ def _validate_field(value, supported, field_name):
return value


def validate_supported_bundles(item_type, bundle, all_product_bundles):
spec = _get_product_bundle_spec()

supported_bundles = []
for product_bundle in all_product_bundles:
availible_item_types = set(
spec['bundles'][product_bundle]['assets'].keys())
if item_type.lower() in [x.lower() for x in availible_item_types]:
supported_bundles.append(product_bundle)

return _validate_field(bundle, supported_bundles, 'bundle')


def _get_product_bundle_spec():
with open(DATA_DIR / PRODUCT_BUNDLE_SPEC_NAME) as f:
data = json.load(f)
return data


def get_match(test_entry, spec_entries, field_name):
'''Find and return matching spec entry regardless of capitalization.

Expand All @@ -107,10 +126,22 @@ def get_match(test_entry, spec_entries, field_name):
return match


def get_product_bundles():
def get_product_bundles(item_type=None):
'''Get product bundles supported by Orders API.'''
spec = _get_product_bundle_spec()
return spec['bundles'].keys()

if item_type:
all_product_bundles = get_product_bundles()

supported_bundles = []
for product_bundle in all_product_bundles:
availible_item_types = set(
spec['bundles'][product_bundle]['assets'].keys())
if item_type.lower() in [x.lower() for x in availible_item_types]:
supported_bundles.append(product_bundle)
else:
supported_bundles = spec['bundles'].keys()
return supported_bundles


def get_item_types(product_bundle=None):
Expand All @@ -127,9 +158,3 @@ def get_item_types(product_bundle=None):
for bundle in get_product_bundles()))

return item_types


def _get_product_bundle_spec():
with open(DATA_DIR / PRODUCT_BUNDLE_SPEC_NAME) as f:
data = json.load(f)
return data
66 changes: 42 additions & 24 deletions tests/integration/test_orders_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def stac_json():
def test_cli_orders_list_basic(invoke, order_descriptions):
next_page_url = TEST_ORDERS_URL + '/blob/?page_marker=IAmATest'
order1, order2, order3 = order_descriptions

page1_response = {
"_links": {
"_self": "string", "next": next_page_url
Expand Down Expand Up @@ -462,10 +461,10 @@ def test_cli_orders_request_basic_success(expected_ids,
stac_json):
result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
f'--id={id_string}',
'--bundle=analytic',
'--item-type=PSOrthoTile'
])
assert not result.exception

Expand All @@ -486,24 +485,43 @@ def test_cli_orders_request_basic_success(expected_ids,
def test_cli_orders_request_item_type_invalid(invoke):
result = invoke([
'request',
'invalid'
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=invalid'
])
assert result.exit_code == 2
assert 'Error: Invalid value: item_type' in result.output
assert "Usage: main orders request [OPTIONS] ITEM_TYPE BUNDLE" in result.output


def test_cli_orders_request_id_empty(invoke):
def test_cli_orders_request_product_bundle_invalid(invoke):
result = invoke([
'request',
'PSScene'
'invalid',
'--name=test',
'--id=',
'--bundle=analytic',
'--item-type=PSOrthoTile',
'--id=4500474_2133707_2021-05-20_2419',
])
assert result.exit_code == 2
assert "Usage: main orders request [OPTIONS] ITEM_TYPE BUNDLE" in result.output


def test_cli_orders_request_product_bundle_incompatible(invoke):
result = invoke([
'request',
'PSScene',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
])
assert result.exit_code == 2
assert "Usage: main orders request [OPTIONS] ITEM_TYPE BUNDLE" in result.output


def test_cli_orders_request_id_empty(invoke):
result = invoke(
['request', 'PSOrthoTile', 'analytic', '--name=test', '--id='])
assert result.exit_code == 2
assert 'Entry cannot be an empty string.' in result.output


Expand All @@ -520,10 +538,10 @@ def test_cli_orders_request_clip_success(geom_fixture,

result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=PSOrthoTile',
f'--clip={json.dumps(geom)}',
])
assert result.exit_code == 0
Expand All @@ -550,10 +568,10 @@ def test_cli_orders_request_clip_success(geom_fixture,
def test_cli_orders_request_clip_invalid_geometry(invoke, point_geom_geojson):
result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=PSOrthoTile',
f'--clip={json.dumps(point_geom_geojson)}'
])
assert result.exit_code == 2
Expand All @@ -567,10 +585,10 @@ def test_cli_orders_request_both_clip_and_tools(invoke, geom_geojson):
# option values are valid json
result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=PSOrthoTile',
f'--clip={json.dumps(geom_geojson)}',
f'--tools={json.dumps(geom_geojson)}'
])
Expand All @@ -592,10 +610,10 @@ def test_cli_orders_request_cloudconfig(invoke, stac_json):

result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=PSOrthoTile',
f'--cloudconfig={json.dumps(config_json)}',
])
assert result.exit_code == 0
Expand All @@ -619,10 +637,10 @@ def test_cli_orders_request_cloudconfig(invoke, stac_json):
def test_cli_orders_request_email(invoke, stac_json):
result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=PSOrthoTile',
'--email'
])
assert result.exit_code == 0
Expand Down Expand Up @@ -650,10 +668,10 @@ def test_cli_orders_request_tools(invoke, geom_geojson, stac_json):

result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=PSOrthoTile',
f'--tools={json.dumps(tools_json)}'
])

Expand All @@ -678,10 +696,10 @@ def test_cli_orders_request_no_stac(invoke):

result = invoke([
'request',
'PSOrthoTile',
'analytic',
'--name=test',
'--id=4500474_2133707_2021-05-20_2419',
'--bundle=analytic',
'--item-type=PSOrthoTile',
'--no-stac'
])

Expand Down
File renamed without changes.
Loading