11"""Subscriptions CLI"""
22
3- from contextlib import asynccontextmanager
4- import itertools
53import json
6- from typing import AsyncIterator , Dict , Set
7- import uuid
84
95import click
106
11- import planet
127from planet import Session
138from planet .cli .cmds import coro , translate_exceptions
149from planet .cli .io import echo_json
15- from planet .exceptions import PlanetError
16-
17- # Collections of fake subscriptions and results for testing. Tests will
18- # monkeypatch these attributes.
19- _fake_subs : Dict [str , dict ] = {}
20- _fake_sub_results : Dict [str , list ] = {}
21-
22-
23- class PlaceholderSubscriptionsClient :
24- """A placeholder client.
25-
26- This class and its methods are derived from tests of a skeleton
27- Subscriptions CLI.
28-
29- """
30-
31- async def list_subscriptions (self ,
32- status : Set [str ] = None ,
33- limit : int = 100 ) -> AsyncIterator [dict ]:
34- """Get Subscriptions.
35-
36- Parameters:
37- status
38- limit
39-
40- Yields:
41- dict
42-
43- """
44- if status :
45- select_subs = (dict (** sub , id = sub_id ) for sub_id ,
46- sub in _fake_subs .items ()
47- if sub ['status' ] in status )
48- else :
49- select_subs = (
50- dict (** sub , id = sub_id ) for sub_id , sub in _fake_subs .items ())
51-
52- filtered_subs = itertools .islice (select_subs , limit )
53-
54- for sub in filtered_subs :
55- yield sub
56-
57- async def create_subscription (self , request : dict ) -> dict :
58- """Create a Subscription.
59-
60- Parameters:
61- request
62-
63- Returns:
64- dict
65-
66- Raises:
67- PlanetError
68-
69- """
70- missing_keys = {'name' , 'delivery' , 'source' } - request .keys ()
71- if missing_keys :
72- raise PlanetError (
73- f"Request lacks required members: { missing_keys !r} " )
74-
75- id = str (uuid .uuid4 ())
76- _fake_subs [id ] = request
77- sub = _fake_subs [id ].copy ()
78- sub .update (id = id )
79- return sub
80-
81- async def cancel_subscription (self , subscription_id : str ) -> dict :
82- """Cancel a Subscription.
83-
84- Parameters:
85- subscription_id
86-
87- Returns:
88- dict
89-
90- Raises:
91- PlanetError
92-
93- """
94- try :
95- sub = _fake_subs .pop (subscription_id )
96- except KeyError :
97- raise PlanetError (f"No such subscription: { subscription_id !r} " )
98-
99- sub .update (id = subscription_id )
100- return sub
101-
102- async def update_subscription (self , subscription_id : str ,
103- request : dict ) -> dict :
104- """Update (edit) a Subscription.
105-
106- Parameters:
107- subscription_id
108-
109- Returns:
110- dict
111-
112- Raises:
113- PlanetError
114-
115- """
116- try :
117- _fake_subs [subscription_id ].update (** request )
118- sub = _fake_subs [subscription_id ].copy ()
119- except KeyError :
120- raise PlanetError (f"No such subscription: { subscription_id !r} " )
121-
122- sub .update (id = subscription_id )
123- return sub
124-
125- async def get_subscription (self , subscription_id : str ) -> dict :
126- """Get a description of a Subscription.
127-
128- Parameters:
129- subscription_id
130-
131- Returns:
132- dict
133-
134- Raises:
135- PlanetError
136-
137- """
138- try :
139- sub = _fake_subs [subscription_id ].copy ()
140- except KeyError :
141- raise PlanetError (f"No such subscription: { subscription_id !r} " )
142-
143- sub .update (id = subscription_id )
144- return sub
145-
146- async def list_subscription_results (
147- self ,
148- subscription_id : str ,
149- status : Set [str ] = None ,
150- limit : int = 100 ) -> AsyncIterator [dict ]:
151- """Get Results of a Subscription.
152-
153- Parameters:
154- subscription_id
155- status
156- limit
157-
158- Yields:
159- dict
160-
161- Raises:
162- PlanetError
163-
164- """
165- try :
166- if status :
167- select_results = (
168- result for result in _fake_sub_results [subscription_id ]
169- if result ['status' ] in status )
170- else :
171- select_results = (
172- result for result in _fake_sub_results [subscription_id ])
173-
174- filtered_results = itertools .islice (select_results , limit )
175- except KeyError :
176- raise PlanetError (f"No such subscription: { subscription_id !r} " )
177-
178- for result in filtered_results :
179- yield result
180-
181-
182- @asynccontextmanager
183- async def subscriptions_client (ctx ):
184- """Create an authenticated client.
185-
186- Note that the session is not currently used with the placeholder
187- client.
188-
189- """
190- auth = ctx .obj ['AUTH' ]
191- async with Session (auth = auth ):
192- yield PlaceholderSubscriptionsClient ()
10+ from planet .clients .subscriptions import (PlaceholderSubscriptionsClient as
11+ SubscriptionsClient )
19312
19413
19514@click .group ()
19615@click .pass_context
19716def subscriptions (ctx ):
198- ctx .obj ['AUTH' ] = planet .Auth .from_file ()
17+ # None means that order of precedence is 1) environment variable,
18+ # 2) secret file.
19+ ctx .obj ['AUTH' ] = None
19920
20021
20122# We want our command to be known as "list" on the command line but
@@ -219,7 +40,8 @@ def subscriptions(ctx):
21940@coro
22041async def list_subscriptions_cmd (ctx , status , limit , pretty ):
22142 """Prints a sequence of JSON-encoded Subscription descriptions."""
222- async with subscriptions_client (ctx ) as client :
43+ async with Session (auth = ctx .obj ['AUTH' ]) as session :
44+ client = SubscriptionsClient (session )
22345 filtered_subs = client .list_subscriptions (status = status , limit = limit )
22446 async for sub in filtered_subs :
22547 echo_json (sub , pretty )
@@ -254,7 +76,8 @@ def parse_request(ctx, param, value: str) -> dict:
25476@coro
25577async def create_subscription_cmd (ctx , request , pretty ):
25678 """Submits a subscription request and prints the API response."""
257- async with subscriptions_client (ctx ) as client :
79+ async with Session (auth = ctx .obj ['AUTH' ]) as session :
80+ client = SubscriptionsClient (session )
25881 sub = await client .create_subscription (request )
25982 echo_json (sub , pretty )
26083
@@ -267,7 +90,8 @@ async def create_subscription_cmd(ctx, request, pretty):
26790@coro
26891async def cancel_subscription_cmd (ctx , subscription_id , pretty ):
26992 """Cancels a subscription and prints the API response."""
270- async with subscriptions_client (ctx ) as client :
93+ async with Session (auth = ctx .obj ['AUTH' ]) as session :
94+ client = SubscriptionsClient (session )
27195 sub = await client .cancel_subscription (subscription_id )
27296 echo_json (sub , pretty )
27397
@@ -281,7 +105,8 @@ async def cancel_subscription_cmd(ctx, subscription_id, pretty):
281105@coro
282106async def update_subscription_cmd (ctx , subscription_id , request , pretty ):
283107 """Cancels a subscription and prints the API response."""
284- async with subscriptions_client (ctx ) as client :
108+ async with Session (auth = ctx .obj ['AUTH' ]) as session :
109+ client = SubscriptionsClient (session )
285110 sub = await client .update_subscription (subscription_id , request )
286111 echo_json (sub , pretty )
287112
@@ -301,7 +126,8 @@ async def describe_subscription_cmd(ctx, subscription_id, pretty):
301126 After we refactor we will change to mocking the API.
302127
303128 """
304- async with subscriptions_client (ctx ) as client :
129+ async with Session (auth = ctx .obj ['AUTH' ]) as session :
130+ client = SubscriptionsClient (session )
305131 sub = await client .get_subscription (subscription_id )
306132 echo_json (sub , pretty )
307133
@@ -336,9 +162,10 @@ async def list_subscription_results_cmd(ctx,
336162 status ,
337163 limit ):
338164 """Gets results of a subscription and prints the API response."""
339- async with subscriptions_client (ctx ) as client :
340- filtered_results = client .list_subscription_results (subscription_id ,
341- status = status ,
342- limit = limit )
165+ async with Session (auth = ctx .obj ['AUTH' ]) as session :
166+ client = SubscriptionsClient (session )
167+ filtered_results = client .get_results (subscription_id ,
168+ status = status ,
169+ limit = limit )
343170 async for result in filtered_results :
344171 echo_json (result , pretty )
0 commit comments