From 1c9aad68b6c23f33435aa907c4821d0ad9367946 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Tue, 30 Apr 2024 12:25:01 +0200 Subject: [PATCH] wip --- toot/ahttp.py | 8 ++++++++ toot/cli/__init__.py | 5 +++++ toot/cli/read.py | 21 +++++++++++++-------- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/toot/ahttp.py b/toot/ahttp.py index 1c416b3..5cc11b3 100644 --- a/toot/ahttp.py +++ b/toot/ahttp.py @@ -74,3 +74,11 @@ def logger_trace_config() -> TraceConfig: async def verify_credentials(session: ClientSession) -> ClientResponse: return await session.get("/api/v1/accounts/verify_credentials") + + +async def fetch_status(session: ClientSession, status_id: str) -> ClientResponse: + """ + Fetch a single status + https://docs.joinmastodon.org/methods/statuses/#get + """ + return await session.get(f"/api/v1/statuses/{status_id}") diff --git a/toot/cli/__init__.py b/toot/cli/__init__.py index f2826cc..528a1db 100644 --- a/toot/cli/__init__.py +++ b/toot/cli/__init__.py @@ -138,6 +138,11 @@ def pass_session(f: "t.Callable[te.Concatenate[aiohttp.ClientSession, P], t.Awai session = await make_session(context) try: return await f(session, *args, **kwargs) + except aiohttp.ClientResponseError as ex: + print(type(ex)) + from pprint import pp + pp(ex.__dict__) + raise click.ClickException("foo") finally: await session.close() diff --git a/toot/cli/read.py b/toot/cli/read.py index 9d2f00e..ad632a4 100644 --- a/toot/cli/read.py +++ b/toot/cli/read.py @@ -20,6 +20,7 @@ from toot.cli import InstanceParamType, async_command, cli, get_context, json_op async def whoami(session: ClientSession, json: bool): """Display logged in user details""" response = await ahttp.verify_credentials(session) + response.raise_for_status() if json: click.echo(await response.text()) @@ -31,10 +32,11 @@ async def whoami(session: ClientSession, json: bool): @cli.command() @click.argument("account") @json_option -@pass_context -def whois(ctx: Context, account: str, json: bool): +@async_command +@pass_session +async def whois(session: ClientSession, account: str, json: bool): """Display account details""" - account_dict = api.find_account(ctx.app, ctx.user, account) + account_dict = ahttp.find_account(ctx.app, ctx.user, account) # Here it's not possible to avoid parsing json since it's needed to find the account. if json: @@ -91,14 +93,17 @@ def search(ctx: Context, query: str, resolve: bool, json: bool): @cli.command() @click.argument("status_id") @json_option -@pass_context -def status(ctx: Context, status_id: str, json: bool): +@async_command +@pass_session +async def status(session: ClientSession, status_id: str, json: bool): """Show a single status""" - response = api.fetch_status(ctx.app, ctx.user, status_id) + response = await ahttp.fetch_status(session, status_id) + response.raise_for_status() + if json: - click.echo(response.text) + click.echo(await response.text()) else: - status = from_dict(Status, response.json()) + status = from_dict(Status, await response.json()) print_status(status)