-
Notifications
You must be signed in to change notification settings - Fork 9
add cli with 5 commands: product, products, order, orders, opportunities #88
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
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf76aad
add cli with 5 commands: product, products, order, orders, opportunities
phil-osk bfdd154
Merge branch 'main' into main
phil-osk 11b9fc6
Merge branch 'main' into main
phil-osk d44efe0
simpler json output, send errors to err
phil-osk a2dbc44
reformat for linter
phil-osk 0fb8a1d
Update pyproject.toml
gadomski b37bc4a
Update pystapi-client/pyproject.toml
gadomski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import itertools | ||
| import json | ||
|
|
||
| import click | ||
|
|
||
| from pystapi_client.client import Client | ||
| from pystapi_client.exceptions import APIError | ||
|
|
||
| CONTEXT_SETTINGS = dict(default_map={"cli": {"url": "http://localhost:8000"}}) | ||
|
|
||
|
|
||
| @click.group(context_settings=CONTEXT_SETTINGS) | ||
| @click.option("--url", type=str, required=True, help="Base URL for STAPI server") | ||
| @click.pass_context | ||
| def cli(ctx: click.Context, url: str) -> None: | ||
| """Command line interface for STAPI client. Group ensures client is created.""" | ||
|
|
||
| client = Client.open(url) | ||
| ctx.obj = {"client": client} | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.pass_context | ||
| @click.option("--max-items", "max_items", type=click.IntRange(min=1), help="Max number of products to display") | ||
| @click.option("--limit", type=click.IntRange(min=1), help="Limit number of products to request") | ||
| def products(ctx: click.Context, limit: int | None, max_items: int | None) -> None: | ||
| """List products.""" | ||
|
|
||
| client: Client = ctx.obj["client"] | ||
|
|
||
| products_iter = client.get_products(limit=limit) | ||
|
|
||
| if max_items: | ||
| products_iter = itertools.islice(products_iter, max_items) | ||
|
|
||
| products_list = list(products_iter) | ||
| if len(products_list) == 0: | ||
| click.echo("No products found.") | ||
| return | ||
|
|
||
| # FIXME: to get around AnyUrl not being JSON serializable, this does loads(pydantic.model_dump_json()). Should be | ||
| # fixed with a custom JSON serializer for AnyUrl. | ||
| click.echo(json.dumps([json.loads(p.model_dump_json()) for p in products_list])) | ||
phil-osk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @click.command() | ||
| @click.pass_context | ||
| @click.option("--id", type=str, required=True, help="Product ID to retrieve") | ||
| def product(ctx: click.Context, id: str) -> None: | ||
| """Get product by ID.""" | ||
|
|
||
| client: Client = ctx.obj["client"] | ||
|
|
||
| product = client.get_product(product_id=id) | ||
| if not product: | ||
| click.echo("Product not found.") | ||
| return | ||
|
|
||
| click.echo(product.model_dump_json()) | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.pass_context | ||
| @click.option("--max-items", "max_items", type=click.IntRange(min=1), help="Max number of products to display") | ||
| @click.option("--limit", type=click.IntRange(min=1), help="Limit number of products to request") | ||
| def orders(ctx: click.Context, max_items: int | None, limit: int | None) -> None: | ||
| """List orders.""" | ||
|
|
||
| client: Client = ctx.obj["client"] | ||
|
|
||
| orders_iter = client.get_orders(limit=limit) | ||
|
|
||
| if max_items: | ||
| orders_iter = itertools.islice(orders_iter, max_items) | ||
|
|
||
| orders_list = list(orders_iter) | ||
| if len(orders_list) == 0: | ||
| click.echo("No orders found.") | ||
| return | ||
|
|
||
| # FIXME: to get around AnyUrl not being JSON serializable, this does loads(pydantic.model_dump_json()). Should be | ||
| # fixed with a custom JSON serializer for AnyUrl. | ||
| click.echo(json.dumps([json.loads(o.model_dump_json()) for o in orders_list])) | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.pass_context | ||
| @click.option("--id", type=str, required=True, help="Order ID to retrieve") | ||
| def order(ctx: click.Context, id: str) -> None: | ||
| """Get order by ID.""" | ||
|
|
||
| client: Client = ctx.obj["client"] | ||
|
|
||
| try: | ||
| order = client.get_order(order_id=id) | ||
| click.echo(order.model_dump_json()) | ||
| except APIError as e: | ||
| if e.status_code == 404: | ||
| click.echo("Order not found.") | ||
| else: | ||
| raise e | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.pass_context | ||
| @click.option("--product-id", "product_id", type=str, required=True, help="Product ID for opportunities") | ||
| @click.option("--max-items", "max_items", type=click.IntRange(min=1), help="Max number of opportunities to display") | ||
| @click.option("--limit", type=click.IntRange(min=1), default=10, help="Max number of opportunities to display") | ||
| def opportunities(ctx: click.Context, product_id: str, limit: int, max_items: None) -> None: | ||
| """List opportunities for a product.""" | ||
|
|
||
| client: Client = ctx.obj["client"] | ||
|
|
||
| date_range = ("2025-01-03T15:18:11Z", "2025-04-03T15:18:11Z") | ||
| geometry = {"type": "Point", "coordinates": [-122.4194, 37.7749]} | ||
|
|
||
| opportunities_iter = client.get_product_opportunities( | ||
| product_id=product_id, geometry=geometry, date_range=date_range, limit=limit | ||
| ) | ||
|
|
||
| if max_items: | ||
| opportunities_iter = itertools.islice(opportunities_iter, max_items) | ||
|
|
||
| opportunities_list = list(opportunities_iter) | ||
| if len(opportunities_list) == 0: | ||
| click.echo("No opportunities found.") | ||
| return | ||
|
|
||
| # FIXME: to get around AnyUrl not being JSON serializable, this does loads(pydantic.model_dump_json()). Should be | ||
| # fixed with a custom JSON serializer for AnyUrl. | ||
| click.echo(json.dumps([json.loads(o.model_dump_json()) for o in opportunities_list])) | ||
|
|
||
|
|
||
| cli.add_command(products) | ||
| cli.add_command(product) | ||
| cli.add_command(opportunities) | ||
| cli.add_command(orders) | ||
| cli.add_command(order) | ||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| cli() | ||
| except Exception as e: | ||
| click.echo(f"Error: {e=}", err=True) | ||
| raise e | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.