Coverage for src/fastapi_cloud_cli/commands/whoami.py: 100%

29 statements  

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

1import logging 1abcdefg

2from typing import Any 1abcdefg

3 

4import typer 1abcdefg

5from pydantic import BaseModel 1abcdefg

6from rich_toolkit import RichToolkit 1abcdefg

7 

8from fastapi_cloud_cli._app import cloud_app 1abcdefg

9from fastapi_cloud_cli.api import APIClient 1abcdefg

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

11from fastapi_cloud_cli.utils.execution import JsonOutputOption 1abcdefg

12 

13logger = logging.getLogger(__name__) 1abcdefg

14 

15 

16class WhoAmIOutput(BaseModel): 1abcdefg

17 email: str | None = None 1abcdefg

18 has_deploy_token: bool 1abcdefg

19 

20 

21def _render_whoami_output(data: WhoAmIOutput, toolkit: RichToolkit) -> None: 1abjcdkefgl

22 toolkit.print(f"[bold]{data.email}[/bold]", emoji="⚡") 1mhjntokpqiurls

23 

24 if data.has_deploy_token: 1mhjntokpqiurls

25 toolkit.print( 1hnopirs

26 "[bold]Using API token from environment variable for " 

27 "[blue]`fastapi deploy`[/blue] command.[/bold]", 

28 emoji="⚡", 

29 ) 

30 

31 

32@cloud_app.command(cls=UserCommand) 1abcdefg

33def whoami( 2a b jbc d kbe f g lb

34 ctx: typer.Context, 

35 json_output: JsonOutputOption = False, 

36) -> Any: 

37 """ 

38 Show the currently logged in user. 

39 """ 

40 

41 command_context = get_user_command_context(ctx) 22 Q 3 R S v w T U V m h x ~ 4 ab5 6 B C 7 8 9 j n D bb! cb# $ E F % ' ( t o G db) eb* + H I , - . k p J / W : X Y y z Z 0 1 q i A fb; gb= ? K L @ [ ] u r M hb^ ib_ ` N O { | } l s P

42 toolkit = command_context.toolkit 22 Q 3 R S v w T U V m h x ~ 4 ab5 6 B C 7 8 9 j n D bb! cb# $ E F % ' ( t o G db) eb* + H I , - . k p J / W : X Y y z Z 0 1 q i A fb; gb= ? K L @ [ ] u r M hb^ ib_ ` N O { | } l s P

43 identity = command_context.identity 22 Q 3 R S v w T U V m h x ~ 4 ab5 6 B C 7 8 9 j n D bb! cb# $ E F % ' ( t o G db) eb* + H I , - . k p J / W : X Y y z Z 0 1 q i A fb; gb= ? K L @ [ ] u r M hb^ ib_ ` N O { | } l s P

44 

45 with ( 12Q3RSvwTUVmhx/W:XYyzZ01qiA

46 APIClient() as client, 

47 toolkit.progress( 

48 title="Fetching profile", 

49 transient=True, 

50 ) as progress, 

51 ): 

52 with client.handle_http_errors( 22 Q 3 R S v w T U V m h x ~ 4 ab5 6 B C 7 8 9 j n D bb! cb# $ E F % ' ( t o G db) eb* + H I , - . k p J / W : X Y y z Z 0 1 q i A fb; gb= ? K L @ [ ] u r M hb^ ib_ ` N O { | } l s P

53 progress, 

54 default_message="", 

55 toolkit=toolkit, 

56 ): 

57 response = client.get("/users/me") 22 Q 3 R S v w T U V m h x ~ 4 ab5 6 B C 7 8 9 j n D bb! cb# $ E F % ' ( t o G db) eb* + H I , - . k p J / W : X Y y z Z 0 1 q i A fb; gb= ? K L @ [ ] u r M hb^ ib_ ` N O { | } l s P

58 response.raise_for_status() 1QRSvwTUVmhx456BC789jnD!#$EF%'(toG)*+HI,-.kpJWXYyzZ01qiA;=?KL@[]urM^_`NO{|}lsP

59 

60 data = response.json() 1vwmhxBCjnDEFtoGHIkpJyzqiAKLurMNOlsP

61 

62 result = WhoAmIOutput( 1vwmhxBCjnDEFtoGHIkpJyzqiAKLurMNOlsP

63 has_deploy_token=identity.has_deploy_token(), email=data["email"] 

64 ) 

65 

66 toolkit.success(result, render_output=_render_whoami_output) 1vwmhxBCjnDEFtoGHIkpJyzqiAKLurMNOlsP