Skip to content

Commit

Permalink
feat: add cli command to run any api method
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinHjelmare committed May 20, 2022
1 parent 8dd9d22 commit f488c4a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/aiortm/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}

Expand All @@ -24,3 +24,4 @@ def cli(debug: bool) -> None:

cli.add_command(authorize)
cli.add_command(check_token)
cli.add_command(method)
33 changes: 30 additions & 3 deletions src/aiortm/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ def authorize(api_key: str, secret: str) -> None:


@click.command(options_metavar="<options>")
@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:
"""Check if the authentication token is valid."""
run_app(check_auth_token, api_key, secret, token=token)


@click.command(options_metavar="<options>")
@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,
Expand Down Expand Up @@ -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(
Expand All @@ -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}")
4 changes: 1 addition & 3 deletions src/aiortm/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f488c4a

Please sign in to comment.