-
Notifications
You must be signed in to change notification settings - Fork 98
Let Session get clients by name #867
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
Changes from all commits
3623413
577e22a
5882a44
7d13359
67f2116
ca11ada
6c88b93
a6d0ab5
69eb2c3
0762454
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |
| """ | ||
| import asyncio | ||
|
|
||
| from planet import Session, DataClient | ||
| from planet import Session | ||
|
|
||
| river_item_id = '20221003_002705_38_2461' | ||
| river_item_type = 'PSScene' | ||
|
|
@@ -33,7 +33,8 @@ | |
| wildfire_asset_type = 'basic_analytic_4b' | ||
|
|
||
|
|
||
| async def download_and_validate(item_id, item_type_id, asset_type_id, client): | ||
| async def download_and_validate(client, item_id, item_type_id, asset_type_id): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the positioning of |
||
| """Activate, download, and validate an asset as a single task.""" | ||
| # Get asset description | ||
| asset = await client.get_asset(item_type_id, item_id, asset_type_id) | ||
|
|
||
|
|
@@ -51,19 +52,18 @@ async def download_and_validate(item_id, item_type_id, asset_type_id, client): | |
|
|
||
|
|
||
| async def main(): | ||
| """Download and validate assets in parallel.""" | ||
| async with Session() as sess: | ||
| # Data client object | ||
| client = DataClient(sess) | ||
| # Download and validate assets in parallel | ||
| client = sess.client('data') | ||
| await asyncio.gather( | ||
| download_and_validate(river_item_id, | ||
| download_and_validate(client, | ||
| river_item_id, | ||
| river_item_type, | ||
| river_asset_type, | ||
| client), | ||
| download_and_validate(wildfire_item_id, | ||
| river_asset_type), | ||
| download_and_validate(client, | ||
| wildfire_item_id, | ||
| wildfire_item_type, | ||
| wildfire_asset_type, | ||
| client)) | ||
| wildfire_asset_type)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ | |
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we get rid of the 2020 copyright on line 1?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, according to @cholmes investigation, we just add a line above |
||
| DOWNLOAD_DIR = os.getenv('TEST_DOWNLOAD_DIR', '.') | ||
|
|
||
| # The Orders API will be asked to mask, or clip, results to | ||
| # this area of interest. | ||
| iowa_aoi = { | ||
| "type": | ||
| "Polygon", | ||
|
|
@@ -36,11 +38,18 @@ | |
| [-91.198465, 42.893071]]] | ||
| } | ||
|
|
||
| iowa_images = ['20200925_161029_69_2223', '20200925_161027_48_2223'] | ||
sgillies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # In practice, you will use a Data API search to find items, but | ||
| # for this example take them as given. | ||
| iowa_items = ['20200925_161029_69_2223', '20200925_161027_48_2223'] | ||
|
|
||
| iowa_order = planet.order_request.build_request( | ||
| 'iowa_order', | ||
| [planet.order_request.product(iowa_images, 'analytic_udm2', 'PSScene')], | ||
| tools=[planet.order_request.clip_tool(iowa_aoi)]) | ||
| name='iowa_order', | ||
| products=[ | ||
| planet.order_request.product(item_ids=iowa_items, | ||
| product_bundle='analytic_udm2', | ||
| item_type='PSScene') | ||
| ], | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using product()'s keyword arguments makes the calls more understandable and the whole example more instructive.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes it much more readable, thank you! |
||
| tools=[planet.order_request.clip_tool(aoi=iowa_aoi)]) | ||
|
|
||
| oregon_aoi = { | ||
| "type": | ||
|
|
@@ -50,33 +59,34 @@ | |
| [-117.558734, 45.229745]]] | ||
| } | ||
|
|
||
| oregon_images = ['20200909_182525_1014', '20200909_182524_1014'] | ||
| oregon_items = ['20200909_182525_1014', '20200909_182524_1014'] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoopsie, the |
||
|
|
||
| oregon_order = planet.order_request.build_request( | ||
| 'oregon_order', | ||
| [planet.order_request.product(oregon_images, 'analytic_udm2', 'PSScene')], | ||
| tools=[planet.order_request.clip_tool(oregon_aoi)]) | ||
| name='oregon_order', | ||
| products=[ | ||
| planet.order_request.product(item_ids=oregon_items, | ||
| product_bundle='analytic_udm2', | ||
| item_type='PSScene') | ||
| ], | ||
| tools=[planet.order_request.clip_tool(aoi=oregon_aoi)]) | ||
|
|
||
|
|
||
| async def create_and_download(order_detail, directory, client): | ||
| async def create_and_download(client, order_detail, directory): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the client argument to the head of the list to make the signature of this function more like that of client.download_order(), where client (aka |
||
| """Make an order, wait for completion, download files as a single task.""" | ||
| with planet.reporting.StateBar(state='creating') as reporter: | ||
| # create | ||
| order = await client.create_order(order_detail) | ||
| reporter.update(state='created', order_id=order['id']) | ||
|
|
||
| # wait for completion | ||
| await client.wait(order['id'], callback=reporter.update_state) | ||
|
|
||
| # download | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed obvious comments and rolled them up into a docstring.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's do the same thing for the data example. |
||
| await client.download_order(order['id'], directory, progress_bar=True) | ||
|
|
||
|
|
||
| async def main(): | ||
| async with planet.Session() as ps: | ||
| client = planet.OrdersClient(ps) | ||
|
|
||
| async with planet.Session() as sess: | ||
| client = sess.client('orders') | ||
| await asyncio.gather( | ||
| create_and_download(iowa_order, DOWNLOAD_DIR, client), | ||
| create_and_download(oregon_order, DOWNLOAD_DIR, client), | ||
| create_and_download(client, iowa_order, DOWNLOAD_DIR), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
| create_and_download(client, oregon_order, DOWNLOAD_DIR), | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """Session module tests.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from planet import DataClient, OrdersClient, SubscriptionsClient, Session | ||
| from planet.exceptions import ClientError | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("client_name,client_class", | ||
| [('data', DataClient), ('orders', OrdersClient), | ||
| ('subscriptions', SubscriptionsClient)]) | ||
| async def test_session_get_client(client_name, client_class): | ||
| """Get a client from a session.""" | ||
| async with Session() as session: | ||
| client = session.client(client_name) | ||
| assert isinstance(client, client_class) | ||
|
|
||
|
|
||
| async def test_session_get_client_error(): | ||
| """Get an exception when no such client exists.""" | ||
| async with Session() as session: | ||
| with pytest.raises(ClientError): | ||
| _ = session.client('bogus') |
Uh oh!
There was an error while loading. Please reload this page.