Skip to content
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

have planet orders request take in a series of item descriptions to create an order #944

Open
jreiberkyle opened this issue May 4, 2023 · 1 comment
Labels
2024-revisit SDK committee reviewed and marked for follow-up CLI/SDK Interface Update the CLI and SDK to the finalized interface proposal

Comments

@jreiberkyle
Copy link
Contributor

jreiberkyle commented May 4, 2023

To support the common use case of creating an order from a search, have planet orders request take in a series of item descriptions to create an order.

Usage:

planet data search --limit 5 PSScene | planet orders request - --name test --clip aoi.geojson --bundle analytic

Implementation notes: ids and item type are taken from the input. There is a check for more than one item type, which at this point results in a failure (the ability to order multiple types could come in a future improvement)

Changes required:

  • planet order requests must now allow multiline input where each line is json in addition to ids as the argument
  • item-type is only required if the input is IDs

Below is a working example of extracting item type and id from the output of planet data search.

$> planet data search --limit 5 PSScene | python ml.py --items -
PSScene: 20230504_155548_86_24b9
PSScene: 20230504_134550_99_2251
PSScene: 20230504_052537_02_2465
PSScene: 20230504_043314_80_248b
PSScene: 20230504_161301_74_2405

Note: MultiJSON is a hacked up version of JSON, which is in the cli/types.py module. There is more work here in defining a type that can handle both comma separated strings and multiple lines of json.

ml.py

import json

import click


class MultiJSON(click.ParamType):
    name = 'MultiJSON'

    def convert(self, value, param, ctx) -> list:
        if isinstance(value, list):
            conv = value
        else:
            # read from raw json
            # skip this if value is a Path object or something else
            if isinstance(value, str) and (value.startswith('{')
                                           or value.startswith('[')):
                print('hereh')
                try:
                    conv = json.loads(value)
                    if isinstance(conv, dict):
                        conv = [conv]

                except json.decoder.JSONDecodeError:
                    self.fail(
                        'JSON string is not valid JSON. Make sure the entire '
                        'JSON string is single-quoted and string entries are '
                        'double-quoted.')

            # read from stdin or file
            else:
                try:
                    with click.open_file(value) as f:
                        conv = [json.loads(line) for line in f if line]
                except FileNotFoundError:
                    self.fail('File not found.')
                except json.decoder.JSONDecodeError:
                    stream = 'stdin' if value == '-' else 'JSON file'
                    self.fail(f'{stream} does not contain valid JSON.')

        if not len(conv):
            self.fail('JSON cannot be empty.')

        return conv

@click.command()
@click.option('--items', type=MultiJSON(), required=True)
def hello(items):
    for i in items:
        click.echo(f"{i['properties']['item_type']}: {i['id']}")


if __name__ == '__main__':
    hello()
@tbarsballe tbarsballe added this to the 2.1: Major improvements milestone Aug 22, 2024
@tbarsballe tbarsballe added CLI/SDK Interface Update the CLI and SDK to the finalized interface 2024-revisit SDK committee reviewed and marked for follow-up labels Aug 22, 2024
@tbarsballe
Copy link
Contributor

This approach is useful, though we should alter it to function at the SDK level, not just the CLI level

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2024-revisit SDK committee reviewed and marked for follow-up CLI/SDK Interface Update the CLI and SDK to the finalized interface proposal
Projects
None yet
Development

No branches or pull requests

2 participants