From 18e0b1a37c36e270ccc9c50cb1343f03637f70e0 Mon Sep 17 00:00:00 2001 From: Harmon Date: Tue, 27 Apr 2021 09:39:21 -0500 Subject: [PATCH] Add Client.block and Client.unblock --- cassettes/test_block_and_unblock.yaml | 127 ++++++++++++++++++++++++++ docs/client.rst | 7 ++ tests/test_client.py | 6 ++ tweepy/client.py | 57 ++++++++++++ 4 files changed, 197 insertions(+) create mode 100644 cassettes/test_block_and_unblock.yaml diff --git a/cassettes/test_block_and_unblock.yaml b/cassettes/test_block_and_unblock.yaml new file mode 100644 index 0000000..cc27ac6 --- /dev/null +++ b/cassettes/test_block_and_unblock.yaml @@ -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 diff --git a/docs/client.rst b/docs/client.rst index 1d8df5d..3857503 100644 --- a/docs/client.rst +++ b/docs/client.rst @@ -49,6 +49,13 @@ Tweet lookup Users ===== +Blocks +------ + +.. automethod:: Client.unblock + +.. automethod:: Client.block + Follows ------- diff --git a/tests/test_client.py b/tests/test_client.py index e4c26e9..fbd9e2e 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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 diff --git a/tweepy/client.py b/tweepy/client.py index a1c62a1..071a518 100644 --- a/tweepy/client.py +++ b/tweepy/client.py @@ -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): -- 2.25.1