Merge pull request #227 from ryanmcgrath/stream-client-args

client_args to modify the request
This commit is contained in:
Mike Helmick 2013-06-25 08:59:05 -07:00
commit b7f7d1e63d

View file

@ -20,7 +20,8 @@ import time
class TwythonStreamer(object): class TwythonStreamer(object):
def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret, def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret,
timeout=300, retry_count=None, retry_in=10, headers=None, handlers=None): timeout=300, retry_count=None, retry_in=10, client_args=None,
handlers=None):
"""Streaming class for a friendly streaming user experience """Streaming class for a friendly streaming user experience
Authentication IS required to use the Twitter Streaming API Authentication IS required to use the Twitter Streaming API
@ -36,8 +37,9 @@ class TwythonStreamer(object):
retired retired
:param retry_in: (optional) Amount of time (in secs) the previous :param retry_in: (optional) Amount of time (in secs) the previous
API call should be tried again API call should be tried again
:param headers: (optional) Custom headers to send along with the :param client_args: (optional) Accepts some requests Session parameters and some requests Request parameters.
request See http://docs.python-requests.org/en/latest/api/#sessionapi and requests section below it for details.
[ex. headers, proxies, verify(SSL verification)]
:param handlers: (optional) Array of message types for which :param handlers: (optional) Array of message types for which
corresponding handlers will be called corresponding handlers will be called
""" """
@ -45,16 +47,28 @@ class TwythonStreamer(object):
self.auth = OAuth1(app_key, app_secret, self.auth = OAuth1(app_key, app_secret,
oauth_token, oauth_token_secret) oauth_token, oauth_token_secret)
self.headers = {'User-Agent': 'Twython Streaming v' + __version__} self.client_args = client_args or {}
if headers: default_headers = {'User-Agent': 'Twython Streaming v' + __version__}
self.headers.update(headers) 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.client = requests.Session() self.client = requests.Session()
self.client.auth = self.auth self.client.auth = self.auth
self.client.headers = self.headers
self.client.stream = True self.client.stream = True
self.timeout = timeout # 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.api_version = '1.1' self.api_version = '1.1'
@ -80,12 +94,20 @@ class TwythonStreamer(object):
func = getattr(self.client, method) func = getattr(self.client, method)
def _send(retry_counter): 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: while self.connected:
try: try:
if method == 'get': if method == 'get':
response = func(url, params=params, timeout=self.timeout) requests_args['params'] = params
else: else:
response = func(url, data=params, timeout=self.timeout) requests_args['data'] = params
response = func(url, **requests_args)
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
self.on_timeout() self.on_timeout()
else: else: