diff --git a/twython/api.py b/twython/api.py index a133590..f4a7a0b 100644 --- a/twython/api.py +++ b/twython/api.py @@ -9,6 +9,8 @@ Twitter Authentication, and miscellaneous methods that are useful when dealing with the Twitter API """ +import sys + import requests from requests.auth import HTTPBasicAuth from requests_oauthlib import OAuth1, OAuth2 @@ -131,7 +133,10 @@ class Twython(EndpointsMixin, object): 'data': params, 'files': files, }) - response = func(url, **requests_args) + try: + response = func(url, **requests_args) + except requests.RequestException as e: + raise TwythonError, str(e), sys.exc_info()[2] content = response.content.decode('utf-8') # create stash for last function intel diff --git a/twython/streaming/api.py b/twython/streaming/api.py index c2fcec8..244a550 100644 --- a/twython/streaming/api.py +++ b/twython/streaming/api.py @@ -20,8 +20,7 @@ import time class TwythonStreamer(object): def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret, - timeout=300, retry_count=None, retry_in=10, client_args=None, - handlers=None): + timeout=300, retry_count=None, retry_in=10, headers=None, handlers=None): """Streaming class for a friendly streaming user experience Authentication IS required to use the Twitter Streaming API @@ -37,9 +36,8 @@ class TwythonStreamer(object): retired :param retry_in: (optional) Amount of time (in secs) the previous API call should be tried again - :param client_args: (optional) Accepts some requests Session parameters and some requests Request parameters. - See http://docs.python-requests.org/en/latest/api/#sessionapi and requests section below it for details. - [ex. headers, proxies, verify(SSL verification)] + :param headers: (optional) Custom headers to send along with the + request :param handlers: (optional) Array of message types for which corresponding handlers will be called """ @@ -47,28 +45,16 @@ class TwythonStreamer(object): self.auth = OAuth1(app_key, app_secret, oauth_token, oauth_token_secret) - self.client_args = client_args or {} - default_headers = {'User-Agent': 'Twython Streaming v' + __version__} - if not 'headers' in self.client_args: - # If they didn't set any headers, set our defaults for them - self.client_args['headers'] = default_headers - elif 'User-Agent' not in self.client_args['headers']: - # If they set headers, but didn't include User-Agent.. set it for them - self.client_args['headers'].update(default_headers) - self.client_args['timeout'] = timeout + self.headers = {'User-Agent': 'Twython Streaming v' + __version__} + if headers: + self.headers.update(headers) self.client = requests.Session() self.client.auth = self.auth + self.client.headers = self.headers self.client.stream = True - # Make a copy of the client args and iterate over them - # Pop out all the acceptable args at this point because they will - # Never be used again. - client_args_copy = self.client_args.copy() - for k, v in client_args_copy.items(): - if k in ('cert', 'headers', 'hooks', 'max_redirects', 'proxies'): - setattr(self.client, k, v) - self.client_args.pop(k) # Pop, pop! + self.timeout = timeout self.api_version = '1.1' @@ -94,20 +80,12 @@ class TwythonStreamer(object): func = getattr(self.client, method) def _send(retry_counter): - requests_args = {} - for k, v in self.client_args.items(): - # Maybe this should be set as a class variable and only done once? - if k in ('timeout', 'allow_redirects', 'verify'): - requests_args[k] = v - while self.connected: try: if method == 'get': - requests_args['params'] = params + response = func(url, params=params, timeout=self.timeout) else: - requests_args['data'] = params - - response = func(url, **requests_args) + response = func(url, data=params, timeout=self.timeout) except requests.exceptions.Timeout: self.on_timeout() else: