From f488c4ac25bf4046529eba3341f4fc7fd1d537e8 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Fri, 20 May 2022 17:57:36 +0200 Subject: [PATCH] feat: add cli command to run any api method --- src/aiortm/cli/__init__.py | 3 ++- src/aiortm/cli/app.py | 33 ++++++++++++++++++++++++++++++--- src/aiortm/client.py | 4 +--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/aiortm/cli/__init__.py b/src/aiortm/cli/__init__.py index b975803..c6104b7 100644 --- a/src/aiortm/cli/__init__.py +++ b/src/aiortm/cli/__init__.py @@ -4,7 +4,7 @@ import click from .. import __version__ -from .app import authorize, check_token +from .app import authorize, check_token, method SETTINGS = {"help_option_names": ["-h", "--help"]} @@ -24,3 +24,4 @@ def cli(debug: bool) -> None: cli.add_command(authorize) cli.add_command(check_token) +cli.add_command(method) diff --git a/src/aiortm/cli/app.py b/src/aiortm/cli/app.py index e09fbd2..981fbdf 100644 --- a/src/aiortm/cli/app.py +++ b/src/aiortm/cli/app.py @@ -24,7 +24,7 @@ def authorize(api_key: str, secret: str) -> None: @click.command(options_metavar="") -@click.option("-k", "--api_key", required=True, help="API key.") +@click.option("-k", "--api-key", required=True, help="API key.") @click.option("-s", "--secret", required=True, help="Shared secret.") @click.option("-t", "--token", required=True, help="Authentication token.") def check_token(api_key: str, secret: str, token: str) -> None: @@ -32,6 +32,16 @@ def check_token(api_key: str, secret: str, token: str) -> None: run_app(check_auth_token, api_key, secret, token=token) +@click.command(options_metavar="") +@click.option("-k", "--api-key", required=True, help="API key.") +@click.option("-s", "--secret", required=True, help="Shared secret.") +@click.option("-t", "--token", required=True, help="Authentication token.") +@click.argument("method") +def method(api_key: str, secret: str, token: str, method: str) -> None: + """Run an arbitrary API method.""" + run_app(run_method, api_key, secret, token=token, api_method=method) + + def run_app( command: Callable[..., Awaitable[None]], api_key: str, @@ -65,7 +75,7 @@ async def authorize_app(api_key: str, secret: str, **kwargs: Any) -> None: result = await auth.get_token(frob) token = result["token"] - click.echo(f"token: {token}") + click.echo(f"Token: {token}") async def check_auth_token( @@ -82,4 +92,21 @@ async def check_auth_token( result = await auth.check_token() - click.echo(f"token is valid: {result}") + click.echo(f"Token is valid: {result}") + + +async def run_method( + api_key: str, secret: str, token: str, api_method: str, **kwargs: Any +) -> None: + """Run an API method.""" + async with aiohttp.ClientSession() as session: + auth = Auth( + client_session=session, + api_key=api_key, + shared_secret=secret, + auth_token=token, + ) + + result = await auth.call_api_auth(api_method) + + click.echo(f"Method result: {result}") diff --git a/src/aiortm/client.py b/src/aiortm/client.py index 4e1f8a2..47d66d3 100644 --- a/src/aiortm/client.py +++ b/src/aiortm/client.py @@ -81,9 +81,7 @@ async def check_token(self) -> bool: if self.auth_token is None: return False try: - await self.call_api( - "rtm.auth.checkToken", api_key=self.api_key, auth_token=self.auth_token - ) + await self.call_api_auth("rtm.auth.checkToken") except APIAuthError: return False