--- /dev/null
+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
Users
=====
+Blocks
+------
+
+.. automethod:: Client.unblock
+
+.. automethod:: Client.block
+
Follows
-------
# @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
), 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):