88from fastapi_cloud_cli .utils .api import APIClient
99from fastapi_cloud_cli .utils .apps import get_app_config
1010from fastapi_cloud_cli .utils .auth import Identity
11- from fastapi_cloud_cli .utils .cli import get_rich_toolkit , handle_http_errors
11+ from fastapi_cloud_cli .utils .cli import get_rich_toolkit
1212from fastapi_cloud_cli .utils .env import validate_environment_variable_name
1313
1414logger = logging .getLogger (__name__ )
@@ -23,17 +23,17 @@ class EnvironmentVariableResponse(BaseModel):
2323 data : list [EnvironmentVariable ]
2424
2525
26- def _get_environment_variables (app_id : str ) -> EnvironmentVariableResponse :
27- with APIClient () as client :
28- response = client .get (f"/apps/{ app_id } /environment-variables/" )
29- response .raise_for_status ()
26+ def _get_environment_variables (
27+ client : APIClient , app_id : str
28+ ) -> EnvironmentVariableResponse :
29+ response = client .get (f"/apps/{ app_id } /environment-variables/" )
30+ response .raise_for_status ()
3031
31- return EnvironmentVariableResponse .model_validate (response .json ())
32+ return EnvironmentVariableResponse .model_validate (response .json ())
3233
3334
34- def _delete_environment_variable (app_id : str , name : str ) -> bool :
35- with APIClient () as client :
36- response = client .delete (f"/apps/{ app_id } /environment-variables/{ name } " )
35+ def _delete_environment_variable (client : APIClient , app_id : str , name : str ) -> bool :
36+ response = client .delete (f"/apps/{ app_id } /environment-variables/{ name } " )
3737
3838 if response .status_code == 404 :
3939 return False
@@ -44,14 +44,13 @@ def _delete_environment_variable(app_id: str, name: str) -> bool:
4444
4545
4646def _set_environment_variable (
47- app_id : str , name : str , value : str , is_secret : bool = False
47+ client : APIClient , app_id : str , name : str , value : str , is_secret : bool = False
4848) -> None :
49- with APIClient () as client :
50- response = client .post (
51- f"/apps/{ app_id } /environment-variables/" ,
52- json = {"name" : name , "value" : value , "is_secret" : is_secret },
53- )
54- response .raise_for_status ()
49+ response = client .post (
50+ f"/apps/{ app_id } /environment-variables/" ,
51+ json = {"name" : name , "value" : value , "is_secret" : is_secret },
52+ )
53+ response .raise_for_status ()
5554
5655
5756env_app = typer .Typer ()
@@ -91,11 +90,14 @@ def list(
9190 )
9291 raise typer .Exit (1 )
9392
94- with toolkit .progress (
95- "Fetching environment variables..." , transient = True
96- ) as progress :
97- with handle_http_errors (progress ):
98- environment_variables = _get_environment_variables (app_config .app_id )
93+ with APIClient () as client :
94+ with toolkit .progress (
95+ "Fetching environment variables..." , transient = True
96+ ) as progress :
97+ with client .handle_http_errors (progress ):
98+ environment_variables = _get_environment_variables (
99+ client = client , app_id = app_config .app_id
100+ )
99101
100102 if not environment_variables .data :
101103 toolkit .print ("No environment variables found." )
@@ -146,42 +148,45 @@ def delete(
146148 )
147149 raise typer .Exit (1 )
148150
149- if not name :
150- with toolkit .progress (
151- "Fetching environment variables..." , transient = True
152- ) as progress :
153- with handle_http_errors (progress ):
154- environment_variables = _get_environment_variables (
155- app_config .app_id
156- )
157-
158- if not environment_variables .data :
159- toolkit .print ("No environment variables found." )
160- return
161-
162- name = toolkit .ask (
163- "Select the environment variable to delete:" ,
164- options = [
165- {"name" : env_var .name , "value" : env_var .name }
166- for env_var in environment_variables .data
167- ],
168- )
169-
170- assert name
171- else :
172- if not validate_environment_variable_name (name ):
173- toolkit .print (
174- f"The environment variable name [bold]{ name } [/] is invalid."
151+ with APIClient () as client :
152+ if not name :
153+ with toolkit .progress (
154+ "Fetching environment variables..." , transient = True
155+ ) as progress :
156+ with client .handle_http_errors (progress ):
157+ environment_variables = _get_environment_variables (
158+ client = client , app_id = app_config .app_id
159+ )
160+
161+ if not environment_variables .data :
162+ toolkit .print ("No environment variables found." )
163+ return
164+
165+ name = toolkit .ask (
166+ "Select the environment variable to delete:" ,
167+ options = [
168+ {"name" : env_var .name , "value" : env_var .name }
169+ for env_var in environment_variables .data
170+ ],
175171 )
176- raise typer .Exit (1 )
177172
178- toolkit .print_line ()
173+ assert name
174+ else :
175+ if not validate_environment_variable_name (name ):
176+ toolkit .print (
177+ f"The environment variable name [bold]{ name } [/] is invalid."
178+ )
179+ raise typer .Exit (1 )
180+
181+ toolkit .print_line ()
179182
180- with toolkit .progress (
181- "Deleting environment variable" , transient = True
182- ) as progress :
183- with handle_http_errors (progress ):
184- deleted = _delete_environment_variable (app_config .app_id , name )
183+ with toolkit .progress (
184+ "Deleting environment variable" , transient = True
185+ ) as progress :
186+ with client .handle_http_errors (progress ):
187+ deleted = _delete_environment_variable (
188+ client = client , app_id = app_config .app_id , name = name
189+ )
185190
186191 if not deleted :
187192 toolkit .print ("Environment variable not found." )
@@ -253,14 +258,21 @@ def set(
253258 else :
254259 value = toolkit .input ("Enter the value of the environment variable:" )
255260
256- with toolkit .progress (
257- "Setting environment variable" , transient = True
258- ) as progress :
259- assert name is not None
260- assert value is not None
261-
262- with handle_http_errors (progress ):
263- _set_environment_variable (app_config .app_id , name , value , secret )
261+ with APIClient () as client :
262+ with toolkit .progress (
263+ "Setting environment variable" , transient = True
264+ ) as progress :
265+ assert name is not None
266+ assert value is not None
267+
268+ with client .handle_http_errors (progress ):
269+ _set_environment_variable (
270+ client = client ,
271+ app_id = app_config .app_id ,
272+ name = name ,
273+ value = value ,
274+ is_secret = secret ,
275+ )
264276
265277 if secret :
266278 toolkit .print (f"Secret environment variable [bold]{ name } [/] set." )
0 commit comments