Changed code to support older Python versions

This commit is contained in:
Malasaur 2025-12-01 23:27:09 +01:00
parent eb92d2d36f
commit 582458cdd0
5027 changed files with 794942 additions and 4 deletions

View file

@ -0,0 +1 @@
__version__ = "0.5.2"

View file

@ -0,0 +1,3 @@
from .cli import main
main()

View file

@ -0,0 +1,43 @@
import typer
from .commands.deploy import deploy
from .commands.env import env_app
from .commands.login import login
from .commands.logout import logout
from .commands.unlink import unlink
from .commands.whoami import whoami
from .logging import setup_logging
from .utils.sentry import init_sentry
setup_logging()
app = typer.Typer(rich_markup_mode="rich")
cloud_app = typer.Typer(
rich_markup_mode="rich",
help="Manage [bold]FastAPI[/bold] Cloud deployments. 🚀",
)
# TODO: use the app structure
# Additional commands
# fastapi cloud [command]
cloud_app.command()(deploy)
cloud_app.command()(login)
cloud_app.command()(logout)
cloud_app.command()(whoami)
cloud_app.command()(unlink)
cloud_app.add_typer(env_app, name="env")
# fastapi [command]
app.command()(deploy)
app.command()(login)
app.add_typer(cloud_app, name="cloud")
def main() -> None:
init_sentry()
app()

View file

@ -0,0 +1,621 @@
import contextlib
import logging
import subprocess
import tempfile
import time
from enum import Enum
from itertools import cycle
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
import fastar
import rignore
import typer
from httpx import Client
from pydantic import BaseModel, EmailStr, ValidationError
from rich.text import Text
from rich_toolkit import RichToolkit
from rich_toolkit.menu import Option
from typing_extensions import Annotated
from fastapi_cloud_cli.commands.login import login
from fastapi_cloud_cli.utils.api import APIClient, BuildLogError, TooManyRetriesError
from fastapi_cloud_cli.utils.apps import AppConfig, get_app_config, write_app_config
from fastapi_cloud_cli.utils.auth import is_logged_in
from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
from fastapi_cloud_cli.utils.pydantic_compat import (
TypeAdapter,
model_dump,
model_validate,
)
logger = logging.getLogger(__name__)
def _get_app_name(path: Path) -> str:
# TODO: use pyproject.toml to get the app name
return path.name
def _should_exclude_entry(path: Path) -> bool:
parts_to_exclude = [
".venv",
"__pycache__",
".mypy_cache",
".pytest_cache",
".gitignore",
".fastapicloudignore",
]
if any(part in path.parts for part in parts_to_exclude):
return True
if path.suffix == ".pyc":
return True
return False
def archive(path: Path, tar_path: Path) -> Path:
logger.debug("Starting archive creation for path: %s", path)
files = rignore.walk(
path,
should_exclude_entry=_should_exclude_entry,
additional_ignore_paths=[".fastapicloudignore"],
ignore_hidden=False,
)
logger.debug("Archive will be created at: %s", tar_path)
file_count = 0
with fastar.open(tar_path, "w") as tar:
for filename in files:
if filename.is_dir():
continue
arcname = filename.relative_to(path)
logger.debug("Adding %s to archive", arcname)
tar.append(filename, arcname=arcname)
file_count += 1
logger.debug("Archive created successfully with %s files", file_count)
return tar_path
class Team(BaseModel):
id: str
slug: str
name: str
def _get_teams() -> List[Team]:
with APIClient() as client:
response = client.get("/teams/")
response.raise_for_status()
data = response.json()["data"]
return [model_validate(Team, team) for team in data]
class AppResponse(BaseModel):
id: str
slug: str
def _create_app(team_id: str, app_name: str) -> AppResponse:
with APIClient() as client:
response = client.post(
"/apps/",
json={"name": app_name, "team_id": team_id},
)
response.raise_for_status()
return model_validate(AppResponse, response.json())
class DeploymentStatus(str, Enum):
waiting_upload = "waiting_upload"
ready_for_build = "ready_for_build"
building = "building"
extracting = "extracting"
extracting_failed = "extracting_failed"
building_image = "building_image"
building_image_failed = "building_image_failed"
deploying = "deploying"
deploying_failed = "deploying_failed"
verifying = "verifying"
verifying_failed = "verifying_failed"
verifying_skipped = "verifying_skipped"
success = "success"
failed = "failed"
@classmethod
def to_human_readable(cls, status: "DeploymentStatus") -> str:
return {
cls.waiting_upload: "Waiting for upload",
cls.ready_for_build: "Ready for build",
cls.building: "Building",
cls.extracting: "Extracting",
cls.extracting_failed: "Extracting failed",
cls.building_image: "Building image",
cls.building_image_failed: "Build failed",
cls.deploying: "Deploying",
cls.deploying_failed: "Deploying failed",
cls.verifying: "Verifying",
cls.verifying_failed: "Verifying failed",
cls.verifying_skipped: "Verification skipped",
cls.success: "Success",
cls.failed: "Failed",
}[status]
class CreateDeploymentResponse(BaseModel):
id: str
app_id: str
slug: str
status: DeploymentStatus
dashboard_url: str
url: str
def _create_deployment(app_id: str) -> CreateDeploymentResponse:
with APIClient() as client:
response = client.post(f"/apps/{app_id}/deployments/")
response.raise_for_status()
return model_validate(CreateDeploymentResponse, response.json())
class RequestUploadResponse(BaseModel):
url: str
fields: Dict[str, str]
def _upload_deployment(deployment_id: str, archive_path: Path) -> None:
logger.debug(
"Starting deployment upload for deployment: %s",
deployment_id,
)
logger.debug(
"Archive path: %s, size: %s bytes",
archive_path,
archive_path.stat().st_size,
)
with APIClient() as fastapi_client, Client() as client:
# Get the upload URL
logger.debug("Requesting upload URL from API")
response = fastapi_client.post(f"/deployments/{deployment_id}/upload")
response.raise_for_status()
upload_data = model_validate(RequestUploadResponse, response.json())
logger.debug("Received upload URL: %s", upload_data.url)
logger.debug("Starting file upload to S3")
with open(archive_path, "rb") as archive_file:
upload_response = client.post(
upload_data.url,
data=upload_data.fields,
files={"file": archive_file},
)
upload_response.raise_for_status()
logger.debug("File upload completed successfully")
# Notify the server that the upload is complete
logger.debug("Notifying API that upload is complete")
notify_response = fastapi_client.post(
f"/deployments/{deployment_id}/upload-complete"
)
notify_response.raise_for_status()
logger.debug("Upload notification sent successfully")
def _get_app(app_slug: str) -> Optional[AppResponse]:
with APIClient() as client:
response = client.get(f"/apps/{app_slug}")
if response.status_code == 404:
return None
response.raise_for_status()
data = response.json()
return model_validate(AppResponse, data)
def _get_apps(team_id: str) -> List[AppResponse]:
with APIClient() as client:
response = client.get("/apps/", params={"team_id": team_id})
response.raise_for_status()
data = response.json()["data"]
return [model_validate(AppResponse, app) for app in data]
WAITING_MESSAGES = [
"🚀 Preparing for liftoff! Almost there...",
"👹 Sneaking past the dependency gremlins... Don't wake them up!",
"🤏 Squishing code into a tiny digital sandwich. Nom nom nom.",
"🐱 Removing cat videos from our servers to free up space.",
"🐢 Uploading at blazing speeds of 1 byte per hour. Patience, young padawan.",
"🔌 Connecting to server... Please stand by while we argue with the firewall.",
"💥 Oops! We've angered the Python God. Sacrificing a rubber duck to appease it.",
"🧙 Sprinkling magic deployment dust. Abracadabra!",
"👀 Hoping that @tiangolo doesn't find out about this deployment.",
"🍪 Cookie monster detected on server. Deploying anti-cookie shields.",
]
LONG_WAIT_MESSAGES = [
"😅 Well, that's embarrassing. We're still waiting for the deployment to finish...",
"🤔 Maybe we should have brought snacks for this wait...",
"🥱 Yawn... Still waiting...",
"🤯 Time is relative... Especially when you're waiting for a deployment...",
]
def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
if not toolkit.confirm(f"Setup and deploy [blue]{path_to_deploy}[/]?", tag="dir"):
raise typer.Exit(0)
toolkit.print_line()
with toolkit.progress("Fetching teams...") as progress:
with handle_http_errors(
progress, message="Error fetching teams. Please try again later."
):
teams = _get_teams()
toolkit.print_line()
team = toolkit.ask(
"Select the team you want to deploy to:",
tag="team",
options=[Option({"name": team.name, "value": team}) for team in teams],
)
toolkit.print_line()
create_new_app = toolkit.confirm(
"Do you want to create a new app?", tag="app", default=True
)
toolkit.print_line()
if not create_new_app:
with toolkit.progress("Fetching apps...") as progress:
with handle_http_errors(
progress, message="Error fetching apps. Please try again later."
):
apps = _get_apps(team.id)
toolkit.print_line()
if not apps:
toolkit.print(
"No apps found in this team. You can create a new app instead.",
)
raise typer.Exit(1)
app = toolkit.ask(
"Select the app you want to deploy to:",
options=[Option({"name": app.slug, "value": app}) for app in apps],
)
else:
app_name = toolkit.input(
title="What's your app name?",
default=_get_app_name(path_to_deploy),
)
toolkit.print_line()
with toolkit.progress(title="Creating app...") as progress:
with handle_http_errors(progress):
app = _create_app(team.id, app_name)
progress.log(f"App created successfully! App slug: {app.slug}")
app_config = AppConfig(app_id=app.id, team_id=team.id)
write_app_config(path_to_deploy, app_config)
return app_config
def _wait_for_deployment(
toolkit: RichToolkit, app_id: str, deployment: CreateDeploymentResponse
) -> None:
messages = cycle(WAITING_MESSAGES)
toolkit.print(
"Checking the status of your deployment 👀",
tag="cloud",
)
toolkit.print_line()
toolkit.print(
f"You can also check the status at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]",
)
toolkit.print_line()
time_elapsed = 0.0
started_at = time.monotonic()
last_message_changed_at = time.monotonic()
with toolkit.progress(
next(messages), inline_logs=True, lines_to_show=20
) as progress, APIClient() as client:
try:
for log in client.stream_build_logs(deployment.id):
time_elapsed = time.monotonic() - started_at
if log.type == "message":
progress.log(Text.from_ansi(log.message.rstrip()))
if log.type == "complete":
progress.log("")
progress.log(
f"🐔 Ready the chicken! Your app is ready at [link={deployment.url}]{deployment.url}[/link]"
)
progress.log("")
progress.log(
f"You can also check the app logs at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]"
)
break
if log.type == "failed":
progress.log("")
progress.log(
f"😔 Oh no! Something went wrong. Check out the logs at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]"
)
raise typer.Exit(1)
if time_elapsed > 30:
messages = cycle(LONG_WAIT_MESSAGES)
if (time.monotonic() - last_message_changed_at) > 2:
progress.title = next(messages)
last_message_changed_at = time.monotonic()
except (BuildLogError, TooManyRetriesError) as e:
logger.error("Build log streaming failed: %s", e)
toolkit.print_line()
toolkit.print(
f"⚠️ Unable to stream build logs. Check the dashboard for status: [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]"
)
raise typer.Exit(1) from e
class SignupToWaitingList(BaseModel):
email: EmailStr
name: Optional[str] = None
organization: Optional[str] = None
role: Optional[str] = None
team_size: Optional[str] = None
location: Optional[str] = None
use_case: Optional[str] = None
secret_code: Optional[str] = None
def _send_waitlist_form(
result: SignupToWaitingList,
toolkit: RichToolkit,
) -> None:
with toolkit.progress("Sending your request...") as progress:
with APIClient() as client:
with handle_http_errors(progress):
response = client.post("/users/waiting-list", json=model_dump(result))
response.raise_for_status()
progress.log("Let's go! Thanks for your interest in FastAPI Cloud! 🚀")
def _waitlist_form(toolkit: RichToolkit) -> None:
from rich_toolkit.form import Form
toolkit.print(
"We're currently in private beta. If you want to be notified when we launch, please fill out the form below.",
tag="waitlist",
)
toolkit.print_line()
email = toolkit.input(
"Enter your email:",
required=True,
validator=TypeAdapter(EmailStr),
)
toolkit.print_line()
result = model_validate(SignupToWaitingList, {"email": email})
if toolkit.confirm(
"Do you want to get access faster by giving us more information?",
tag="waitlist",
):
toolkit.print_line()
form = Form("Waitlist form", style=toolkit.style)
form.add_input("name", label="Name", placeholder="John Doe")
form.add_input("organization", label="Organization", placeholder="Acme Inc.")
form.add_input("team", label="Team", placeholder="Team A")
form.add_input("role", label="Role", placeholder="Developer")
form.add_input("location", label="Location", placeholder="San Francisco")
form.add_input(
"use_case",
label="How do you plan to use FastAPI Cloud?",
placeholder="I'm building a web app",
)
form.add_input("secret_code", label="Secret code", placeholder="123456")
result = form.run() # type: ignore
try:
result = model_validate(
SignupToWaitingList,
{
"email": email,
**result, # type: ignore
},
)
except ValidationError:
toolkit.print(
"[error]Invalid form data. Please try again.[/]",
)
return
toolkit.print_line()
if toolkit.confirm(
(
"Do you agree to\n"
"- Terms of Service: [link=https://fastapicloud.com/legal/terms]https://fastapicloud.com/legal/terms[/link]\n"
"- Privacy Policy: [link=https://fastapicloud.com/legal/privacy-policy]https://fastapicloud.com/legal/privacy-policy[/link]\n"
),
tag="terms",
):
toolkit.print_line()
_send_waitlist_form(
result,
toolkit,
)
with contextlib.suppress(Exception):
subprocess.run(
["open", "-g", "raycast://confetti?emojis=🐔⚡"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
def deploy(
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to the folder containing the app you want to deploy"
),
] = None,
skip_wait: Annotated[
bool, typer.Option("--no-wait", help="Skip waiting for deployment status")
] = False,
) -> Any:
"""
Deploy a [bold]FastAPI[/bold] app to FastAPI Cloud. 🚀
"""
logger.debug("Deploy command started")
logger.debug("Deploy path: %s, skip_wait: %s", path, skip_wait)
with get_rich_toolkit() as toolkit:
if not is_logged_in():
logger.debug("User not logged in, prompting for login or waitlist")
toolkit.print_title("Welcome to FastAPI Cloud!", tag="FastAPI")
toolkit.print_line()
toolkit.print(
"You need to be logged in to deploy to FastAPI Cloud.",
tag="info",
)
toolkit.print_line()
choice = toolkit.ask(
"What would you like to do?",
tag="auth",
options=[
Option({"name": "Login to my existing account", "value": "login"}),
Option({"name": "Join the waiting list", "value": "waitlist"}),
],
)
toolkit.print_line()
if choice == "login":
login()
else:
_waitlist_form(toolkit)
raise typer.Exit(1)
toolkit.print_title("Starting deployment", tag="FastAPI")
toolkit.print_line()
path_to_deploy = path or Path.cwd()
logger.debug("Deploying from path: %s", path_to_deploy)
app_config = get_app_config(path_to_deploy)
if not app_config:
logger.debug("No app config found, configuring new app")
app_config = _configure_app(toolkit, path_to_deploy=path_to_deploy)
toolkit.print_line()
else:
logger.debug("Existing app config found, proceeding with deployment")
toolkit.print("Deploying app...")
toolkit.print_line()
with toolkit.progress("Checking app...", transient=True) as progress:
with handle_http_errors(progress):
logger.debug("Checking app with ID: %s", app_config.app_id)
app = _get_app(app_config.app_id)
if not app:
logger.debug("App not found in API")
progress.set_error(
"App not found. Make sure you're logged in the correct account."
)
if not app:
toolkit.print_line()
toolkit.print(
"If you deleted this app, you can run [bold]fastapi cloud unlink[/] to unlink the local configuration.",
tag="tip",
)
raise typer.Exit(1)
with tempfile.TemporaryDirectory() as temp_dir:
logger.debug("Creating archive for deployment")
archive_path = Path(temp_dir) / "archive.tar"
archive(path or Path.cwd(), archive_path)
with toolkit.progress(
title="Creating deployment"
) as progress, handle_http_errors(progress):
logger.debug("Creating deployment for app: %s", app.id)
deployment = _create_deployment(app.id)
progress.log(
f"Deployment created successfully! Deployment slug: {deployment.slug}"
)
progress.log("Uploading deployment...")
_upload_deployment(deployment.id, archive_path)
progress.log("Deployment uploaded successfully!")
toolkit.print_line()
if not skip_wait:
logger.debug("Waiting for deployment to complete")
_wait_for_deployment(toolkit, app.id, deployment=deployment)
else:
logger.debug("Skipping deployment wait as requested")
toolkit.print(
f"Check the status of your deployment at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]"
)

View file

@ -0,0 +1,249 @@
import logging
from pathlib import Path
from typing import Any, List, Union
import typer
from pydantic import BaseModel
from typing_extensions import Annotated
from fastapi_cloud_cli.utils.api import APIClient
from fastapi_cloud_cli.utils.apps import get_app_config
from fastapi_cloud_cli.utils.auth import is_logged_in
from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
from fastapi_cloud_cli.utils.env import validate_environment_variable_name
from fastapi_cloud_cli.utils.pydantic_compat import model_validate
logger = logging.getLogger(__name__)
class EnvironmentVariable(BaseModel):
name: str
value: str
class EnvironmentVariableResponse(BaseModel):
data: List[EnvironmentVariable]
def _get_environment_variables(app_id: str) -> EnvironmentVariableResponse:
with APIClient() as client:
response = client.get(f"/apps/{app_id}/environment-variables/")
response.raise_for_status()
return model_validate(EnvironmentVariableResponse, response.json())
def _delete_environment_variable(app_id: str, name: str) -> bool:
with APIClient() as client:
response = client.delete(f"/apps/{app_id}/environment-variables/{name}")
if response.status_code == 404:
return False
response.raise_for_status()
return True
def _set_environment_variable(
app_id: str, name: str, value: str, is_secret: bool = False
) -> None:
with APIClient() as client:
response = client.post(
f"/apps/{app_id}/environment-variables/",
json={"name": name, "value": value, "is_secret": is_secret},
)
response.raise_for_status()
env_app = typer.Typer()
@env_app.command()
def list(
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to the folder containing the app you want to deploy"
),
] = None,
) -> Any:
"""
List the environment variables for the app.
"""
with get_rich_toolkit(minimal=True) as toolkit:
if not is_logged_in():
toolkit.print(
"No credentials found. Use [blue]`fastapi login`[/] to login.",
tag="auth",
)
raise typer.Exit(1)
app_path = path or Path.cwd()
app_config = get_app_config(app_path)
if not app_config:
toolkit.print(
f"No app found in the folder [bold]{app_path}[/].",
)
raise typer.Exit(1)
with toolkit.progress(
"Fetching environment variables...", transient=True
) as progress:
with handle_http_errors(progress):
environment_variables = _get_environment_variables(app_config.app_id)
if not environment_variables.data:
toolkit.print("No environment variables found.")
return
toolkit.print("Environment variables:")
toolkit.print_line()
for env_var in environment_variables.data:
toolkit.print(f"[bold]{env_var.name}[/]")
@env_app.command()
def delete(
name: Union[str, None] = typer.Argument(
None,
help="The name of the environment variable to delete",
),
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to the folder containing the app you want to deploy"
),
] = None,
) -> Any:
"""
Delete an environment variable from the app.
"""
with get_rich_toolkit(minimal=True) as toolkit:
# TODO: maybe this logic can be extracted to a function
if not is_logged_in():
toolkit.print(
"No credentials found. Use [blue]`fastapi login`[/] to login.",
tag="auth",
)
raise typer.Exit(1)
path_to_deploy = path or Path.cwd()
app_config = get_app_config(path_to_deploy)
if not app_config:
toolkit.print(
f"No app found in the folder [bold]{path_to_deploy}[/].",
)
raise typer.Exit(1)
if not name:
with toolkit.progress(
"Fetching environment variables...", transient=True
) as progress:
with handle_http_errors(progress):
environment_variables = _get_environment_variables(
app_config.app_id
)
if not environment_variables.data:
toolkit.print("No environment variables found.")
return
name = toolkit.ask(
"Select the environment variable to delete:",
options=[
{"name": env_var.name, "value": env_var.name}
for env_var in environment_variables.data
],
)
assert name
else:
if not validate_environment_variable_name(name):
toolkit.print(
f"The environment variable name [bold]{name}[/] is invalid."
)
raise typer.Exit(1)
toolkit.print_line()
with toolkit.progress(
"Deleting environment variable", transient=True
) as progress:
with handle_http_errors(progress):
deleted = _delete_environment_variable(app_config.app_id, name)
if not deleted:
toolkit.print("Environment variable not found.")
raise typer.Exit(1)
toolkit.print(f"Environment variable [bold]{name}[/] deleted.")
@env_app.command()
def set(
name: Union[str, None] = typer.Argument(
None,
help="The name of the environment variable to set",
),
value: Union[str, None] = typer.Argument(
None,
help="The value of the environment variable to set",
),
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to the folder containing the app you want to deploy"
),
] = None,
) -> Any:
"""
Set an environment variable for the app.
"""
with get_rich_toolkit(minimal=True) as toolkit:
if not is_logged_in():
toolkit.print(
"No credentials found. Use [blue]`fastapi login`[/] to login.",
tag="auth",
)
raise typer.Exit(1)
path_to_deploy = path or Path.cwd()
app_config = get_app_config(path_to_deploy)
if not app_config:
toolkit.print(
f"No app found in the folder [bold]{path_to_deploy}[/].",
)
raise typer.Exit(1)
if not name:
name = toolkit.input("Enter the name of the environment variable to set:")
if not value:
value = toolkit.input(
"Enter the value of the environment variable to set:", password=True
)
with toolkit.progress(
"Setting environment variable", transient=True
) as progress:
assert name is not None
assert value is not None
with handle_http_errors(progress):
_set_environment_variable(app_config.app_id, name, value)
toolkit.print(f"Environment variable [bold]{name}[/] set.")

View file

@ -0,0 +1,125 @@
import logging
import time
from typing import Any
import httpx
import typer
from pydantic import BaseModel
from fastapi_cloud_cli.config import Settings
from fastapi_cloud_cli.utils.api import APIClient
from fastapi_cloud_cli.utils.auth import (
AuthConfig,
get_auth_token,
is_logged_in,
is_token_expired,
write_auth_config,
)
from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
from fastapi_cloud_cli.utils.pydantic_compat import model_validate_json
logger = logging.getLogger(__name__)
class AuthorizationData(BaseModel):
user_code: str
device_code: str
verification_uri: str
verification_uri_complete: str
interval: int = 5
class TokenResponse(BaseModel):
access_token: str
def _start_device_authorization(
client: httpx.Client,
) -> AuthorizationData:
settings = Settings.get()
response = client.post(
"/login/device/authorization", data={"client_id": settings.client_id}
)
response.raise_for_status()
return model_validate_json(AuthorizationData, response.text)
def _fetch_access_token(client: httpx.Client, device_code: str, interval: int) -> str:
settings = Settings.get()
while True:
response = client.post(
"/login/device/token",
data={
"device_code": device_code,
"client_id": settings.client_id,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
)
if response.status_code not in (200, 400):
response.raise_for_status()
if response.status_code == 400:
data = response.json()
if data.get("error") != "authorization_pending":
response.raise_for_status()
if response.status_code == 200:
break
time.sleep(interval)
response_data = model_validate_json(TokenResponse, response.text)
return response_data.access_token
def login() -> Any:
"""
Login to FastAPI Cloud. 🚀
"""
token = get_auth_token()
if token is not None and is_token_expired(token):
with get_rich_toolkit(minimal=True) as toolkit:
toolkit.print("Your session has expired. Logging in again...")
toolkit.print_line()
if is_logged_in():
with get_rich_toolkit(minimal=True) as toolkit:
toolkit.print("You are already logged in.")
toolkit.print(
"Run [bold]fastapi cloud logout[/bold] first if you want to switch accounts."
)
return
with get_rich_toolkit() as toolkit, APIClient() as client:
toolkit.print_title("Login to FastAPI Cloud", tag="FastAPI")
toolkit.print_line()
with toolkit.progress("Starting authorization") as progress:
with handle_http_errors(progress):
authorization_data = _start_device_authorization(client)
url = authorization_data.verification_uri_complete
progress.log(f"Opening [link={url}]{url}[/link]")
toolkit.print_line()
with toolkit.progress("Waiting for user to authorize...") as progress:
typer.launch(url)
with handle_http_errors(progress):
access_token = _fetch_access_token(
client, authorization_data.device_code, authorization_data.interval
)
write_auth_config(AuthConfig(access_token=access_token))
progress.log("Now you are logged in! 🚀")

View file

@ -0,0 +1,12 @@
from fastapi_cloud_cli.utils.auth import delete_auth_config
from fastapi_cloud_cli.utils.cli import get_rich_toolkit
def logout() -> None:
"""
Logout from FastAPI Cloud. 🚀
"""
with get_rich_toolkit(minimal=True) as toolkit:
delete_auth_config()
toolkit.print("You are now logged out! 🚀")

View file

@ -0,0 +1,29 @@
import logging
import shutil
from pathlib import Path
from typing import Any
import typer
from fastapi_cloud_cli.utils.cli import get_rich_toolkit
logger = logging.getLogger(__name__)
def unlink() -> Any:
"""
Unlink by deleting the `.fastapicloud` directory.
"""
with get_rich_toolkit(minimal=True) as toolkit:
config_dir = Path.cwd() / ".fastapicloud"
if not config_dir.exists():
toolkit.print(
"No FastAPI Cloud configuration found in the current directory."
)
logger.debug(f"Configuration directory not found: {config_dir}")
raise typer.Exit(1)
shutil.rmtree(config_dir)
toolkit.print("FastAPI Cloud configuration has been unlinked successfully! 🚀")
logger.debug(f"Deleted configuration directory: {config_dir}")

View file

@ -0,0 +1,27 @@
import logging
from typing import Any
from rich import print
from rich_toolkit.progress import Progress
from fastapi_cloud_cli.utils.api import APIClient
from fastapi_cloud_cli.utils.auth import is_logged_in
from fastapi_cloud_cli.utils.cli import handle_http_errors
logger = logging.getLogger(__name__)
def whoami() -> Any:
if not is_logged_in():
print("No credentials found. Use [blue]`fastapi login`[/] to login.")
return
with APIClient() as client:
with Progress(title="⚡ Fetching profile", transient=True) as progress:
with handle_http_errors(progress, message=""):
response = client.get("/users/me")
response.raise_for_status()
data = response.json()
print(f"⚡ [bold]{data['email']}[/bold]")

View file

@ -0,0 +1,26 @@
import json
from pathlib import Path
from pydantic import BaseModel
from .utils.config import get_cli_config_path
class Settings(BaseModel):
base_api_url: str = "https://api.fastapicloud.com/api/v1"
client_id: str = "fastapi-cli"
@classmethod
def from_user_settings(cls, config_path: Path) -> "Settings":
try:
content = config_path.read_bytes() if config_path.exists() else b"{}"
user_settings = json.loads(content)
except json.JSONDecodeError:
user_settings = {}
return cls(**user_settings)
@classmethod
def get(cls) -> "Settings":
return cls.from_user_settings(get_cli_config_path())

View file

@ -0,0 +1,31 @@
import logging
import os
from typing import Union
from rich.console import Console
from rich.logging import RichHandler
def setup_logging(
terminal_width: Union[int, None] = None, level: Union[int, None] = None
) -> None:
if level is None:
level = (
logging.DEBUG if os.getenv("FASTAPI_CLOUD_DEBUG") == "1" else logging.INFO
)
logger = logging.getLogger("fastapi_cloud_cli")
console = Console(width=terminal_width) if terminal_width else None
rich_handler = RichHandler(
show_time=False,
rich_tracebacks=True,
tracebacks_show_locals=True,
markup=True,
show_path=False,
console=console,
)
rich_handler.setFormatter(logging.Formatter("{message}", style="{"))
logger.addHandler(rich_handler)
logger.setLevel(level)
logger.propagate = False

View file

@ -0,0 +1,195 @@
import json
import logging
import time
from contextlib import contextmanager
from datetime import timedelta
from functools import wraps
from typing import (
Callable,
Generator,
Literal,
Optional,
TypeVar,
Union,
)
import httpx
from pydantic import BaseModel, Field, ValidationError
from typing_extensions import Annotated, ParamSpec
from fastapi_cloud_cli import __version__
from fastapi_cloud_cli.config import Settings
from fastapi_cloud_cli.utils.auth import get_auth_token
from fastapi_cloud_cli.utils.pydantic_compat import TypeAdapter
logger = logging.getLogger(__name__)
BUILD_LOG_MAX_RETRIES = 3
BUILD_LOG_TIMEOUT = timedelta(minutes=5)
class BuildLogError(Exception):
pass
class TooManyRetriesError(Exception):
pass
class BuildLogLineGeneric(BaseModel):
type: Literal["complete", "failed", "timeout", "heartbeat"]
id: Optional[str] = None
class BuildLogLineMessage(BaseModel):
type: Literal["message"] = "message"
message: str
id: Optional[str] = None
BuildLogLine = Union[BuildLogLineMessage, BuildLogLineGeneric]
BuildLogAdapter = TypeAdapter[BuildLogLine](
Annotated[BuildLogLine, Field(discriminator="type")] # type: ignore
)
@contextmanager
def attempt(attempt_number: int) -> Generator[None, None, None]:
def _backoff() -> None:
backoff_seconds = min(2**attempt_number, 30)
logger.debug(
"Retrying in %ds (attempt %d)",
backoff_seconds,
attempt_number,
)
time.sleep(backoff_seconds)
try:
yield
except (
httpx.TimeoutException,
httpx.NetworkError,
httpx.RemoteProtocolError,
) as error:
logger.debug("Network error (will retry): %s", error)
_backoff()
except httpx.HTTPStatusError as error:
if error.response.status_code >= 500:
logger.debug(
"Server error %d (will retry): %s",
error.response.status_code,
error,
)
_backoff()
else:
# Try to get response text, but handle streaming responses gracefully
try:
error_detail = error.response.text
except Exception:
error_detail = "(response body unavailable)"
raise BuildLogError(
f"HTTP {error.response.status_code}: {error_detail}"
) from error
P = ParamSpec("P")
T = TypeVar("T")
def attempts(
total_attempts: int = 3, timeout: timedelta = timedelta(minutes=5)
) -> Callable[
[Callable[P, Generator[T, None, None]]], Callable[P, Generator[T, None, None]]
]:
def decorator(
func: Callable[P, Generator[T, None, None]],
) -> Callable[P, Generator[T, None, None]]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Generator[T, None, None]:
start = time.monotonic()
for attempt_number in range(total_attempts):
if time.monotonic() - start > timeout.total_seconds():
raise TimeoutError(
"Build log streaming timed out after %ds",
timeout.total_seconds(),
)
with attempt(attempt_number):
yield from func(*args, **kwargs)
# If we get here without exception, the generator completed successfully
return
raise TooManyRetriesError(f"Failed after {total_attempts} attempts")
return wrapper
return decorator
class APIClient(httpx.Client):
def __init__(self) -> None:
settings = Settings.get()
token = get_auth_token()
super().__init__(
base_url=settings.base_api_url,
timeout=httpx.Timeout(20),
headers={
"Authorization": f"Bearer {token}",
"User-Agent": f"fastapi-cloud-cli/{__version__}",
},
)
@attempts(BUILD_LOG_MAX_RETRIES, BUILD_LOG_TIMEOUT)
def stream_build_logs(
self, deployment_id: str
) -> Generator[BuildLogLine, None, None]:
last_id = None
while True:
params = {"last_id": last_id} if last_id else None
with self.stream(
"GET",
f"/deployments/{deployment_id}/build-logs",
timeout=60,
params=params,
) as response:
response.raise_for_status()
for line in response.iter_lines():
if not line or not line.strip():
continue
if log_line := self._parse_log_line(line):
if log_line.id:
last_id = log_line.id
if log_line.type == "message":
yield log_line
if log_line.type in ("complete", "failed"):
yield log_line
return
if log_line.type == "timeout":
logger.debug("Received timeout; reconnecting")
break # Breaks for loop to reconnect
else:
logger.debug("Connection closed by server unexpectedly; will retry")
raise httpx.NetworkError("Connection closed without terminal state")
time.sleep(0.5)
def _parse_log_line(self, line: str) -> Optional[BuildLogLine]:
try:
return BuildLogAdapter.validate_json(line)
except (ValidationError, json.JSONDecodeError) as e:
logger.debug("Skipping malformed log: %s (error: %s)", line[:100], e)
return None

View file

@ -0,0 +1,61 @@
import logging
from pathlib import Path
from typing import Optional
from pydantic import BaseModel
from fastapi_cloud_cli.utils.pydantic_compat import model_dump_json, model_validate_json
logger = logging.getLogger("fastapi_cli")
class AppConfig(BaseModel):
app_id: str
team_id: str
def get_app_config(path_to_deploy: Path) -> Optional[AppConfig]:
config_path = path_to_deploy / ".fastapicloud/cloud.json"
logger.debug("Looking for app config at: %s", config_path)
if not config_path.exists():
logger.debug("App config file doesn't exist")
return None
logger.debug("App config loaded successfully")
return model_validate_json(AppConfig, config_path.read_text(encoding="utf-8"))
README = """
> Why do I have a folder named ".fastapicloud" in my project? 🤔
The ".fastapicloud" folder is created when you link a directory to a FastAPI Cloud project.
> What does the "cloud.json" file contain?
The "cloud.json" file contains:
- The ID of the FastAPI app that you linked ("app_id")
- The ID of the team your FastAPI Cloud project is owned by ("team_id")
> Should I commit the ".fastapicloud" folder?
No, you should not commit the ".fastapicloud" folder to your version control system.
That's why there's a ".gitignore" file in this folder.
"""
def write_app_config(path_to_deploy: Path, app_config: AppConfig) -> None:
config_path = path_to_deploy / ".fastapicloud/cloud.json"
readme_path = path_to_deploy / ".fastapicloud/README.md"
gitignore_path = path_to_deploy / ".fastapicloud/.gitignore"
logger.debug("Writing app config to: %s", config_path)
logger.debug("App config data: %s", app_config)
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(
model_dump_json(app_config),
encoding="utf-8",
)
readme_path.write_text(README, encoding="utf-8")
gitignore_path.write_text("*")
logger.debug("App config files written successfully")

View file

@ -0,0 +1,124 @@
import base64
import binascii
import json
import logging
import time
from typing import Optional
from pydantic import BaseModel
from fastapi_cloud_cli.utils.pydantic_compat import model_dump_json, model_validate_json
from .config import get_auth_path
logger = logging.getLogger("fastapi_cli")
class AuthConfig(BaseModel):
access_token: str
def write_auth_config(auth_data: AuthConfig) -> None:
auth_path = get_auth_path()
logger.debug("Writing auth config to: %s", auth_path)
auth_path.write_text(model_dump_json(auth_data), encoding="utf-8")
logger.debug("Auth config written successfully")
def delete_auth_config() -> None:
auth_path = get_auth_path()
logger.debug("Deleting auth config at: %s", auth_path)
if auth_path.exists():
auth_path.unlink()
logger.debug("Auth config deleted successfully")
else:
logger.debug("Auth config file doesn't exist, nothing to delete")
def read_auth_config() -> Optional[AuthConfig]:
auth_path = get_auth_path()
logger.debug("Reading auth config from: %s", auth_path)
if not auth_path.exists():
logger.debug("Auth config file doesn't exist")
return None
logger.debug("Auth config loaded successfully")
return model_validate_json(AuthConfig, auth_path.read_text(encoding="utf-8"))
def get_auth_token() -> Optional[str]:
logger.debug("Getting auth token")
auth_data = read_auth_config()
if auth_data is None:
logger.debug("No auth data found")
return None
logger.debug("Auth token retrieved successfully")
return auth_data.access_token
def is_token_expired(token: str) -> bool:
try:
parts = token.split(".")
if len(parts) != 3:
logger.debug("Invalid JWT format: expected 3 parts, got %d", len(parts))
return True
payload = parts[1]
# Add padding if needed (JWT uses base64url encoding without padding)
if padding := len(payload) % 4:
payload += "=" * (4 - padding)
payload = payload.replace("-", "+").replace("_", "/")
decoded_bytes = base64.b64decode(payload)
payload_data = json.loads(decoded_bytes)
exp = payload_data.get("exp")
if exp is None:
logger.debug("No 'exp' claim found in token")
return False
if not isinstance(exp, int): # pragma: no cover
logger.debug("Invalid 'exp' claim: expected int, got %s", type(exp))
return True
current_time = time.time()
is_expired = current_time >= exp
logger.debug(
"Token expiration check: current=%d, exp=%d, expired=%s",
current_time,
exp,
is_expired,
)
return is_expired
except (binascii.Error, json.JSONDecodeError) as e:
logger.debug("Error parsing JWT token: %s", e)
return True
def is_logged_in() -> bool:
token = get_auth_token()
if token is None:
logger.debug("Login status: False (no token)")
return False
if is_token_expired(token):
logger.debug("Login status: False (token expired)")
return False
logger.debug("Login status: True")
return True

View file

@ -0,0 +1,101 @@
import contextlib
import logging
from typing import Any, Dict, Generator, List, Optional, Tuple
import typer
from httpx import HTTPError, HTTPStatusError, ReadTimeout
from rich.segment import Segment
from rich_toolkit import RichToolkit, RichToolkitTheme
from rich_toolkit.progress import Progress
from rich_toolkit.styles import MinimalStyle, TaggedStyle
logger = logging.getLogger(__name__)
class FastAPIStyle(TaggedStyle):
def __init__(self, tag_width: int = 11):
super().__init__(tag_width=tag_width)
def _get_tag_segments(
self,
metadata: Dict[str, Any],
is_animated: bool = False,
done: bool = False,
) -> Tuple[List[Segment], int]:
if not is_animated:
return super()._get_tag_segments(metadata, is_animated, done)
emojis = [
"🥚",
"🐣",
"🐤",
"🐥",
"🐓",
"🐔",
]
tag = emojis[self.animation_counter % len(emojis)]
if done:
tag = emojis[-1]
left_padding = self.tag_width - 1
left_padding = max(0, left_padding)
return [Segment(tag)], left_padding
def get_rich_toolkit(minimal: bool = False) -> RichToolkit:
style = MinimalStyle() if minimal else FastAPIStyle(tag_width=11)
theme = RichToolkitTheme(
style=style,
theme={
"tag.title": "white on #009485",
"tag": "white on #007166",
"placeholder": "grey85",
"text": "white",
"selected": "#007166",
"result": "grey85",
"progress": "on #007166",
"error": "red",
},
)
return RichToolkit(theme=theme)
@contextlib.contextmanager
def handle_http_errors(
progress: Progress,
message: Optional[str] = None,
) -> Generator[None, None, None]:
try:
yield
except ReadTimeout as e:
logger.debug(e)
progress.set_error(
"The request to the FastAPI Cloud server timed out. Please try again later."
)
raise typer.Exit(1) from None
except HTTPError as e:
logger.debug(e)
# Handle validation errors from Pydantic models, this should make it easier to debug :)
if isinstance(e, HTTPStatusError) and e.response.status_code == 422:
logger.debug(e.response.json()) # pragma: no cover
if isinstance(e, HTTPStatusError) and e.response.status_code in (401, 403):
message = "The specified token is not valid. Use `fastapi login` to generate a new token."
else:
message = (
message
or f"Something went wrong while contacting the FastAPI Cloud server. Please try again later. \n\n{e}"
)
progress.set_error(message)
raise typer.Exit(1) from None

View file

@ -0,0 +1,21 @@
from pathlib import Path
import typer
def get_config_folder() -> Path:
return Path(typer.get_app_dir("fastapi-cli"))
def get_auth_path() -> Path:
auth_path = get_config_folder() / "auth.json"
auth_path.parent.mkdir(parents=True, exist_ok=True)
return auth_path
def get_cli_config_path() -> Path:
cli_config_path = get_config_folder() / "cli.json"
cli_config_path.parent.mkdir(parents=True, exist_ok=True)
return cli_config_path

View file

@ -0,0 +1,5 @@
def validate_environment_variable_name(name: str) -> bool:
if name.isidentifier():
return True
return False

View file

@ -0,0 +1,72 @@
from typing import Any, Dict, Generic, Type, TypeVar
from pydantic import BaseModel
from pydantic.version import VERSION as PYDANTIC_VERSION
PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(".")[:2])
PYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2
T = TypeVar("T")
Model = TypeVar("Model", bound=BaseModel)
def model_validate(model_class: Type[Model], data: Dict[Any, Any]) -> Model:
if PYDANTIC_V2:
return model_class.model_validate(data) # type: ignore[no-any-return, unused-ignore, attr-defined]
else:
return model_class.parse_obj(data) # type: ignore[no-any-return, unused-ignore, attr-defined]
def model_validate_json(model_class: Type[Model], data: str) -> Model:
if PYDANTIC_V2:
return model_class.model_validate_json(data) # type: ignore[no-any-return, unused-ignore, attr-defined]
else:
return model_class.parse_raw(data) # type: ignore[no-any-return, unused-ignore, attr-defined]
def model_dump(obj: BaseModel, **kwargs: Any) -> Dict[Any, Any]:
if PYDANTIC_V2:
return obj.model_dump(**kwargs) # type: ignore[no-any-return, unused-ignore, attr-defined]
else:
return obj.dict(**kwargs) # type: ignore[no-any-return, unused-ignore, attr-defined]
def model_dump_json(obj: BaseModel) -> str:
if PYDANTIC_V2:
return obj.model_dump_json() # type: ignore[no-any-return, unused-ignore, attr-defined]
else:
# Use compact separators to match Pydantic v2's output format
return obj.json(separators=(",", ":")) # type: ignore[no-any-return, unused-ignore, attr-defined]
class TypeAdapter(Generic[T]):
def __init__(self, type_: Type[T]) -> None:
self.type_ = type_
if PYDANTIC_V2:
from pydantic import ( # type: ignore[attr-defined, unused-ignore]
TypeAdapter as PydanticTypeAdapter,
)
self._adapter = PydanticTypeAdapter(type_)
else:
self._adapter = None # type: ignore[assignment, unused-ignore]
def validate_python(self, value: Any) -> T:
"""Validate a Python object against the type."""
if PYDANTIC_V2:
return self._adapter.validate_python(value) # type: ignore[no-any-return, union-attr, unused-ignore]
else:
from pydantic import parse_obj_as
return parse_obj_as(self.type_, value) # type: ignore[no-any-return, unused-ignore]
def validate_json(self, value: str) -> T:
"""Validate a JSON string against the type."""
if PYDANTIC_V2:
return self._adapter.validate_json(value) # type: ignore[no-any-return, union-attr, unused-ignore]
else:
from pydantic import parse_raw_as
return parse_raw_as(self.type_, value) # type: ignore[no-any-return, unused-ignore, operator]

View file

@ -0,0 +1,18 @@
import sentry_sdk
from sentry_sdk.integrations.typer import TyperIntegration
from .auth import is_logged_in
SENTRY_DSN = "https://230250605ea4b58a0b69c768e9ec1168@o4506985151856640.ingest.us.sentry.io/4508449198899200"
def init_sentry() -> None:
"""Initialize Sentry error tracking only if user is logged in."""
if not is_logged_in():
return
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[TyperIntegration()],
send_default_pii=False,
)