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 , Optional , Union
9
+ from typing import Any , Dict , List , Union
10
10
11
11
import rignore
12
12
import typer
13
- from httpx import Client , HTTPError , ReadTimeout
13
+ from httpx import Client
14
14
from pydantic import BaseModel
15
15
from rich_toolkit import RichToolkit
16
16
from rich_toolkit .menu import Option
20
20
from fastapi_cli .utils .api import APIClient
21
21
from fastapi_cli .utils .apps import AppConfig , get_app_config , write_app_config
22
22
from fastapi_cli .utils .auth import is_logged_in
23
- from fastapi_cli .utils .cli import get_rich_toolkit
23
+ from fastapi_cli .utils .cli import get_rich_toolkit , handle_http_errors
24
24
25
25
logger = logging .getLogger (__name__ )
26
26
@@ -66,20 +66,14 @@ class Team(BaseModel):
66
66
name : str
67
67
68
68
69
- def _get_teams () -> Optional [ List [Team ] ]:
69
+ def _get_teams () -> List [Team ]:
70
70
with APIClient () as client :
71
- try :
72
- response = client .get ("/teams/" )
73
- response .raise_for_status ()
74
- except (HTTPError , ReadTimeout ) as e :
75
- logger .debug (e )
76
- return None
71
+ response = client .get ("/teams/" )
72
+ response .raise_for_status ()
77
73
78
74
data = response .json ()["data" ]
79
75
80
- teams = [Team .model_validate (team ) for team in data ]
81
-
82
- return teams
76
+ return [Team .model_validate (team ) for team in data ]
83
77
84
78
85
79
class CreateAppResponse (BaseModel ):
@@ -89,19 +83,14 @@ class CreateAppResponse(BaseModel):
89
83
slug : str
90
84
91
85
92
- def _create_app (team_slug : str , app_name : str ) -> Optional [ CreateAppResponse ] :
86
+ def _create_app (team_slug : str , app_name : str ) -> CreateAppResponse :
93
87
with APIClient () as client :
94
- try :
95
- response = client .post (
96
- "/apps/" ,
97
- json = {"name" : app_name , "team_slug" : team_slug },
98
- )
99
- except (HTTPError , ReadTimeout ) as e :
100
- logger .debug (e )
101
- return None
88
+ response = client .post (
89
+ "/apps/" ,
90
+ json = {"name" : app_name , "team_slug" : team_slug },
91
+ )
102
92
103
- if response .status_code != 201 :
104
- return None
93
+ response .raise_for_status ()
105
94
106
95
return CreateAppResponse .model_validate (response .json ())
107
96
@@ -121,16 +110,10 @@ class CreateDeploymentResponse(BaseModel):
121
110
status : DeploymentStatus
122
111
123
112
124
- def _create_deployment (app_id : str ) -> Optional [ CreateDeploymentResponse ] :
113
+ def _create_deployment (app_id : str ) -> CreateDeploymentResponse :
125
114
with APIClient () as client :
126
- try :
127
- response = client .post (f"/apps/{ app_id } /deployments/" )
128
- except (HTTPError , ReadTimeout ) as e :
129
- logger .debug (e )
130
- return None
131
-
132
- if response .status_code != 201 :
133
- return None
115
+ response = client .post (f"/apps/{ app_id } /deployments/" )
116
+ response .raise_for_status ()
134
117
135
118
return CreateDeploymentResponse .model_validate (response .json ())
136
119
@@ -140,32 +123,21 @@ class RequestUploadResponse(BaseModel):
140
123
fields : Dict [str , str ]
141
124
142
125
143
- def _upload_deployment (deployment_id : str , archive_path : Path ) -> bool :
126
+ def _upload_deployment (deployment_id : str , archive_path : Path ) -> None :
144
127
with APIClient () as client :
145
- try :
146
- response = client .post (f"/deployments/{ deployment_id } /upload" )
147
-
148
- response .raise_for_status ()
149
- except (HTTPError , ReadTimeout ) as e :
150
- logger .debug (e )
151
- return False
128
+ response = client .post (f"/deployments/{ deployment_id } /upload" )
129
+ response .raise_for_status ()
152
130
153
131
upload_data = RequestUploadResponse .model_validate (response .json ())
154
132
155
133
with Client () as client :
156
- try :
157
- response = client .post (
158
- upload_data .url ,
159
- data = upload_data .fields ,
160
- files = {"file" : archive_path .open ("rb" )},
161
- )
162
-
163
- response .raise_for_status ()
164
- except (HTTPError , ReadTimeout ) as e :
165
- logger .debug (e )
166
- return False
134
+ response = client .post (
135
+ upload_data .url ,
136
+ data = upload_data .fields ,
137
+ files = {"file" : archive_path .open ("rb" )},
138
+ )
167
139
168
- return True
140
+ response . raise_for_status ()
169
141
170
142
171
143
class DeploymentResponse (BaseModel ):
@@ -176,19 +148,13 @@ class DeploymentResponse(BaseModel):
176
148
url : str
177
149
178
150
179
- def _get_deployment (app_id : str , deployment_id : str ) -> Optional [ DeploymentResponse ] :
151
+ def _get_deployment (app_id : str , deployment_id : str ) -> DeploymentResponse :
180
152
with APIClient () as client :
181
- try :
182
- response = client .get (f"/apps/{ app_id } /deployments/{ deployment_id } " )
183
- except (HTTPError , ReadTimeout ) as e :
184
- logger .debug (e )
185
- return None
153
+ response = client .get (f"/apps/{ app_id } /deployments/{ deployment_id } " )
154
+ response .raise_for_status ()
186
155
187
156
data = response .json ()
188
157
189
- if response .status_code != 200 :
190
- return None
191
-
192
158
return DeploymentResponse .model_validate (data )
193
159
194
160
@@ -220,12 +186,10 @@ def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
220
186
toolkit .print_line ()
221
187
222
188
with toolkit .progress ("Fetching teams..." ) as progress :
223
- teams = _get_teams ()
224
-
225
- if teams is None :
226
- progress .set_error ("Error fetching teams. Please try again later." )
227
-
228
- raise typer .Exit (1 )
189
+ with handle_http_errors (
190
+ progress , message = "Error fetching teams. Please try again later."
191
+ ):
192
+ teams = _get_teams ()
229
193
230
194
toolkit .print_line ()
231
195
@@ -246,12 +210,8 @@ def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
246
210
toolkit .print_line ()
247
211
248
212
with toolkit .progress (title = "Creating app..." ) as progress :
249
- app_data = _create_app (team_slug , app_name )
250
-
251
- if app_data is None :
252
- progress .set_error ("Error creating app. Please try again later." )
253
-
254
- raise typer .Exit (1 )
213
+ with handle_http_errors (progress ):
214
+ app_data = _create_app (team_slug , app_name )
255
215
256
216
progress .log (f"App created successfully! App slug: { app_data .slug } " )
257
217
@@ -278,14 +238,8 @@ def _wait_for_deployment(
278
238
279
239
with toolkit .progress ("Deploying..." ) as progress :
280
240
while True :
281
- deployment = _get_deployment (app_id , deployment_id )
282
-
283
- if deployment is None :
284
- progress .set_error (
285
- "[error]Error fetching deployment status.[/]\n \n Please try again later."
286
- )
287
-
288
- raise typer .Exit (1 ) from None
241
+ with handle_http_errors (progress ):
242
+ deployment = _get_deployment (app_id , deployment_id )
289
243
290
244
if deployment .status == DeploymentStatus .success :
291
245
progress .log (
@@ -354,25 +308,16 @@ def deploy(
354
308
archive_path = archive (path or Path .cwd ()) # noqa: F841
355
309
356
310
with toolkit .progress (title = "Creating deployment" ) as progress :
357
- deployment = _create_deployment (app_config .id )
358
-
359
- if deployment is None :
360
- progress .set_error ("Error creating deployment. Please try again later." )
361
-
362
- raise typer .Exit (1 )
363
-
364
- progress .log (
365
- f"Deployment created successfully! Deployment slug: { deployment .slug } "
366
- )
367
-
368
- progress .log ("Uploading deployment..." )
311
+ with handle_http_errors (progress ):
312
+ deployment = _create_deployment (app_config .id )
369
313
370
- if not _upload_deployment (deployment .id , archive_path ):
371
- progress .set_error (
372
- "[error]Something went while upload the archive[/]\n \n Please try again later."
314
+ progress .log (
315
+ f"Deployment created successfully! Deployment slug: { deployment .slug } "
373
316
)
374
317
375
- raise typer .Exit (1 ) from None
318
+ progress .log ("Uploading deployment..." )
319
+
320
+ _upload_deployment (deployment .id , archive_path )
376
321
377
322
progress .log ("Deployment uploaded successfully!" )
378
323
0 commit comments