66from enum import Enum
77from itertools import cycle
88from pathlib import Path
9- from typing import Any , Dict , List , Union
9+ from typing import Any , Dict , List , Optional , Tuple , Union
1010
1111import rignore
1212import typer
@@ -76,14 +76,12 @@ def _get_teams() -> List[Team]:
7676 return [Team .model_validate (team ) for team in data ]
7777
7878
79- class CreateAppResponse (BaseModel ):
80- name : str
79+ class AppResponse (BaseModel ):
8180 id : str
82- team_id : str
8381 slug : str
8482
8583
86- def _create_app (team_slug : str , app_name : str ) -> CreateAppResponse :
84+ def _create_app (team_slug : str , app_name : str ) -> AppResponse :
8785 with APIClient () as client :
8886 response = client .post (
8987 "/apps/" ,
@@ -92,7 +90,7 @@ def _create_app(team_slug: str, app_name: str) -> CreateAppResponse:
9290
9391 response .raise_for_status ()
9492
95- return CreateAppResponse .model_validate (response .json ())
93+ return AppResponse .model_validate (response .json ())
9694
9795
9896class DeploymentStatus (str , Enum ):
@@ -140,6 +138,20 @@ def _upload_deployment(deployment_id: str, archive_path: Path) -> None:
140138 response .raise_for_status ()
141139
142140
141+ def _get_app (app_slug : str ) -> Optional [AppResponse ]:
142+ with APIClient () as client :
143+ response = client .get (f"/apps/{ app_slug } " )
144+
145+ if response .status_code == 404 :
146+ return None
147+
148+ response .raise_for_status ()
149+
150+ data = response .json ()
151+
152+ return AppResponse .model_validate (data )
153+
154+
143155class DeploymentResponse (BaseModel ):
144156 id : str
145157 app_id : str
@@ -179,7 +191,9 @@ def _get_deployment(app_id: str, deployment_id: str) -> DeploymentResponse:
179191]
180192
181193
182- def _configure_app (toolkit : RichToolkit , path_to_deploy : Path ) -> AppConfig :
194+ def _configure_app (
195+ toolkit : RichToolkit , path_to_deploy : Path
196+ ) -> Tuple [AppConfig , AppResponse ]:
183197 if not toolkit .confirm (f"Setup and deploy [blue]{ path_to_deploy } [/]?" , tag = "dir" ):
184198 raise typer .Exit (0 )
185199
@@ -215,7 +229,7 @@ def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
215229
216230 progress .log (f"App created successfully! App slug: { app_data .slug } " )
217231
218- return write_app_config (path_to_deploy , id = app_data . id , slug = app_data .slug )
232+ return write_app_config (path_to_deploy , slug = app_data .slug ), app_data
219233
220234
221235def _wait_for_deployment (
@@ -297,19 +311,33 @@ def deploy(
297311 path_to_deploy = path or Path .cwd ()
298312
299313 app_config = get_app_config (path_to_deploy )
314+ app_data : Optional [AppResponse ] = None
300315
301316 if not app_config :
302- app_config = _configure_app (toolkit , path_to_deploy = path_to_deploy )
317+ app_config , app_data = _configure_app (
318+ toolkit , path_to_deploy = path_to_deploy
319+ )
303320 else :
304321 toolkit .print (f"Deploying app [blue]{ app_config .slug } [blue]..." )
322+ toolkit .print_line ()
323+
324+ with toolkit .progress ("Checking app..." , transient = True ) as progress :
325+ app_data = _get_app (app_config .slug )
326+
327+ if not app_data :
328+ progress .set_error (
329+ "App not found. Make sure you're logged in the correct account."
330+ )
331+
332+ raise typer .Exit (1 )
305333
306334 toolkit .print_line ()
307335
308336 archive_path = archive (path or Path .cwd ()) # noqa: F841
309337
310338 with toolkit .progress (title = "Creating deployment" ) as progress :
311339 with handle_http_errors (progress ):
312- deployment = _create_deployment (app_config .id )
340+ deployment = _create_deployment (app_data .id )
313341
314342 progress .log (
315343 f"Deployment created successfully! Deployment slug: { deployment .slug } "
@@ -330,7 +358,7 @@ def deploy(
330358
331359 if not skip_wait :
332360 _wait_for_deployment (
333- toolkit , app_config .id , deployment .id , check_deployment_url
361+ toolkit , app_data .id , deployment .id , check_deployment_url
334362 )
335363 else :
336364 toolkit .print (
0 commit comments