|
1 | 1 | ---
|
2 |
| -title: Upgrading from V1 to V2 |
| 2 | +title: Upgrade from Version 1 to Version 2 |
3 | 3 | ---
|
4 | 4 |
|
5 |
| -The Planet SDK for Python is Version 2 of what was previously referred to as |
6 |
| -the Planet API client. |
| 5 | +The Planet SDK for Python is Version 2 of what was previously referred to as the Planet API client. This V2 is a major update of the V1 SDK. As such, there are major structural changes to how it operates. However, many aspects are still quite similar to V1. Follow this migration guide to upgrade an application developed with V1 of the SDK to V2 with minimal fuss. |
7 | 6 |
|
8 |
| -Version 2 is essentially a complete rewrite and brings with it big changes to |
9 |
| -the Python library. |
| 7 | +## Imports |
10 | 8 |
|
11 |
| -In Version 1, a single client was created for all APIs, |
12 |
| -`client=api.ClientV1(api_key=API_KEY)`. With this client, all commumication was |
13 |
| -synchronous. Asynchronous bulk support was provided with the `downloader` |
14 |
| -module. There was no built-in support for polling when an order was |
15 |
| -ready to download or tracking when an order was downloaded. |
| 9 | +The V2 SDK has a flatter code structure. Everything of note previously found under `planet.api` is now available from the `planet` module. So, `import planet` should give you everything you need. |
16 | 10 |
|
17 |
| -In Version 2, a `Session` is used to manage all communication with the Planet |
18 |
| -APIs. This provides for multiple asynchronous connections. An API-specific |
19 |
| -client is created. This client manages polling and downloading, along with |
20 |
| -any other capabilities provided by the API. |
| 11 | +If you are migrating from V1, and are using the Data API filter builder (from `planet.api import filters`), we do recommend also including `from planet import data_filter as filters` in your imports for an easy migration from the old filter module to the new one. |
| 12 | + |
| 13 | +## Authentication |
| 14 | + |
| 15 | +If you have your API key stored in the `PL_API_KEY` environment variable you will be automatically authenticated to the V2 API, similar to how V1 worked. For other methods for authenticating against the V2 SDK, check out [Authenticate with the Planet server](quick-start-guide/#authenticate-with-the-planet-server). |
| 16 | + |
| 17 | +## Session for all communication |
| 18 | + |
| 19 | +In Version 2, sessions are used to manage all communication with the Planet APIs. This provides for multiple asynchronous connections. For each API, there is a specific client object. This client manages polling and downloading, along with any other capabilities provided by the API. |
| 20 | + |
| 21 | +Each client now requires a `Session` object, which stores connection information and authentication. |
| 22 | + |
| 23 | +The best way of doing this is wrapping any code that invokes a client class in a block like so: |
| 24 | + |
| 25 | +```python |
| 26 | +async with Session() as session: |
| 27 | + client = OrdersClient(session) |
| 28 | + result = await client.create_order(order) |
| 29 | +# Process result |
| 30 | +``` |
| 31 | + |
| 32 | +For more information about Session, refer to the [Python SDK User Guide](../../python/sdk-guide/#session). |
| 33 | + |
| 34 | +## Asynchronous Methods |
| 35 | + |
| 36 | +With the V1 client, all communication was synchronous. Asynchronous bulk support was provided with the `downloader` module. There was no built-in support for polling when an order was ready to download or tracking when an order was downloaded. |
| 37 | + |
| 38 | +In V2, all `*Client` methods (for example, `DataClient().search_aiter`, `OrderClient().create_order`) are asynchronous. Any functions that call such methods must include `async` in their definition. To invoke asynchronous methods from synchronous code, you can wrap the async method calls in `asyncio.run()`. The following is an example of using async with session. |
| 39 | + |
| 40 | +```python |
| 41 | +import asyncio |
| 42 | +from datetime import datetime |
| 43 | +from planet import Session, DataClient |
| 44 | +from planet import data_filter as filters |
| 45 | + |
| 46 | +async def do_search(): |
| 47 | + async with Session() as session: |
| 48 | + client = DataClient(session) |
| 49 | + date_filter = filters.date_range_filter('acquired', gte=datetime.fromisoformat("2022-11-18"), lte=datetime.fromisoformat("2022-11-21")) |
| 50 | + cloud_filter = filters.range_filter('cloud_cover', lte=0.1) |
| 51 | + download_filter = filters.permission_filter() |
| 52 | + search_results = await client.search(["PSScene"], filters.and_filter([date_filter, cloud_filter, download_filter])) |
| 53 | + return [item async for item in search_results] |
| 54 | + |
| 55 | +items = asyncio.run(do_search()) |
| 56 | +``` |
| 57 | + |
| 58 | +For more details on interacting with the asynchronous portions of the SDK, refer to the [Python SDK User Guide](../../python/sdk-guide/#session). |
| 59 | + |
| 60 | +## Data API |
| 61 | +The Data API portion of SDK V2 is quite similar to V1, although some filters have been renamed for consistency (also reference the note on imports): |
| 62 | + |
| 63 | +* `date_range` to `date_range_filter` |
| 64 | +* `geom_filter` to `geometry_filter` |
| 65 | +* `and_filter` and `or_filter` now takes a list of filters instead of multiple arguments, so just wrap the contents in `[]` |
| 66 | +* `permissions_filter` is now split into `permissions_filter` and `asset_filter`, reflecting a recent change in API structure. If you were using this previously, you’ll want to convert the old `permissions_filter` into an `asset_filter` (this also involves changing the filter values, e.g. `assets.ortho_analytic_8b_sr:download` will become `ortho_analytic_8b_sr`) and adding an empty `permissions_filter`. |
| 67 | + |
| 68 | +`filters.build_seach_request` no longer exists, and has instead been integrated into the replacement for `client.quick_seach`. For example: |
| 69 | + |
| 70 | +```python |
| 71 | +planet.api.ClientV1().quick_search(filters.build_search_request(all_filters, ["PSScene"])) |
| 72 | +``` |
| 73 | + |
| 74 | +Is now |
| 75 | + |
| 76 | +```python |
| 77 | +async with Session() as session: |
| 78 | + items_aiter = planet.DataClient(session).search_aiter(["PSScene"], all_filters) |
| 79 | + |
| 80 | +## Orders API |
| 81 | + |
| 82 | +The Orders API capabilities in V1 were quite primitive, but those that did exist have been retained in much the same form; `ClientV1().create_order` becomes `OrderClient(session).create_order`. (As with the `DataClient`, you must also use `async` and `Session` with `OrderClient`.) |
| 83 | + |
| 84 | +Additionally, there is now also an order builder in `planet.order_request`, similar to the preexisting search filter builder. For more details on this, refer to the [Creating an Order](../../python/sdk-guide/#creating-an-order). |
0 commit comments