]> jfr.im git - z_archive/twitter.git/commitdiff
Merge pull request #287 from jessamynsmith/master
authorMike Verdone <redacted>
Mon, 20 Apr 2015 19:22:35 +0000 (21:22 +0200)
committerMike Verdone <redacted>
Mon, 20 Apr 2015 19:22:35 +0000 (21:22 +0200)
Improved error handling

I had to make a small change to get tests running on my environment. Then I added better checking on OAuth credentials, so the code doesn't fail out later with an obscure message. Finally, I brought the handling of error data in line with the handling of response data. This last does change behaviour, and if you don't want that, I can back out that change.

tests/test_util.py
twitter/api.py
twitter/auth.py
twitter/oauth.py
twitter/oauth2.py

index d7a1f6ee8cf7f9830bedfd11ab51e0d9fd4f4fbb..7180c382eff1c7e428ef9d7ef0d9604fc78025cc 100644 (file)
@@ -34,7 +34,7 @@ def start_server(*resp):
     """HTTP server replying with the given responses to the expected
     requests."""
     def url(port, path):
-        return 'http://%s:%s%s' % (socket.gethostname(), port, path)
+        return 'http://%s:%s%s' % (socket.gethostname().lower(), port, path)
 
     responses = list(reversed(resp))
 
@@ -106,7 +106,7 @@ def test_follow_redirects_filtered_by_site_after_redirect():
     with start_server(
         Response(link, 301, {"Location": redirected}),
         Response(redirected, 301, {"Location": filtered})) as url:
-        hosts = [socket.gethostname()]
+        hosts = [socket.gethostname().lower()]
         assert filtered == follow_redirects(url(link), hosts)
 
 def test_follow_redirects_filtered_by_site_allowed():
@@ -115,7 +115,7 @@ def test_follow_redirects_filtered_by_site_allowed():
     with start_server(
         Response(link, 301, {"Location": redirected}),
         Response(redirected, 200, {})) as url:
-        hosts = [socket.gethostname()]
+        hosts = [socket.gethostname().lower()]
         assert url(redirected) == follow_redirects(url(link), hosts)
 
 def test_expand_line():
index 94574c4f7cf60279323246977ab925d872df1370..bb1ca6c2be5c527aba1def43f5233ee8508afc8c 100644 (file)
@@ -65,9 +65,14 @@ class TwitterHTTPError(TwitterError):
         if self.e.headers.get('Content-Encoding') == 'gzip':
             buf = StringIO(data)
             f = gzip.GzipFile(fileobj=buf)
-            self.response_data = f.read()
+            data = f.read()
+        if len(data) == 0:
+            data = {}
+        elif "json" == self.format:
+            data = json.loads(data.decode('utf8'))
         else:
-            self.response_data = data
+            data = data.decode('utf8')
+        self.response_data = data
         super(TwitterHTTPError, self).__init__(str(self))
 
     def __str__(self):
index 77633fff6eca0b166a72b1c0ea8af75628b16bbc..45f1f4c001ae21df31a0aa904a5aad06aafaee97 100644 (file)
@@ -5,6 +5,7 @@ except ImportError:
     import urllib as urllib_parse
     from base64 import encodestring as encodebytes
 
+
 class Auth(object):
     """
     ABC for Authenticator objects.
@@ -21,6 +22,7 @@ class Auth(object):
         by the authentication scheme in use."""
         raise NotImplementedError()
 
+
 class UserPassAuth(Auth):
     """
     Basic auth authentication using email/username and
@@ -41,6 +43,7 @@ class UserPassAuth(Auth):
                 .encode('utf8')).strip(b'\n')
                 }
 
+
 class NoAuth(Auth):
     """
     No authentication authenticator.
@@ -53,3 +56,7 @@ class NoAuth(Auth):
 
     def generate_headers(self):
         return {}
+
+
+class MissingCredentialsError(Exception):
+    pass
index 2df5ff0a1eb9a0319d6ad959ac21702d2edd05de..b0d7f41b4bc77aec6c7f85a8172413d89e714ba3 100644 (file)
@@ -57,7 +57,7 @@ import hashlib
 import hmac
 import base64
 
-from .auth import Auth
+from .auth import Auth, MissingCredentialsError
 
 
 def write_token_file(filename, oauth_token, oauth_token_secret):
@@ -92,6 +92,10 @@ class OAuth(Auth):
         self.consumer_key = consumer_key
         self.consumer_secret = consumer_secret
 
+        if token_secret is None or consumer_secret is None:
+            raise MissingCredentialsError(
+                'You must supply strings for token_secret and consumer_secret, not None.')
+
     def encode_params(self, base_url, method, params):
         params = params.copy()
 
index 6c69c2b59cbdd648a0389a9508e6bc20dff3cfd1..fb5fefe9e4543aefb534b8d67ae76c354d3d8c82 100644 (file)
@@ -31,7 +31,7 @@ except ImportError:
     from urllib import quote, urlencode
 
 from base64 import b64encode
-from .auth import Auth
+from .auth import Auth, MissingCredentialsError
 
 def write_bearer_token_file(filename, oauth2_bearer_token):
     """
@@ -89,7 +89,3 @@ class OAuth2(Auth):
                 ).encode('utf8')
             }
         return headers
-
-
-class MissingCredentialsError(Exception):
-    pass