From 4e690f5568b450b27d0f54691e406219d5567832 Mon Sep 17 00:00:00 2001 From: Edward Hades Date: Mon, 28 Feb 2011 19:10:07 +0800 Subject: [PATCH 1/2] Parameters now can be of any type. The patch by Eugene (9133d0f10ec) is nice, because you don't have to manually encode() the parameters yourself. But it sucks a little, because all your parameter values must be unicodes now. Proposed patch encodes unicodes to utf-8 and allows the olde strs and longs (I love to use longs in 'id' fields somewhy). --- twython/twython.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/twython/twython.py b/twython/twython.py index 7ce8973..7232c20 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -176,7 +176,7 @@ class Twython(object): # Then open and load that shiiit, yo. TODO: check HTTP method and junk, handle errors/authentication if fn['method'] == 'POST': - resp, content = self.client.request(base, fn['method'], urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in kwargs.items())), headers = self.headers) + resp, content = self.client.request(base, fn['method'], urllib.urlencode(dict([k, Twython.encode(v)] for k, v in kwargs.items())), headers = self.headers) else: url = base + "?" + "&".join(["%s=%s" %(key, value) for (key, value) in kwargs.iteritems()]) resp, content = self.client.request(url, fn['method'], headers = self.headers) @@ -463,3 +463,9 @@ class Twython(object): except: pass return text + + @staticmethod + def encode(text): + if isinstance(text, (str,unicode)): + return Twython.unicode2utf8(text) + return str(text) From f184595ccf31ee235e5d0776cebc9d03e15d0d67 Mon Sep 17 00:00:00 2001 From: Edward Hades Date: Mon, 28 Feb 2011 22:48:44 +0800 Subject: [PATCH 2/2] Allow passing arbitrary arguments to client backend. Useful for timeout, for example. --- twython/twython.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/twython/twython.py b/twython/twython.py index 7232c20..91e9942 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -100,7 +100,7 @@ class AuthError(TwythonError): class Twython(object): - def __init__(self, twitter_token = None, twitter_secret = None, oauth_token = None, oauth_token_secret = None, headers=None, callback_url=None): + def __init__(self, twitter_token = None, twitter_secret = None, oauth_token = None, oauth_token_secret = None, headers=None, callback_url=None, client_args={}): """setup(self, oauth_token = None, headers = None) Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below). @@ -112,6 +112,7 @@ class Twython(object): pass it in and it'll be used for all requests going forward. oauth_token_secret - see oauth_token; it's the other half. headers - User agent header, dictionary style ala {'User-Agent': 'Bert'} + client_args - additional arguments for HTTP client (see httplib2.Http.__init__), e.g. {'timeout': 10.0} ** Note: versioning is not currently used by search.twitter functions; when Twitter moves their junk, it'll be supported. """ @@ -142,12 +143,12 @@ class Twython(object): # Filter down through the possibilities here - if they have a token, if they're first stage, etc. if consumer is not None and token is not None: - self.client = oauth.Client(consumer, token) + self.client = oauth.Client(consumer, token, **client_args) elif consumer is not None: - self.client = oauth.Client(consumer) + self.client = oauth.Client(consumer, **client_args) else: # If they don't do authentication, but still want to request unprotected resources, we need an opener. - self.client = httplib2.Http() + self.client = httplib2.Http(**client_args) def __getattr__(self, api_call): """