Add Client.unmute and Client.mute
authorHarmon <Harmon758@gmail.com>
Mon, 20 Sep 2021 21:49:36 +0000 (16:49 -0500)
committerHarmon <Harmon758@gmail.com>
Mon, 20 Sep 2021 21:49:36 +0000 (16:49 -0500)
cassettes/test_mute_and_unmute.yaml [new file with mode: 0644]
docs/client.rst
tests/test_client.py
tweepy/client.py

diff --git a/cassettes/test_mute_and_unmute.yaml b/cassettes/test_mute_and_unmute.yaml
new file mode 100644 (file)
index 0000000..b06597e
--- /dev/null
@@ -0,0 +1,125 @@
+interactions:
+- request:
+    body: '{"target_user_id": "17874544"}'
+    headers:
+      Accept:
+      - '*/*'
+      Accept-Encoding:
+      - gzip, deflate
+      Connection:
+      - keep-alive
+      Content-Length:
+      - '30'
+      Content-Type:
+      - application/json
+      User-Agent:
+      - Python/3.9.6 Requests/2.25.1 Tweepy/4.0.0-alpha
+    method: POST
+    uri: https://api.twitter.com/2/users/1072250532645998596/muting
+  response:
+    body:
+      string: !!binary |
+        H4sIAAAAAAAAAKpWSkksSVSyqlbKLS3JzEtXsiopKk2trQUAAAD//wMAKOKMLRgAAAA=
+    headers:
+      api-version:
+      - '2.24'
+      cache-control:
+      - no-cache, no-store, max-age=0
+      content-disposition:
+      - attachment; filename=json.json
+      content-encoding:
+      - gzip
+      content-length:
+      - '50'
+      content-type:
+      - application/json; charset=utf-8
+      date:
+      - Mon, 20 Sep 2021 21:47:48 UTC
+      server:
+      - tsa_a
+      set-cookie:
+      - personalization_id="v1_tx5lZdBKqUcj82GIzmOxSg=="; Max-Age=63072000; Expires=Wed,
+        20 Sep 2023 21:47:48 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None
+      - guest_id=v1%3A163217446858292923; Max-Age=63072000; Expires=Wed, 20 Sep 2023
+        21:47:48 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None
+      strict-transport-security:
+      - max-age=631138519
+      x-access-level:
+      - read-write-directmessages
+      x-connection-hash:
+      - b749ed00177b9e8d4fa0590d2ddcb7210910a14c470247d2796add7567c67c6f
+      x-content-type-options:
+      - nosniff
+      x-frame-options:
+      - SAMEORIGIN
+      x-rate-limit-limit:
+      - '50'
+      x-rate-limit-remaining:
+      - '49'
+      x-rate-limit-reset:
+      - '1632175368'
+      x-xss-protection:
+      - '0'
+    status:
+      code: 200
+      message: OK
+- request:
+    body: null
+    headers:
+      Accept:
+      - '*/*'
+      Accept-Encoding:
+      - gzip, deflate
+      Connection:
+      - keep-alive
+      Content-Length:
+      - '0'
+      Cookie:
+      - guest_id=v1%3A163217446858292923; personalization_id="v1_tx5lZdBKqUcj82GIzmOxSg=="
+      User-Agent:
+      - Python/3.9.6 Requests/2.25.1 Tweepy/4.0.0-alpha
+    method: DELETE
+    uri: https://api.twitter.com/2/users/1072250532645998596/muting/17874544
+  response:
+    body:
+      string: !!binary |
+        H4sIAAAAAAAAAKpWSkksSVSyqlbKLS3JzEtXskpLzClOra0FAAAA//8DAL40mrYZAAAA
+    headers:
+      api-version:
+      - '2.24'
+      cache-control:
+      - no-cache, no-store, max-age=0
+      content-disposition:
+      - attachment; filename=json.json
+      content-encoding:
+      - gzip
+      content-length:
+      - '51'
+      content-type:
+      - application/json; charset=utf-8
+      date:
+      - Mon, 20 Sep 2021 21:47:48 UTC
+      server:
+      - tsa_a
+      strict-transport-security:
+      - max-age=631138519
+      x-access-level:
+      - read-write-directmessages
+      x-connection-hash:
+      - b749ed00177b9e8d4fa0590d2ddcb7210910a14c470247d2796add7567c67c6f
+      x-content-type-options:
+      - nosniff
+      x-frame-options:
+      - SAMEORIGIN
+      x-rate-limit-limit:
+      - '50'
+      x-rate-limit-remaining:
+      - '49'
+      x-rate-limit-reset:
+      - '1632175368'
+      x-xss-protection:
+      - '0'
+    status:
+      code: 200
+      message: OK
+version: 1
index 5b7fa50fc4e456d37e1ba467e5a5a6afa0277961..acfb2e0bdd35621f9b34d94d74d61e5e158fb3c1 100644 (file)
@@ -80,6 +80,13 @@ Follows
 
 .. automethod:: Client.follow
 
+Mutes
+-----
+
+.. automethod:: Client.unmute
+
+.. automethod:: Client.mute
+
 User lookup
 -----------
 
index 619195bb83b0d4d0e6c876d61b538af7de19a369..b052dc71fba403a84efa1b86f0bad0ccc304b489 100644 (file)
@@ -93,6 +93,12 @@ class TweepyTestCase(unittest.TestCase):
         user_id = 783214  # User ID for @Twitter
         self.client.get_users_following(user_id)
 
+    @tape.use_cassette("test_mute_and_unmute.yaml", serializer="yaml")
+    def test_mute_and_unmute(self):
+        user_id = 17874544  # User ID for @TwitterSupport
+        self.client.mute(user_id)
+        self.client.unmute(user_id)
+
     @tape.use_cassette("test_get_user.yaml", serializer="yaml")
     def test_get_user(self):
         self.client.get_user(username="Twitter")
index 31a8124aef1d6b4f5d915f656b1c27ddea7e1db5..21086e22cb9f8cb1d762aacd495fbbe5292a9aeb 100644 (file)
@@ -1244,6 +1244,56 @@ class Client:
             user_auth=True
         )
 
+    # Mutes
+
+    def unmute(self, target_user_id):
+        """Allows an authenticated user ID to unmute the target user.
+
+        The request succeeds with no action when the user sends a request to a
+        user they're not muting or have already unmuted.
+
+        Parameters
+        ----------
+        target_user_id : Union[int, str]
+            The user ID of the user that you would like to unmute.
+
+        Returns
+        -------
+        Union[dict, requests.Response, Response]
+
+        References
+        ----------
+        https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/delete-users-user_id-muting
+        """
+        source_user_id = self.access_token.partition('-')[0]
+        route = f"/2/users/{source_user_id}/muting/{target_user_id}"
+
+        return self._make_request("DELETE", route, user_auth=True)
+
+    def mute(self, target_user_id):
+        """Allows an authenticated user ID to mute the target user.
+
+        Parameters
+        ----------
+        target_user_id : Union[int, str]
+            The user ID of the user that you would like to mute.
+
+        Returns
+        -------
+        Union[dict, requests.Response, Response]
+
+        References
+        ----------
+        https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/post-users-user_id-muting
+        """
+        id = self.access_token.partition('-')[0]
+        route = f"/2/users/{id}/muting"
+
+        return self._make_request(
+            "POST", route, json={"target_user_id": str(target_user_id)},
+            user_auth=True
+        )
+
     # User lookup
 
     def get_user(self, *, id=None, username=None, user_auth=False, **params):