diff --git a/toot/api.py b/toot/api.py index 9a34792..6633d5f 100644 --- a/toot/api.py +++ b/toot/api.py @@ -735,8 +735,13 @@ def get_lists(app, user): return http.get(app, user, "/api/v1/lists").json() -def get_poll(app, user, poll_id): - return http.get(app, user, f"/api/v1/polls/{poll_id}").json() +def get_poll(app, user, poll_id) -> Response: + return http.get(app, user, f"/api/v1/polls/{poll_id}") + + +def vote_poll(app, user, poll_id, choices) -> Response: + json = {"choices": choices} + return http.post(app, user, f"/api/v1/polls/{poll_id}/votes", json=json) def get_list_accounts(app, user, list_id): diff --git a/toot/cli/__init__.py b/toot/cli/__init__.py index 27bb2ec..9366f1a 100644 --- a/toot/cli/__init__.py +++ b/toot/cli/__init__.py @@ -169,11 +169,12 @@ json_option = click.option( @click.group(context_settings=CONTEXT) @click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot") @click.option("--debug/--no-debug", default=False, help="Log debug info to stderr") +@click.option("--verbose", is_flag=True, help="Log verbose info") @click.option("--color/--no-color", default=sys.stdout.isatty(), help="Use ANSI color in output") @click.option("--as", "as_user", type=AccountParamType(), help="The account to use, overrides the active account.") @click.version_option(__version__, message="%(prog)s v%(version)s") @click.pass_context -def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, as_user: str): +def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, verbose: bool, as_user: str): """Toot is a Mastodon CLI""" ctx.obj = TootObj(color, debug, as_user) ctx.color = color diff --git a/toot/cli/polls.py b/toot/cli/polls.py index bb29b95..c1ef4ec 100644 --- a/toot/cli/polls.py +++ b/toot/cli/polls.py @@ -1,10 +1,11 @@ import json as pyjson +from typing import Tuple import click from toot import api from toot.cli import Context, cli, json_option, pass_context -from toot.entities import Poll, from_dict +from toot.entities import Poll, from_response from toot.output import print_poll @@ -13,16 +14,41 @@ def polls(): """Show and vote on polls""" pass + @polls.command() @click.argument("poll_id") @json_option @pass_context def show(ctx: Context, poll_id: str, json: bool): """Show a poll by ID""" - data = api.get_poll(ctx.app, ctx.user, poll_id) + response = api.get_poll(ctx.app, ctx.user, poll_id) if json: - click.echo(pyjson.dumps(data)) + click.echo(response.text) else: - poll = from_dict(Poll, data) + poll = from_response(Poll, response) + print_poll(poll) + + +@polls.command() +@click.argument("poll_id") +@click.argument("choices", type=int, nargs=-1, required=True) +@json_option +@pass_context +def vote(ctx: Context, poll_id: str, choices: Tuple[int], json: bool): + """Vote on a poll + + CHOICES is one or more zero-indexed integers corresponding to the desired poll choices. + + e.g. to vote for the first and third options use: + + toot polls vote 0 2 + """ + response = api.vote_poll(ctx.app, ctx.user, poll_id, choices) + + if json: + click.echo(response.text) + else: + poll = from_response(Poll, response) + click.echo("You voted!\n") print_poll(poll)