Update requests dependency, add str py2/3 compat, __repr__ definition, removed unicode2utf8 & encode static methods, update HISTORY

@ryanmcgrath Let me know if you're okay with the removal of
Twython.unicode2utf8 and Twython.encode. I moved Twython.encode to
_encode in helpers.py (only place being used is
Twython.construct_api_url) If it's python 2 and unicode then we encode
it, otherwise return the original value

[ci skip]
This commit is contained in:
Mike Helmick 2013-05-21 11:41:43 -04:00
parent 3dbef22cee
commit c7bce9189f
6 changed files with 27 additions and 21 deletions

View file

@ -20,10 +20,12 @@ if is_py2:
except ImportError:
from cgi import parse_qsl
str = unicode
basestring = basestring
elif is_py3:
from urllib.parse import urlencode, quote_plus, parse_qsl
str = str
basestring = (str, bytes)

View file

@ -1,4 +1,4 @@
from .compat import basestring
from .compat import basestring, is_py2, str
def _transparent_params(_params):
@ -17,3 +17,9 @@ def _transparent_params(_params):
else:
continue
return params, files
def _encode(value):
if is_py2 and isinstance(value, str):
value.encode('utf-8')
return value

View file

@ -6,10 +6,10 @@ from requests_oauthlib import OAuth1
from . import __version__
from .advisory import TwythonDeprecationWarning
from .compat import json, urlencode, parse_qsl, quote_plus
from .compat import json, urlencode, parse_qsl, quote_plus, str
from .endpoints import api_table
from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError
from .helpers import _transparent_params
from .helpers import _encode, _transparent_params
warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 >
@ -106,6 +106,9 @@ class Twython(object):
# create stash for last call intel
self._last_call = None
def __repr__(self):
return '<Twython: %s>' % (self.app_key)
def _constructFunc(self, api_call, deprecated_key, **kwargs):
# Go through and replace any mustaches that are in our API url.
fn = api_table[api_call]
@ -346,7 +349,14 @@ class Twython(object):
@staticmethod
def construct_api_url(base_url, params):
return base_url + '?' + '&'.join(['%s=%s' % (Twython.unicode2utf8(key), quote_plus(Twython.unicode2utf8(value))) for (key, value) in params.iteritems()])
querystring = []
params, _ = _transparent_params(params)
params = requests.utils.to_key_val_list(params)
for (k, v) in params:
querystring.append(
'%s=%s' % (_encode(k), quote_plus(_encode(v)))
)
return '%s?%s' % (base_url, '&'.join(querystring))
def searchGen(self, search_query, **kwargs):
warnings.warn(
@ -383,18 +393,3 @@ class Twython(object):
for tweet in self.searchGen(search_query, **kwargs):
yield tweet
@staticmethod
def unicode2utf8(text):
try:
if isinstance(text, unicode):
text = text.encode('utf-8')
except:
pass
return text
@staticmethod
def encode(text):
if isinstance(text, (str, unicode)):
return Twython.unicode2utf8(text)
return str(text)