Skip to content

Commit

Permalink
Add Client.block and Client.unblock
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed Apr 27, 2021
1 parent 5beaa24 commit 18e0b1a
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
127 changes: 127 additions & 0 deletions cassettes/test_block_and_unblock.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ Tweet lookup
Users
=====

Blocks
------

.. automethod:: Client.unblock

.. automethod:: Client.block

Follows
-------

Expand Down
6 changes: 6 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def test_get_tweets(self):
# @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
Expand Down
57 changes: 57 additions & 0 deletions tweepy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,63 @@ def get_tweets(self, ids, *, user_auth=False, **params):
), 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):
Expand Down

0 comments on commit 18e0b1a

Please sign in to comment.