2828 TooManyRetriesError ,
2929)
3030from fastapi_cloud_cli .utils .apps import AppConfig , get_app_config , write_app_config
31- from fastapi_cloud_cli .utils .auth import Identity
31+ from fastapi_cloud_cli .utils .auth import AuthMode , Identity
3232from fastapi_cloud_cli .utils .cli import get_rich_toolkit , handle_http_errors
3333from fastapi_cloud_cli .utils .progress_file import ProgressFile
3434
@@ -72,7 +72,7 @@ def _cancel_upload(deployment_id: str) -> None:
7272 logger .debug ("Cancelling upload for deployment: %s" , deployment_id )
7373
7474 try :
75- with APIClient () as client :
75+ with APIClient (use_deploy_token = True ) as client :
7676 response = client .post (f"/deployments/{ deployment_id } /upload-cancelled" )
7777 response .raise_for_status ()
7878
@@ -142,7 +142,7 @@ class Team(BaseModel):
142142
143143
144144def _get_teams () -> list [Team ]:
145- with APIClient () as client :
145+ with APIClient (use_deploy_token = True ) as client :
146146 response = client .get ("/teams/" )
147147 response .raise_for_status ()
148148
@@ -158,7 +158,7 @@ class AppResponse(BaseModel):
158158
159159
160160def _update_app (app_id : str , directory : str | None ) -> AppResponse :
161- with APIClient () as client :
161+ with APIClient (use_deploy_token = True ) as client :
162162 response = client .patch (
163163 f"/apps/{ app_id } " ,
164164 json = {"directory" : directory },
@@ -170,7 +170,7 @@ def _update_app(app_id: str, directory: str | None) -> AppResponse:
170170
171171
172172def _create_app (team_id : str , app_name : str , directory : str | None ) -> AppResponse :
173- with APIClient () as client :
173+ with APIClient (use_deploy_token = True ) as client :
174174 response = client .post (
175175 "/apps/" ,
176176 json = {"name" : app_name , "team_id" : team_id , "directory" : directory },
@@ -191,7 +191,7 @@ class CreateDeploymentResponse(BaseModel):
191191
192192
193193def _create_deployment (app_id : str ) -> CreateDeploymentResponse :
194- with APIClient () as client :
194+ with APIClient (use_deploy_token = True ) as client :
195195 response = client .post (f"/apps/{ app_id } /deployments/" )
196196 response .raise_for_status ()
197197
@@ -230,7 +230,7 @@ def progress_callback(bytes_read: int) -> None:
230230 f"Uploading deployment ({ _format_size (bytes_read )} of { archive_size_str } )..."
231231 )
232232
233- with APIClient () as fastapi_client , Client () as client :
233+ with APIClient (use_deploy_token = True ) as fastapi_client , Client () as client :
234234 # Get the upload URL
235235 logger .debug ("Requesting upload URL from API" )
236236 response = fastapi_client .post (f"/deployments/{ deployment_id } /upload" )
@@ -264,7 +264,7 @@ def progress_callback(bytes_read: int) -> None:
264264
265265
266266def _get_app (app_slug : str ) -> AppResponse | None :
267- with APIClient () as client :
267+ with APIClient (use_deploy_token = True ) as client :
268268 response = client .get (f"/apps/{ app_slug } " )
269269
270270 if response .status_code == 404 :
@@ -278,7 +278,7 @@ def _get_app(app_slug: str) -> AppResponse | None:
278278
279279
280280def _get_apps (team_id : str ) -> list [AppResponse ]:
281- with APIClient () as client :
281+ with APIClient (use_deploy_token = True ) as client :
282282 response = client .get ("/apps/" , params = {"team_id" : team_id })
283283 response .raise_for_status ()
284284
@@ -308,14 +308,20 @@ def _get_apps(team_id: str) -> list[AppResponse]:
308308]
309309
310310
311- def _configure_app (toolkit : RichToolkit , path_to_deploy : Path ) -> AppConfig :
311+ def _configure_app (
312+ toolkit : RichToolkit ,
313+ path_to_deploy : Path ,
314+ auth_mode : AuthMode = "user" ,
315+ ) -> AppConfig :
312316 toolkit .print (f"Setting up and deploying [blue]{ path_to_deploy } [/blue]" , tag = "path" )
313317
314318 toolkit .print_line ()
315319
316320 with toolkit .progress ("Fetching teams..." ) as progress :
317321 with handle_http_errors (
318- progress , default_message = "Error fetching teams. Please try again later."
322+ progress ,
323+ default_message = "Error fetching teams. Please try again later." ,
324+ auth_mode = auth_mode ,
319325 ):
320326 teams = _get_teams ()
321327
@@ -341,7 +347,9 @@ def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
341347 if not create_new_app :
342348 with toolkit .progress ("Fetching apps..." ) as progress :
343349 with handle_http_errors (
344- progress , default_message = "Error fetching apps. Please try again later."
350+ progress ,
351+ default_message = "Error fetching apps. Please try again later." ,
352+ auth_mode = auth_mode ,
345353 ):
346354 apps = _get_apps (team .id )
347355
@@ -411,7 +419,7 @@ def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
411419 if directory != selected_app .directory :
412420 with (
413421 toolkit .progress (title = "Updating app directory..." ) as progress ,
414- handle_http_errors (progress ),
422+ handle_http_errors (progress , auth_mode = auth_mode ),
415423 ):
416424 app = _update_app (selected_app .id , directory = directory )
417425
@@ -420,7 +428,7 @@ def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
420428 app = selected_app
421429 else :
422430 with toolkit .progress (title = "Creating app..." ) as progress :
423- with handle_http_errors (progress ):
431+ with handle_http_errors (progress , auth_mode = auth_mode ):
424432 app = _create_app (team .id , app_name , directory = directory )
425433
426434 progress .log (f"App created successfully! App slug: { app .slug } " )
@@ -485,7 +493,7 @@ def _wait_for_deployment(
485493
486494 last_message_changed_at = time .monotonic ()
487495
488- with APIClient () as client :
496+ with APIClient (use_deploy_token = True ) as client :
489497 with (
490498 toolkit .progress (
491499 next (messages ),
@@ -556,7 +564,7 @@ def _send_waitlist_form(
556564 toolkit : RichToolkit ,
557565) -> None :
558566 with toolkit .progress ("Sending your request..." ) as progress :
559- with APIClient () as client :
567+ with APIClient (use_deploy_token = True ) as client :
560568 with handle_http_errors (progress ):
561569 response = client .post ("/users/waiting-list" , json = result .model_dump ())
562570
@@ -674,15 +682,18 @@ def deploy(
674682 )
675683
676684 identity = Identity ()
685+ use_deploy = identity .has_deploy_token ()
686+ has_auth = use_deploy or identity .is_logged_in ()
687+ auth_mode : AuthMode = "token" if use_deploy else "user"
677688
678689 with get_rich_toolkit () as toolkit :
679- if not identity . is_logged_in () :
690+ if not has_auth :
680691 logger .debug ("User not logged in, prompting for login or waitlist" )
681692
682693 toolkit .print_title ("Welcome to FastAPI Cloud!" , tag = "FastAPI" )
683694 toolkit .print_line ()
684695
685- if identity .token and identity .is_expired ():
696+ if identity .user_token and identity .is_user_token_expired ():
686697 toolkit .print (
687698 "Your session has expired. Please log in again." ,
688699 tag = "info" ,
@@ -709,6 +720,13 @@ def deploy(
709720 _waitlist_form (toolkit )
710721 raise typer .Exit (1 )
711722
723+ if use_deploy :
724+ toolkit .print (
725+ "Using token from [bold blue]FASTAPI_CLOUD_TOKEN[/] environment variable" ,
726+ tag = "info" ,
727+ )
728+ toolkit .print_line ()
729+
712730 toolkit .print_title ("Starting deployment" , tag = "FastAPI" )
713731 toolkit .print_line ()
714732
@@ -738,7 +756,9 @@ def deploy(
738756 else :
739757 logger .debug ("No app config found, configuring new app" )
740758
741- app_config = _configure_app (toolkit , path_to_deploy = path_to_deploy )
759+ app_config = _configure_app (
760+ toolkit , path_to_deploy = path_to_deploy , auth_mode = auth_mode
761+ )
742762 toolkit .print_line ()
743763
744764 target_app_id = app_config .app_id
@@ -751,7 +771,7 @@ def deploy(
751771 toolkit .print_line ()
752772
753773 with toolkit .progress ("Checking app..." , transient = True ) as progress :
754- with handle_http_errors (progress ):
774+ with handle_http_errors (progress , auth_mode = auth_mode ):
755775 logger .debug ("Checking app with ID: %s" , target_app_id )
756776 app = _get_app (target_app_id )
757777
@@ -780,7 +800,7 @@ def deploy(
780800 toolkit .progress (
781801 title = "Creating deployment" , done_emoji = "📦"
782802 ) as progress ,
783- handle_http_errors (progress ),
803+ handle_http_errors (progress , auth_mode = auth_mode ),
784804 ):
785805 logger .debug ("Creating deployment for app: %s" , app .id )
786806 deployment = _create_deployment (app .id )
0 commit comments