Coverage for src/fastapi_cloud_cli/commands/tokens/delete.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-11-09 00:25 +0000

1from typing import Annotated, Any 1abcdefg

2 

3import typer 1abcdefg

4from pydantic import BaseModel 1abcdefg

5from rich_toolkit import RichToolkit 1abcdefg

6 

7from fastapi_cloud_cli.api import APIClient 1abcdefg

8from fastapi_cloud_cli.commands._auth import UserCommand, get_user_command_context 1abcdefg

9from fastapi_cloud_cli.commands.tokens._app import tokens_app 1abcdefg

10from fastapi_cloud_cli.utils.apps import resolve_app_id_or_fail 1abcdefg

11from fastapi_cloud_cli.utils.execution import JsonOutputOption 1abcdefg

12 

13 

14class DeployTokenDeleteOutput(BaseModel): 1abcdefg

15 token_id: str 1abcdefg

16 deleted: bool = True 1abcdefg

17 

18 

19def _delete_deploy_token(client: APIClient, *, app_id: str, token_id: str) -> bool: 1abcdefg

20 response = client.delete(f"/apps/{app_id}/tokens/{token_id}") 1kopqhrstluvwixyzmABCnDEFjGHI

21 

22 if response.status_code == 404: 1kopqhrstluvwixyzmABCnDEFjGHI

23 return False 1oruxADG

24 

25 response.raise_for_status() 1kpqhstlvwiyzmBCnEFjHI

26 

27 return True 1kpqhstlvwiyzmBCnEFjHI

28 

29 

30def _render_deploy_token_delete_output( 1abhcdiefgj

31 data: DeployTokenDeleteOutput, toolkit: RichToolkit 

32) -> None: 

33 toolkit.print( 1khlimnj

34 f"Deleted deploy token [bold]{data.token_id}[/bold]", 

35 bullet=False, 

36 ) 

37 

38 

39@tokens_app.command("delete", cls=UserCommand) 1abcdefg

40def delete_token( 1abQcdRefgS

41 ctx: typer.Context, 

42 token_id: Annotated[ 

43 str, 

44 typer.Argument( 

45 help="ID of the deploy token to delete.", 

46 ), 

47 ], 

48 app_id: Annotated[ 

49 str | None, 

50 typer.Option( 

51 "--app-id", 

52 help="ID of the app that owns the deploy token.", 

53 ), 

54 ] = None, 

55 json_output: JsonOutputOption = False, 

56) -> Any: 

57 """ 

58 Delete a deploy token for an app. 

59 """ 

60 

61 toolkit = get_user_command_context(ctx).toolkit 1kopqJhrstKluvwLixyzMmABCNnDEFOjGHIP

62 

63 target_app_id = resolve_app_id_or_fail(toolkit, app_id=app_id) 1kopqJhrstKluvwLixyzMmABCNnDEFOjGHIP

64 

65 with APIClient() as client: 1kopqhrstluvwixyzmABCnDEFjGHI

66 with toolkit.progress( 1kopqhrstluvwixyzmABCnDEFjGHI

67 title="Deleting deploy token", 

68 transient=True, 

69 ) as progress: 

70 with client.handle_http_errors( 1kopqhrstluvwixyzmABCnDEFjGHI

71 progress, 

72 default_message="Error deleting deploy token. Please try again later.", 

73 not_found_message="Deploy token not found.", 

74 toolkit=toolkit, 

75 ): 

76 deleted = _delete_deploy_token( 1kopqhrstluvwixyzmABCnDEFjGHI

77 client, 

78 app_id=target_app_id, 

79 token_id=token_id, 

80 ) 

81 

82 if not deleted: 1kopqhrstluvwixyzmABCnDEFjGHI

83 message = ( 1oruxADG

84 f"Deploy token {token_id} not found." 

85 if toolkit.mode == "json" 

86 else "Deploy token not found." 

87 ) 

88 toolkit.fail( 1oruxADG

89 "not_found", 

90 message, 

91 hint="Run `fastapi cloud tokens list` to see available deploy tokens.", 

92 ) 

93 

94 toolkit.success( 1kpqhstlvwiyzmBCnEFjHI

95 DeployTokenDeleteOutput(token_id=token_id), 

96 render_output=_render_deploy_token_delete_output, 

97 )