Skip to content

Commit

Permalink
Merge pull request #500 from manuelcortez/master
Browse files Browse the repository at this point in the history
Added support for the new direct message endpoints
  • Loading branch information
michaelhelmick authored Apr 10, 2019
2 parents a5e3124 + 4f29fd0 commit 7366de8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
30 changes: 21 additions & 9 deletions twython/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ def __init__(self, app_key=None, app_secret=None, oauth_token=None,
def __repr__(self):
return '<Twython: %s>' % (self.app_key)

def _request(self, url, method='GET', params=None, api_call=None):
def _request(self, url, method='GET', params=None, api_call=None, json_encoded=False):
"""Internal request method"""
method = method.lower()
params = params or {}

func = getattr(self.client, method)
if isinstance(params, dict):
if isinstance(params, dict) and json_encoded is False:
params, files = _transparent_params(params)
else:
params = params
Expand All @@ -153,11 +153,16 @@ def _request(self, url, method='GET', params=None, api_call=None):
if k in ('timeout', 'allow_redirects', 'stream', 'verify'):
requests_args[k] = v

if method == 'get':
if method == 'get' or method == 'delete':
requests_args['params'] = params
else:
# Check for json_encoded so we will sent params as "data" or "json"
if json_encoded:
data_key = 'json'
else:
data_key = 'data'
requests_args.update({
'data': params,
data_key: params,
'files': files,
})
try:
Expand Down Expand Up @@ -230,14 +235,14 @@ def _get_error_message(self, response):

return error_message

def request(self, endpoint, method='GET', params=None, version='1.1'):
def request(self, endpoint, method='GET', params=None, version='1.1', json_encoded=False):
"""Return dict of response received from Twitter's API
:param endpoint: (required) Full url or Twitter API endpoint
(e.g. search/tweets)
:type endpoint: string
:param method: (optional) Method of accessing data, either
GET or POST. (default GET)
GET, POST or DELETE. (default GET)
:type method: string
:param params: (optional) Dict of parameters (if any) accepted
the by Twitter API endpoint you are trying to
Expand All @@ -246,6 +251,9 @@ def request(self, endpoint, method='GET', params=None, version='1.1'):
:param version: (optional) Twitter API version to access
(default 1.1)
:type version: string
:param json_encoded: (optional) Flag to indicate if this method should send data encoded as json
(default False)
:type json_encoded: bool
:rtype: dict
"""
Expand All @@ -261,17 +269,21 @@ def request(self, endpoint, method='GET', params=None, version='1.1'):
url = '%s/%s.json' % (self.api_url % version, endpoint)

content = self._request(url, method=method, params=params,
api_call=url)
api_call=url, json_encoded=json_encoded)

return content

def get(self, endpoint, params=None, version='1.1'):
"""Shortcut for GET requests via :class:`request`"""
return self.request(endpoint, params=params, version=version)

def post(self, endpoint, params=None, version='1.1'):
def post(self, endpoint, params=None, version='1.1', json_encoded=False):
"""Shortcut for POST requests via :class:`request`"""
return self.request(endpoint, 'POST', params=params, version=version)
return self.request(endpoint, 'POST', params=params, version=version, json_encoded=json_encoded)

def delete(self, endpoint, params=None, version='1.1', json_encoded=False):
"""Shortcut for delete requests via :class:`request`"""
return self.request(endpoint, 'DELETE', params=params, version=version, json_encoded=json_encoded)

def get_lastfunction_header(self, header, default_return_value=None):
"""Returns a specific header from the last API call
Expand Down
16 changes: 8 additions & 8 deletions twython/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ def get_direct_messages(self, **params):
"""Returns the 20 most recent direct messages sent to the authenticating user.
Docs:
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-messages
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
"""
return self.get('direct_messages', params=params)
return self.get('direct_messages/events/list', params=params)
get_direct_messages.iter_mode = 'id'

def get_sent_messages(self, **params):
Expand All @@ -320,29 +320,29 @@ def get_direct_message(self, **params):
"""Returns a single direct message, specified by an ``id`` parameter.
Docs:
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-message
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-event
"""
return self.get('direct_messages/show', params=params)
return self.get('direct_messages/events/show', params=params)

def destroy_direct_message(self, **params):
"""Destroys the direct message specified in the required ``id`` parameter
Docs:
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/delete-message
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/delete-message-event
"""
return self.post('direct_messages/destroy', params=params)
return self.delete('direct_messages/events/destroy', params=params)

def send_direct_message(self, **params):
"""Sends a new direct message to the specified user from the
authenticating user.
Docs:
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-message
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
"""
return self.post('direct_messages/new', params=params)
return self.post('direct_messages/events/new', params=params, json_encoded=True)

# Friends & Followers
def get_user_ids_of_blocked_retweets(self, **params):
Expand Down

0 comments on commit 7366de8

Please sign in to comment.