diff --git a/tests/integration/test_accounts.py b/tests/integration/test_accounts.py index bf01f1b..fe18def 100644 --- a/tests/integration/test_accounts.py +++ b/tests/integration/test_accounts.py @@ -1,3 +1,6 @@ +import json + +from toot.entities import Account, from_dict def test_whoami(user, run): @@ -6,6 +9,12 @@ def test_whoami(user, run): assert f"@{user.username}" in out +def test_whoami_json(user, run): + out = run("whoami", "--json") + account = from_dict(Account, json.loads(out)) + assert account.username == user.username + + def test_whois(app, friend, run): variants = [ friend.username, diff --git a/toot/commands.py b/toot/commands.py index de77c01..78c4fab 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -516,8 +516,12 @@ def blocked(app, user, args): def whoami(app, user, args): - account = api.verify_credentials(app, user).json() - print_account(account) + response = api.verify_credentials(app, user) + if args.json: + print(response.text) + else: + account = response.json() + print_account(account) def whois(app, user, args): diff --git a/toot/console.py b/toot/console.py index b6613d0..9733d09 100644 --- a/toot/console.py +++ b/toot/console.py @@ -232,6 +232,12 @@ tag_arg = (["tag_name"], { "help": "tag name, e.g. Caturday, or \"#Caturday\"", }) +json_arg = (["--json"], { + "action": "store_true", + "default": False, + "help": "print json instead of plaintext", +}) + # Arguments for selecting a timeline (see `toot.commands.get_timeline_generator`) common_timeline_args = [ (["-p", "--public"], { @@ -388,7 +394,7 @@ READ_COMMANDS = [ Command( name="whoami", description="Display logged in user details", - arguments=[], + arguments=[json_arg], require_auth=True, ), Command(