From 7793d4499acdc2944601ee464f1209cffebe0ed1 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Tue, 21 Nov 2023 12:19:07 +0100 Subject: [PATCH] Add --json option to post command --- tests/integration/test_post.py | 15 ++++++++++++++- tests/integration/test_read.py | 8 ++++---- tests/integration/test_status.py | 10 +++++----- toot/api.py | 4 ++-- toot/commands.py | 14 +++++++++----- toot/console.py | 1 + toot/tui/app.py | 9 +++++++-- 7 files changed, 42 insertions(+), 19 deletions(-) diff --git a/tests/integration/test_post.py b/tests/integration/test_post.py index 7ad7eb9..7410c45 100644 --- a/tests/integration/test_post.py +++ b/tests/integration/test_post.py @@ -1,3 +1,4 @@ +import json import re import uuid @@ -27,6 +28,18 @@ def test_post(app, user, run): assert status["application"]["website"] == CLIENT_WEBSITE +def test_post_json(run): + content = "i wish i was a #lumberjack" + out = run("post", content, "--json") + status = json.loads(out) + + assert get_text(status["content"]) == content + assert status["visibility"] == "public" + assert status["sensitive"] is False + assert status["spoiler_text"] == "" + assert status["poll"] is None + + def test_post_visibility(app, user, run): for visibility in ["public", "unlisted", "private", "direct"]: out = run("post", "foo", "--visibility", visibility) @@ -269,7 +282,7 @@ def test_media_attachment_without_text(mock_read, mock_ml, app, user, run): def test_reply_thread(app, user, friend, run): - status = api.post_status(app, friend, "This is the status") + status = api.post_status(app, friend, "This is the status").json() out = run("post", "--reply-to", status["id"], "This is the reply") status_id = posted_status_id(out) diff --git a/tests/integration/test_read.py b/tests/integration/test_read.py index 881a958..a6855e0 100644 --- a/tests/integration/test_read.py +++ b/tests/integration/test_read.py @@ -95,7 +95,7 @@ def test_tags(run, base_url): def test_status(app, user, run): uuid = str(uuid4()) - response = api.post_status(app, user, uuid) + response = api.post_status(app, user, uuid).json() out = run("status", response["id"]) assert uuid in out @@ -105,9 +105,9 @@ def test_status(app, user, run): def test_thread(app, user, run): uuid = str(uuid4()) - s1 = api.post_status(app, user, uuid + "1") - s2 = api.post_status(app, user, uuid + "2", in_reply_to_id=s1["id"]) - s3 = api.post_status(app, user, uuid + "3", in_reply_to_id=s2["id"]) + s1 = api.post_status(app, user, uuid + "1").json() + s2 = api.post_status(app, user, uuid + "2", in_reply_to_id=s1["id"]).json() + s3 = api.post_status(app, user, uuid + "3", in_reply_to_id=s2["id"]).json() for status in [s1, s2, s3]: out = run("thread", status["id"]) diff --git a/tests/integration/test_status.py b/tests/integration/test_status.py index b23f44e..3fd91a5 100644 --- a/tests/integration/test_status.py +++ b/tests/integration/test_status.py @@ -6,7 +6,7 @@ from toot.exceptions import NotFoundError def test_delete_status(app, user, run): - status = api.post_status(app, user, "foo") + status = api.post_status(app, user, "foo").json() out = run("delete", status["id"]) assert out == "✓ Status deleted" @@ -16,7 +16,7 @@ def test_delete_status(app, user, run): def test_favourite(app, user, run): - status = api.post_status(app, user, "foo") + status = api.post_status(app, user, "foo").json() assert not status["favourited"] out = run("favourite", status["id"]) @@ -36,7 +36,7 @@ def test_favourite(app, user, run): def test_reblog(app, user, run): - status = api.post_status(app, user, "foo") + status = api.post_status(app, user, "foo").json() assert not status["reblogged"] out = run("reblog", status["id"]) @@ -56,7 +56,7 @@ def test_reblog(app, user, run): def test_pin(app, user, run): - status = api.post_status(app, user, "foo") + status = api.post_status(app, user, "foo").json() assert not status["pinned"] out = run("pin", status["id"]) @@ -73,7 +73,7 @@ def test_pin(app, user, run): def test_bookmark(app, user, run): - status = api.post_status(app, user, "foo") + status = api.post_status(app, user, "foo").json() assert not status["bookmarked"] out = run("bookmark", status["id"]) diff --git a/toot/api.py b/toot/api.py index d9a7c18..e9f12b9 100644 --- a/toot/api.py +++ b/toot/api.py @@ -201,7 +201,7 @@ def post_status( poll_expires_in=None, poll_multiple=None, poll_hide_totals=None, -): +) -> Response: """ Publish a new status. https://docs.joinmastodon.org/methods/statuses/#create @@ -233,7 +233,7 @@ def post_status( "hide_totals": poll_hide_totals, } - return http.post(app, user, '/api/v1/statuses', json=data, headers=headers).json() + return http.post(app, user, '/api/v1/statuses', json=data, headers=headers) def fetch_status(app, user, id): diff --git a/toot/commands.py b/toot/commands.py index 932aa7f..ce689be 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -121,12 +121,16 @@ def post(app, user, args): poll_hide_totals=args.poll_hide_totals, ) - if "scheduled_at" in response: - scheduled_at = parse_datetime(response["scheduled_at"]) - scheduled_at = datetime.strftime(scheduled_at, "%Y-%m-%d %H:%M:%S%z") - print_out(f"Toot scheduled for: {scheduled_at}") + if args.json: + print(response.text) else: - print_out(f"Toot posted: {response['url']}") + status = response.json() + if "scheduled_at" in status: + scheduled_at = parse_datetime(status["scheduled_at"]) + scheduled_at = datetime.strftime(scheduled_at, "%Y-%m-%d %H:%M:%S%z") + print_out(f"Toot scheduled for: {scheduled_at}") + else: + print_out(f"Toot posted: {status['url']}") delete_tmp_status_file() diff --git a/toot/console.py b/toot/console.py index 5f02043..1916941 100644 --- a/toot/console.py +++ b/toot/console.py @@ -583,6 +583,7 @@ POST_COMMANDS = [ "default": False, "help": "Hide vote counts until the poll ends. Defaults to false." }), + json_arg, ], require_auth=True, ), diff --git a/toot/tui/app.py b/toot/tui/app.py index 1e7132a..8a2ce09 100644 --- a/toot/tui/app.py +++ b/toot/tui/app.py @@ -529,10 +529,15 @@ class TUI(urwid.Frame): )) def post_status(self, content, warning, visibility, in_reply_to_id): - data = api.post_status(self.app, self.user, content, + data = api.post_status( + self.app, + self.user, + content, spoiler_text=warning, visibility=visibility, - in_reply_to_id=in_reply_to_id) + in_reply_to_id=in_reply_to_id + ).json() + status = self.make_status(data) # TODO: fetch new items from the timeline?