@tape.use_cassette("test_hide_and_unhide_reply.yaml", serializer="yaml")
def test_hide_and_unhide_reply(self):
reply_id = 1344794616005066752 # Test Tweet for reply hide/unhide
- self.assertTrue(self.client.hide_reply(reply_id))
- self.assertFalse(self.client.unhide_reply(reply_id))
+ self.client.hide_reply(reply_id)
+ self.client.unhide_reply(reply_id)
@tape.use_cassette("test_like_and_unlike.yaml", serializer="yaml")
def test_like_and_unlike(self):
tweet_id = 1293593516040269825 # @TwitterDev Tweet announcing API v2
- self.assertTrue(self.client.like(tweet_id))
- self.assertFalse(self.client.unlike(tweet_id))
+ self.client.like(tweet_id)
+ self.client.unlike(tweet_id)
# TODO: test_search_all_tweets with access to Academic Research product track
@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))
+ self.client.block(user_id)
+ self.client.unblock(user_id)
@tape.use_cassette("test_follow_and_unfollow.yaml", serializer="yaml")
def test_follow_and_unfollow(self):
class Client:
- """Twitter API v2 Client
+ """Client( \
+ bearer_token=None, consumer_key=None, consumer_secret=None, \
+ access_token=None, access_token_secret=None, *, return_type=Response, \
+ wait_on_rate_limit=False \
+ )
+
+ Twitter API v2 Client
Parameters
----------
Twitter API Access Token
access_token_secret : Optional[str]
Twitter API Access Token Secret
+ return_type : Type[dict, requests.Response, Response]
+ Type to return from requests to the API
wait_on_rate_limit : bool
Whether to wait when rate limit is reached
User agent used when making requests to the API
"""
- def __init__(self, bearer_token=None, consumer_key=None,
- consumer_secret=None, access_token=None,
- access_token_secret=None, *, wait_on_rate_limit=False):
+ def __init__(
+ self, bearer_token=None, consumer_key=None, consumer_secret=None,
+ access_token=None, access_token_secret=None, *, return_type=Response,
+ wait_on_rate_limit=False
+ ):
self.bearer_token = bearer_token
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.access_token = access_token
self.access_token_secret = access_token_secret
+ self.return_type = return_type
self.wait_on_rate_limit = wait_on_rate_limit
self.session = requests.Session()
raise TwitterServerError(response)
if not 200 <= response.status_code < 300:
raise HTTPException(response)
- return response.json()
+ return response
def _make_request(self, method, route, params={}, endpoint_parameters=None,
json=None, data_type=None, user_auth=False):
response = self.request(method, route, params=request_params,
json=json, user_auth=user_auth)
+ if self.return_type is requests.Response:
+ return response
+
+ response = response.json()
+
+ if self.return_type is dict:
+ return response
+
data = response.get("data")
if data_type is not None:
if isinstance(data, list):
Returns
-------
- bool
- Indicates if the Tweet was successfully hidden.
+ Union[dict, requests.Response, Response]
References
----------
return self._make_request(
"PUT", f"/2/tweets/{id}/hidden", json={"hidden": True},
user_auth=True
- )[0]["hidden"]
+ )
def unhide_reply(self, id):
"""Unhides a reply to a Tweet.
Returns
-------
- bool
- Indicates if the Tweet was successfully unhidden.
+ Union[dict, requests.Response, Response]
References
----------
return self._make_request(
"PUT", f"/2/tweets/{id}/hidden", json={"hidden": False},
user_auth=True
- )[0]["hidden"]
+ )
# Likes
Returns
-------
- bool
- Indicates whether the user is unliking the specified Tweet as a
- result of this request. The returned value is ``False`` for a
- successful unlike request.
+ Union[dict, requests.Response, Response]
References
----------
id = self.access_token.partition('-')[0]
route = f"/2/users/{id}/likes/{tweet_id}"
- return self._make_request("DELETE", route, user_auth=True)[0]["liked"]
+ return self._make_request("DELETE", route, user_auth=True)
def like(self, tweet_id):
"""Like a Tweet.
Returns
-------
- bool
- Indicates whether the user likes the specified Tweet as a result of
- this request.
+ Union[dict, requests.Response, Response]
References
----------
return self._make_request(
"POST", route, json={"tweet_id": str(tweet_id)}, user_auth=True
- )[0]["liked"]
+ )
# Search Tweets
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
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.
+ Union[dict, requests.Response, Response]
References
----------
return self._make_request(
"DELETE", route, user_auth=True
- )[0]["blocking"]
+ )
def block(self, target_user_id):
"""Block another user.
Returns
-------
- bool
- Indicates whether the user is blocking the specified user as a
- result of this request.
+ Union[dict, requests.Response, Response]
References
----------
return self._make_request(
"POST", route, json={"target_user_id": str(target_user_id)},
user_auth=True
- )[0]["blocking"]
+ )
# Follows
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------
Returns
-------
- :ref:`response_reference`
+ Union[dict, requests.Response, Response]
References
----------