From 3eba3f8835f25800f37b9da18a09429b084effa0 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 28 Apr 2019 21:53:01 +0200 Subject: Add preferences endpoint --- docs/index.rst | 27 ++++++++++++++++++++++++++- mastodon/Mastodon.py | 15 +++++++++++++++ tests/cassettes/test_preferences.yaml | 30 ++++++++++++++++++++++++++++++ tests/test_account.py | 5 ++++- 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/cassettes/test_preferences.yaml diff --git a/docs/index.rst b/docs/index.rst index d6d0d04..a98eb23 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -335,7 +335,7 @@ Scheduled toot dicts .. code-block:: python - api2.status_post("text", scheduled_at=the_future) + mastodon.status_post("text", scheduled_at=the_future) # Returns the following dictionary: { 'id': # Scheduled toot ID (note: Not the id of the toot once it gets posted!) @@ -693,6 +693,26 @@ Push notification dicts 'title': # Title for the notification } +Preference dicts +~~~~~~~~~~~~~~~~ +.. _preference dict: + +.. code-block:: python + + mastodon.preferences() + # Returns the following dictionary + { + 'posting:default:visibility': # The default visibility setting for the users posts, + # as a string + 'posting:default:sensitive': # Boolean indicating whether the users uploads should + # be marked sensitive by default + 'posting:default:language': # The users default post language, if set (None if not) + 'reading:expand:media': # How the user wishes to be shown sensitive media. Can be + # 'default' (hide if sensitive), 'hide_all' or 'show_all' + 'reading:expand:spoilers': # Boolean indicating whether the user wishes to expand + # content warnings by default + } + App registration and user authentication ---------------------------------------- Before you can use the mastodon API, you have to register your @@ -878,6 +898,11 @@ Reading data: Endorsements .. automethod:: Mastodon.endorsements +Reading data: Preferences +-------------------------- + +.. automethod:: Mastodon.preferences + Writing data: Statuses ---------------------- diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 3fe3d21..6b3de9a 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -180,6 +180,7 @@ class Mastodon: __DICT_VERSION_FILTER = "2.4.3" __DICT_VERSION_CONVERSATION = bigger_version(bigger_version("2.6.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS) __DICT_VERSION_SCHEDULED_STATUS = bigger_version("2.7.0", __DICT_VERSION_STATUS) + __DICT_VERSION_PREFERENCES = "2.8.0" ### # Registering apps @@ -1349,6 +1350,20 @@ class Mastodon: """ return self.__api_request('GET', '/api/v1/push/subscription') + ### + # Reading data: Preferences + ### + @api_version("2.8.0", "2.8.0", __DICT_VERSION_PREFERENCES) + def preferences(self): + """ + Fetch the users preferences, which can be used to set some default options. + As of 2.8.0, apps can only fetch, not update preferences. + + Returns a `preference dict`_. + + """ + return self.__api_request('GET', '/api/v1/preferences') + ### # Writing data: Statuses ### diff --git a/tests/cassettes/test_preferences.yaml b/tests/cassettes/test_preferences.yaml new file mode 100644 index 0000000..e6e660f --- /dev/null +++ b/tests/cassettes/test_preferences.yaml @@ -0,0 +1,30 @@ +interactions: +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.18.4] + method: GET + uri: http://localhost:3000/api/v1/preferences + response: + body: {string: '{"posting:default:visibility":"public","posting:default:sensitive":false,"posting:default:language":null,"reading:expand:media":"default","reading:expand:spoilers":false}'} + headers: + Cache-Control: ['max-age=0, private, must-revalidate'] + Content-Type: [application/json; charset=utf-8] + ETag: [W/"16e1b4c608ece78202df9c19d6a56932"] + Referrer-Policy: [strict-origin-when-cross-origin] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Download-Options: [noopen] + X-Frame-Options: [SAMEORIGIN] + X-Permitted-Cross-Domain-Policies: [none] + X-Request-Id: [0b19cc10-64ae-4fd4-827e-0e8e1b46c673] + X-Runtime: ['0.032833'] + X-XSS-Protection: [1; mode=block] + content-length: ['170'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/test_account.py b/tests/test_account.py index cd7633d..9821c13 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -205,4 +205,7 @@ def test_account_pin_unpin(api, api2): assert not relationship['endorsed'] assert not user["id"] in map(lambda x: x["id"], endorsed2) - +@pytest.mark.vcr() +def test_preferences(api): + prefs = api.preferences() + assert prefs -- cgit v1.2.3