6
6
from enum import Enum
7
7
from itertools import cycle
8
8
from pathlib import Path
9
- from typing import Any , Dict , List , Union
9
+ from typing import Any , Dict , List , Optional , Tuple , Union
10
10
11
11
import rignore
12
12
import typer
@@ -76,14 +76,12 @@ def _get_teams() -> List[Team]:
76
76
return [Team .model_validate (team ) for team in data ]
77
77
78
78
79
- class CreateAppResponse (BaseModel ):
80
- name : str
79
+ class AppResponse (BaseModel ):
81
80
id : str
82
- team_id : str
83
81
slug : str
84
82
85
83
86
- def _create_app (team_slug : str , app_name : str ) -> CreateAppResponse :
84
+ def _create_app (team_slug : str , app_name : str ) -> AppResponse :
87
85
with APIClient () as client :
88
86
response = client .post (
89
87
"/apps/" ,
@@ -92,7 +90,7 @@ def _create_app(team_slug: str, app_name: str) -> CreateAppResponse:
92
90
93
91
response .raise_for_status ()
94
92
95
- return CreateAppResponse .model_validate (response .json ())
93
+ return AppResponse .model_validate (response .json ())
96
94
97
95
98
96
class DeploymentStatus (str , Enum ):
@@ -140,6 +138,20 @@ def _upload_deployment(deployment_id: str, archive_path: Path) -> None:
140
138
response .raise_for_status ()
141
139
142
140
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
+
143
155
class DeploymentResponse (BaseModel ):
144
156
id : str
145
157
app_id : str
@@ -179,7 +191,9 @@ def _get_deployment(app_id: str, deployment_id: str) -> DeploymentResponse:
179
191
]
180
192
181
193
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 ]:
183
197
if not toolkit .confirm (f"Setup and deploy [blue]{ path_to_deploy } [/]?" , tag = "dir" ):
184
198
raise typer .Exit (0 )
185
199
@@ -215,7 +229,7 @@ def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
215
229
216
230
progress .log (f"App created successfully! App slug: { app_data .slug } " )
217
231
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
219
233
220
234
221
235
def _wait_for_deployment (
@@ -297,19 +311,33 @@ def deploy(
297
311
path_to_deploy = path or Path .cwd ()
298
312
299
313
app_config = get_app_config (path_to_deploy )
314
+ app_data : Optional [AppResponse ] = None
300
315
301
316
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
+ )
303
320
else :
304
321
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 )
305
333
306
334
toolkit .print_line ()
307
335
308
336
archive_path = archive (path or Path .cwd ()) # noqa: F841
309
337
310
338
with toolkit .progress (title = "Creating deployment" ) as progress :
311
339
with handle_http_errors (progress ):
312
- deployment = _create_deployment (app_config .id )
340
+ deployment = _create_deployment (app_data .id )
313
341
314
342
progress .log (
315
343
f"Deployment created successfully! Deployment slug: { deployment .slug } "
@@ -330,7 +358,7 @@ def deploy(
330
358
331
359
if not skip_wait :
332
360
_wait_for_deployment (
333
- toolkit , app_config .id , deployment .id , check_deployment_url
361
+ toolkit , app_data .id , deployment .id , check_deployment_url
334
362
)
335
363
else :
336
364
toolkit .print (
0 commit comments