]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/auth.py
add doc to send tweets with images in README + consolidate api.py/README
[z_archive/twitter.git] / twitter / auth.py
index e441d5b2179d6470c6698d67a91f6cb6a33cd4bb..77633fff6eca0b166a72b1c0ea8af75628b16bbc 100644 (file)
@@ -1,20 +1,31 @@
-import urllib
-from base64 import encodestring
+try:
+    import urllib.parse as urllib_parse
+    from base64 import encodebytes
+except ImportError:
+    import urllib as urllib_parse
+    from base64 import encodestring as encodebytes
+
+class Auth(object):
+    """
+    ABC for Authenticator objects.
+    """
 
-class Auth:
     def encode_params(self, base_url, method, params):
         """Encodes parameters for a request suitable for including in a URL
         or POST body.  This method may also add new params to the request
         if required by the authentication scheme in use."""
-        raise NotImplementedError
+        raise NotImplementedError()
 
     def generate_headers(self):
         """Generates headers which should be added to the request if required
         by the authentication scheme in use."""
-        raise NotImplementedError
+        raise NotImplementedError()
 
-# An implementation using username and password.
 class UserPassAuth(Auth):
+    """
+    Basic auth authentication using email/username and
+    password. Deprecated.
+    """
     def __init__(self, username, password):
         self.username = username
         self.password = password
@@ -22,8 +33,23 @@ class UserPassAuth(Auth):
     def encode_params(self, base_url, method, params):
         # We could consider automatically converting unicode to utf8 strings
         # before encoding...
-        return urllib.urlencode(params)
+        return urllib_parse.urlencode(params)
+
+    def generate_headers(self):
+        return {b"Authorization": b"Basic " + encodebytes(
+                ("%s:%s" %(self.username, self.password))
+                .encode('utf8')).strip(b'\n')
+                }
+
+class NoAuth(Auth):
+    """
+    No authentication authenticator.
+    """
+    def __init__(self):
+        pass
+
+    def encode_params(self, base_url, method, params):
+        return urllib_parse.urlencode(params)
 
     def generate_headers(self):
-        return {"Authorization": "Basic " + encodestring("%s:%s" %(
-                self.username, self.password)).strip('\n')}
+        return {}