Add Client.block and Client.unblock
authorHarmon <Harmon758@gmail.com>
Tue, 27 Apr 2021 14:39:21 +0000 (09:39 -0500)
committerHarmon <Harmon758@gmail.com>
Tue, 27 Apr 2021 14:39:21 +0000 (09:39 -0500)
cassettes/test_block_and_unblock.yaml [new file with mode: 0644]
docs/client.rst
tests/test_client.py
tweepy/client.py

diff --git a/cassettes/test_block_and_unblock.yaml b/cassettes/test_block_and_unblock.yaml
new file mode 100644 (file)
index 0000000..cc27ac6
--- /dev/null
@@ -0,0 +1,127 @@
+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.4 Requests/2.25.1 Tweepy/3.10.0
+    method: POST
+    uri: https://api.twitter.com/2/users/1072250532645998596/blocking
+  response:
+    body:
+      string: !!binary |
+        H4sIAAAAAAAAAKpWSkksSVSyqlZKyslPzs7MS1eyKikqTa2tBQAAAP//AwBcCHSuGgAAAA==
+    headers:
+      cache-control:
+      - no-cache, no-store, max-age=0
+      content-disposition:
+      - attachment; filename=json.json
+      content-encoding:
+      - gzip
+      content-length:
+      - '52'
+      content-type:
+      - application/json; charset=utf-8
+      date:
+      - Tue, 27 Apr 2021 14:37:54 GMT
+      server:
+      - tsa_b
+      set-cookie:
+      - personalization_id="v1_wzSqJU5xmaPBwmsTkIQvCQ=="; Max-Age=63072000; Expires=Thu,
+        27 Apr 2023 14:37:54 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None
+      - guest_id=v1%3A161953427486161259; Max-Age=63072000; Expires=Thu, 27 Apr 2023
+        14:37:54 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None
+      strict-transport-security:
+      - max-age=631138519
+      x-access-level:
+      - read-write-directmessages
+      x-connection-hash:
+      - 5c25c08c94e3e4166ae103e975e2c840
+      x-content-type-options:
+      - nosniff
+      x-frame-options:
+      - SAMEORIGIN
+      x-rate-limit-limit:
+      - '50'
+      x-rate-limit-remaining:
+      - '48'
+      x-rate-limit-reset:
+      - '1619534909'
+      x-response-time:
+      - '50'
+      x-tsa-request-body-time:
+      - '4'
+      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%3A161953427486161259; personalization_id="v1_wzSqJU5xmaPBwmsTkIQvCQ=="
+      User-Agent:
+      - Python/3.9.4 Requests/2.25.1 Tweepy/3.10.0
+    method: DELETE
+    uri: https://api.twitter.com/2/users/1072250532645998596/blocking/17874544
+  response:
+    body:
+      string: !!binary |
+        H4sIAAAAAAAAAKpWSkksSVSyqlZKyslPzs7MS1eySkvMKU6trQUAAAD//wMAcXlx4RsAAAA=
+    headers:
+      cache-control:
+      - no-cache, no-store, max-age=0
+      content-disposition:
+      - attachment; filename=json.json
+      content-encoding:
+      - gzip
+      content-length:
+      - '53'
+      content-type:
+      - application/json; charset=utf-8
+      date:
+      - Tue, 27 Apr 2021 14:37:55 GMT
+      server:
+      - tsa_b
+      strict-transport-security:
+      - max-age=631138519
+      x-access-level:
+      - read-write-directmessages
+      x-connection-hash:
+      - 5c25c08c94e3e4166ae103e975e2c840
+      x-content-type-options:
+      - nosniff
+      x-frame-options:
+      - SAMEORIGIN
+      x-rate-limit-limit:
+      - '50'
+      x-rate-limit-remaining:
+      - '48'
+      x-rate-limit-reset:
+      - '1619534920'
+      x-response-time:
+      - '401'
+      x-xss-protection:
+      - '0'
+    status:
+      code: 200
+      message: OK
+version: 1
index 1d8df5d98c6b43dac60cc24b7bab820cacb50c21..385750388f6e2ca255b7d63bb8c2d99194aa40f0 100644 (file)
@@ -49,6 +49,13 @@ Tweet lookup
 Users
 =====
 
+Blocks
+------
+
+.. automethod:: Client.unblock
+
+.. automethod:: Client.block
+
 Follows
 -------
 
index e4c26e90583efd9dbb7daa0df9796ea3fb52c9a2..fbd9e2e8f4db9d03508c0b6b869e37444b65667a 100644 (file)
@@ -53,6 +53,12 @@ class TweepyTestCase(unittest.TestCase):
         # @TwitterDev and @TwitterAPI Tweets announcing API v2
         self.client.get_tweets(tweet_ids)
 
+    @tape.use_cassette("test_block_and_unblock.yaml", serializer="yaml")
+    def test_block_and_unblock(self):
+        user_id = 17874544  # User ID for @TwitterSupport
+        self.assertTrue(self.client.block(user_id))
+        self.assertFalse(self.client.unblock(user_id))
+
     @tape.use_cassette("test_follow_and_unfollow.yaml", serializer="yaml")
     def test_follow_and_unfollow(self):
         user_id = 783214  # User ID for @Twitter
index a1c62a1157bc21fae51e6312bdad3cd016e0f345..071a51874555b2da5e15e2ac1339a301ccbfff2b 100644 (file)
@@ -747,6 +747,63 @@ class Client:
             ), data_type=Tweet, user_auth=user_auth
         )
 
+    # Blocks
+
+    def unblock(self, target_user_id):
+        """Unblock another user.
+
+        The request succeeds with no action when the user sends a request to a
+        user they're not blocking or have already unblocked.
+
+        Parameters
+        ----------
+        target_user_id : Union[int, str]
+            The user ID of the user that you would like to unblock.
+
+        Returns
+        -------
+        bool
+            Indicates whether the user is blocking the specified user as a
+            result of this request. The returned value is ``False`` for a
+            successful unblock request.
+
+        References
+        ----------
+        https://developer.twitter.com/en/docs/twitter-api/users/blocks/api-reference/delete-users-user_id-blocking
+        """
+        source_user_id = self.access_token.partition('-')[0]
+        route = f"/2/users/{source_user_id}/blocking/{target_user_id}"
+
+        return self._make_request(
+            "DELETE", route, user_auth=True
+        )[0]["blocking"]
+
+    def block(self, target_user_id):
+        """Block another user.
+
+        Parameters
+        ----------
+        target_user_id : Union[int, str]
+            The user ID of the user that you would like to block.
+
+        Returns
+        -------
+        bool
+            Indicates whether the user is blocking the specified user as a
+            result of this request.
+
+        References
+        ----------
+        https://developer.twitter.com/en/docs/twitter-api/users/blocks/api-reference/post-users-user_id-blocking
+        """
+        id = self.access_token.partition('-')[0]
+        route = f"/2/users/{id}/blocking"
+
+        return self._make_request(
+            "POST", route, json={"target_user_id": str(target_user_id)},
+            user_auth=True
+        )[0]["blocking"]
+
     # Follows
 
     def unfollow(self, target_user_id):