Skip to content

Commit c1a4c7b

Browse files
authored
Subscriptions API client (#607)
* First steps towards a real subscriptions client. Mocking the service in the tests is the hard part. There are several different approaches we can take and none of them are obviously the best. * Subscriptions API client using httpx Tests use respx to mock server routing. Subscriptions results is not covered in this batch of work. * Implement subscription get_results And subclass Session with custom x-planet-app header * Switch CLI commands over to CliSession * Add cli/session.py * Delete redundant code, exclude some lines from coverage * Linting * Restore exception translation for list_subscriptions_cmd
1 parent 50c73a5 commit c1a4c7b

File tree

9 files changed

+412
-291
lines changed

9 files changed

+412
-291
lines changed

planet/cli/data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
import click
2121

22-
from planet import data_filter, exceptions, io, DataClient, Session
23-
22+
from planet import data_filter, exceptions, io, DataClient
2423
from .cmds import coro, translate_exceptions
2524
from .io import echo_json
25+
from .session import CliSession
2626

2727
pretty = click.option('--pretty', is_flag=True, help='Pretty-print output.')
2828

@@ -31,7 +31,7 @@
3131
async def data_client(ctx):
3232
auth = ctx.obj['AUTH']
3333
base_url = ctx.obj['BASE_URL']
34-
async with Session(auth=auth) as sess:
34+
async with CliSession(auth=auth) as sess:
3535
cl = DataClient(sess, base_url=base_url)
3636
yield cl
3737

planet/cli/orders.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
import click
2121

2222
import planet
23-
from planet import OrdersClient, Session # allow mocking
23+
from planet import OrdersClient # allow mocking
2424
from .cmds import coro, translate_exceptions
2525
from .io import echo_json
2626
from .options import pretty
27+
from .session import CliSession
2728

2829
LOGGER = logging.getLogger(__name__)
2930

@@ -32,7 +33,7 @@
3233
async def orders_client(ctx):
3334
auth = ctx.obj['AUTH']
3435
base_url = ctx.obj['BASE_URL']
35-
async with Session(auth=auth) as sess:
36+
async with CliSession(auth=auth) as sess:
3637
cl = OrdersClient(sess, base_url=base_url)
3738
yield cl
3839

planet/cli/session.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""CLI HTTP/auth sessions."""
2+
3+
from planet.auth import AuthType
4+
from planet.http import Session
5+
6+
7+
class CliSession(Session):
8+
9+
def __init__(self, auth: AuthType = None):
10+
super().__init__(auth)
11+
self._client.headers.update({'X-Planet-App': 'python-cli'})

planet/cli/subscriptions.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import click
66

7-
from planet import Session
8-
from planet.cli.cmds import coro, translate_exceptions
9-
from planet.cli.io import echo_json
7+
from .cmds import coro, translate_exceptions
8+
from .io import echo_json
9+
from .session import CliSession
1010
from planet.clients.subscriptions import SubscriptionsClient
1111

1212

@@ -39,7 +39,7 @@ def subscriptions(ctx):
3939
@coro
4040
async def list_subscriptions_cmd(ctx, status, limit, pretty):
4141
"""Prints a sequence of JSON-encoded Subscription descriptions."""
42-
async with Session(auth=ctx.obj['AUTH']) as session:
42+
async with CliSession(auth=ctx.obj['AUTH']) as session:
4343
client = SubscriptionsClient(session)
4444
filtered_subs = client.list_subscriptions(status=status, limit=limit)
4545
async for sub in filtered_subs:
@@ -75,7 +75,7 @@ def parse_request(ctx, param, value: str) -> dict:
7575
@coro
7676
async def create_subscription_cmd(ctx, request, pretty):
7777
"""Submits a subscription request and prints the API response."""
78-
async with Session(auth=ctx.obj['AUTH']) as session:
78+
async with CliSession(auth=ctx.obj['AUTH']) as session:
7979
client = SubscriptionsClient(session)
8080
sub = await client.create_subscription(request)
8181
echo_json(sub, pretty)
@@ -89,7 +89,7 @@ async def create_subscription_cmd(ctx, request, pretty):
8989
@coro
9090
async def cancel_subscription_cmd(ctx, subscription_id, pretty):
9191
"""Cancels a subscription and prints the API response."""
92-
async with Session(auth=ctx.obj['AUTH']) as session:
92+
async with CliSession(auth=ctx.obj['AUTH']) as session:
9393
client = SubscriptionsClient(session)
9494
sub = await client.cancel_subscription(subscription_id)
9595
echo_json(sub, pretty)
@@ -104,7 +104,7 @@ async def cancel_subscription_cmd(ctx, subscription_id, pretty):
104104
@coro
105105
async def update_subscription_cmd(ctx, subscription_id, request, pretty):
106106
"""Cancels a subscription and prints the API response."""
107-
async with Session(auth=ctx.obj['AUTH']) as session:
107+
async with CliSession(auth=ctx.obj['AUTH']) as session:
108108
client = SubscriptionsClient(session)
109109
sub = await client.update_subscription(subscription_id, request)
110110
echo_json(sub, pretty)
@@ -125,7 +125,7 @@ async def describe_subscription_cmd(ctx, subscription_id, pretty):
125125
After we refactor we will change to mocking the API.
126126
127127
"""
128-
async with Session(auth=ctx.obj['AUTH']) as session:
128+
async with CliSession(auth=ctx.obj['AUTH']) as session:
129129
client = SubscriptionsClient(session)
130130
sub = await client.get_subscription(subscription_id)
131131
echo_json(sub, pretty)
@@ -161,7 +161,7 @@ async def list_subscription_results_cmd(ctx,
161161
status,
162162
limit):
163163
"""Gets results of a subscription and prints the API response."""
164-
async with Session(auth=ctx.obj['AUTH']) as session:
164+
async with CliSession(auth=ctx.obj['AUTH']) as session:
165165
client = SubscriptionsClient(session)
166166
filtered_results = client.get_results(subscription_id,
167167
status=status,

0 commit comments

Comments
 (0)