Skip to content

Commit 9e8fa41

Browse files
committed
Add pydantic compat
1 parent 132a122 commit 9e8fa41

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

src/fastapi_cloud_cli/commands/deploy.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from fastapi_cloud_cli.utils.apps import AppConfig, get_app_config, write_app_config
2525
from fastapi_cloud_cli.utils.auth import is_logged_in
2626
from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
27+
from fastapi_cloud_cli.utils.pydantic_compat import model_validate
2728

2829
logger = logging.getLogger(__name__)
2930

@@ -91,7 +92,7 @@ def _get_teams() -> List[Team]:
9192

9293
data = response.json()["data"]
9394

94-
return [Team.model_validate(team) for team in data]
95+
return [model_validate(Team, team) for team in data]
9596

9697

9798
class AppResponse(BaseModel):
@@ -108,7 +109,7 @@ def _create_app(team_id: str, app_name: str) -> AppResponse:
108109

109110
response.raise_for_status()
110111

111-
return AppResponse.model_validate(response.json())
112+
return model_validate(AppResponse, response.json())
112113

113114

114115
class DeploymentStatus(str, Enum):
@@ -161,7 +162,7 @@ def _create_deployment(app_id: str) -> CreateDeploymentResponse:
161162
response = client.post(f"/apps/{app_id}/deployments/")
162163
response.raise_for_status()
163164

164-
return CreateDeploymentResponse.model_validate(response.json())
165+
return model_validate(CreateDeploymentResponse, response.json())
165166

166167

167168
class RequestUploadResponse(BaseModel):
@@ -186,7 +187,7 @@ def _upload_deployment(deployment_id: str, archive_path: Path) -> None:
186187
response = fastapi_client.post(f"/deployments/{deployment_id}/upload")
187188
response.raise_for_status()
188189

189-
upload_data = RequestUploadResponse.model_validate(response.json())
190+
upload_data = model_validate(RequestUploadResponse, response.json())
190191
logger.debug("Received upload URL: %s", upload_data.url)
191192

192193
# Upload the archive
@@ -221,7 +222,7 @@ def _get_app(app_slug: str) -> Optional[AppResponse]:
221222

222223
data = response.json()
223224

224-
return AppResponse.model_validate(data)
225+
return model_validate(AppResponse, data)
225226

226227

227228
def _get_apps(team_id: str) -> List[AppResponse]:
@@ -231,7 +232,7 @@ def _get_apps(team_id: str) -> List[AppResponse]:
231232

232233
data = response.json()["data"]
233234

234-
return [AppResponse.model_validate(app) for app in data]
235+
return [model_validate(AppResponse, app) for app in data]
235236

236237

237238
def _stream_build_logs(deployment_id: str) -> Generator[str, None, None]:
@@ -467,11 +468,12 @@ def _waitlist_form(toolkit: RichToolkit) -> None:
467468
result = form.run() # type: ignore
468469

469470
try:
470-
result = SignupToWaitingList.model_validate(
471+
result = model_validate(
472+
SignupToWaitingList,
471473
{
472474
"email": email,
473475
**result, # type: ignore
474-
}
476+
},
475477
)
476478
except ValidationError:
477479
toolkit.print(

src/fastapi_cloud_cli/commands/env.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from fastapi_cloud_cli.utils.auth import is_logged_in
1212
from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
1313
from fastapi_cloud_cli.utils.env import validate_environment_variable_name
14+
from fastapi_cloud_cli.utils.pydantic_compat import model_validate
1415

1516
logger = logging.getLogger(__name__)
1617

@@ -29,7 +30,7 @@ def _get_environment_variables(app_id: str) -> EnvironmentVariableResponse:
2930
response = client.get(f"/apps/{app_id}/environment-variables/")
3031
response.raise_for_status()
3132

32-
return EnvironmentVariableResponse.model_validate(response.json())
33+
return model_validate(EnvironmentVariableResponse, response.json())
3334

3435

3536
def _delete_environment_variable(app_id: str, name: str) -> bool:

src/fastapi_cloud_cli/commands/login.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
write_auth_config,
1717
)
1818
from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
19+
from fastapi_cloud_cli.utils.pydantic_compat import model_validate
1920

2021
logger = logging.getLogger(__name__)
2122

@@ -43,7 +44,7 @@ def _start_device_authorization(
4344

4445
response.raise_for_status()
4546

46-
return AuthorizationData.model_validate(response.json())
47+
return model_validate(AuthorizationData, response.json())
4748

4849

4950
def _fetch_access_token(client: httpx.Client, device_code: str, interval: int) -> str:
@@ -73,7 +74,7 @@ def _fetch_access_token(client: httpx.Client, device_code: str, interval: int) -
7374

7475
time.sleep(interval)
7576

76-
response_data = TokenResponse.model_validate(response.json())
77+
response_data = model_validate(TokenResponse, response.json())
7778

7879
return response_data.access_token
7980

src/fastapi_cloud_cli/utils/apps.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from pydantic import BaseModel
66

7+
from fastapi_cloud_cli.utils.pydantic_compat import model_validate_json
8+
79
logger = logging.getLogger("fastapi_cli")
810

911

@@ -21,7 +23,7 @@ def get_app_config(path_to_deploy: Path) -> Optional[AppConfig]:
2123
return None
2224

2325
logger.debug("App config loaded successfully")
24-
return AppConfig.model_validate_json(config_path.read_text(encoding="utf-8"))
26+
return model_validate_json(AppConfig, config_path.read_text(encoding="utf-8"))
2527

2628

2729
README = """

src/fastapi_cloud_cli/utils/auth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from pydantic import BaseModel
99

10+
from fastapi_cloud_cli.utils.pydantic_compat import model_validate_json
11+
1012
from .config import get_auth_path
1113

1214
logger = logging.getLogger("fastapi_cli")
@@ -44,7 +46,7 @@ def read_auth_config() -> Optional[AuthConfig]:
4446
return None
4547

4648
logger.debug("Auth config loaded successfully")
47-
return AuthConfig.model_validate_json(auth_path.read_text(encoding="utf-8"))
49+
return model_validate_json(AuthConfig, auth_path.read_text(encoding="utf-8"))
4850

4951

5052
def get_auth_token() -> Optional[str]:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import Any, Dict, Type, TypeVar
2+
3+
from pydantic import BaseModel
4+
from pydantic.version import VERSION as PYDANTIC_VERSION
5+
6+
PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(".")[:2])
7+
PYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2
8+
9+
10+
Model = TypeVar("Model", bound=BaseModel)
11+
12+
13+
def model_validate(model_class: Type[Model], data: Dict[Any, Any]) -> Model:
14+
if PYDANTIC_V2:
15+
return model_class.model_validate(data) # type: ignore[no-any-return, unused-ignore, attr-defined]
16+
else:
17+
return model_class.parse_obj(data) # type: ignore[no-any-return, unused-ignore, attr-defined]
18+
19+
20+
def model_validate_json(model_class: Type[Model], data: str) -> Model:
21+
if PYDANTIC_V2:
22+
return model_class.model_validate_json(data) # type: ignore[no-any-return, unused-ignore, attr-defined]
23+
else:
24+
return model_class.parse_raw(data) # type: ignore[no-any-return, unused-ignore, attr-defined]

0 commit comments

Comments
 (0)