]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/api.py
typo
[z_archive/twitter.git] / twitter / api.py
index 4933f88c438cfb269c6d1ca9067975c95b33c6cd..42d9e53a62aee3f00c8959ac9822805688023c1f 100644 (file)
@@ -4,7 +4,7 @@ import urllib2
 from exceptions import Exception
 
 from twitter.twitter_globals import POST_ACTIONS
-from twitter.auth import UserPassAuth
+from twitter.auth import UserPassAuth, NoAuth
 
 def _py26OrGreater():
     import sys
@@ -17,11 +17,26 @@ else:
 
 class TwitterError(Exception):
     """
-    Exception thrown by the Twitter object when there is an
-    error interacting with twitter.com.
+    Base Exception thrown by the Twitter object when there is a
+    general error interacting with the API.
     """
     pass
 
+class TwitterHTTPError(TwitterError):
+    """
+    Exception thrown by the Twitter object when there is an
+    HTTP error interacting with twitter.com.
+    """
+    def __init__(self, e, uri, format, encoded_args):
+      self.e = e
+      self.uri = uri
+      self.format = format
+      self.encoded_args = encoded_args
+
+    def __str__(self):
+        return "Twitter sent status %i for URL: %s.%s using parameters: (%s)\ndetails: %s" %(
+                    self.e.code, self.uri, self.format, self.encoded_args, self.e.fp.read())
+
 class TwitterCall(object):
     def __init__(
         self, auth, format, domain, uri="", agent=None,
@@ -55,8 +70,11 @@ class TwitterCall(object):
         secure_str = ''
         if self.secure:
             secure_str = 's'
-        uriBase = "http%s://%s/%s.%s" %(
-                    secure_str, self.domain, uri, self.format)
+        dot = ""
+        if self.format != '':
+            dot = "."
+        uriBase = "http%s://%s/%s%s%s" %(
+                    secure_str, self.domain, uri, dot, self.format)
 
         if (not self.encoded_args):
             if kwargs.has_key('id'):
@@ -90,9 +108,7 @@ class TwitterCall(object):
             if (e.code == 304):
                 return []
             else:
-                raise TwitterError(
-                    "Twitter sent status %i for URL: %s.%s using parameters: (%s)\ndetails: %s" %(
-                        e.code, uri, self.format, self.encoded_args, e.fp.read()))
+                raise TwitterHTTPError(e, uri, self.format, self.encoded_args)
 
 class Twitter(TwitterCall):
     """
@@ -171,10 +187,13 @@ class Twitter(TwitterCall):
                 raise ValueError, "can't specify 'email' or 'password' and 'auth' params"
             auth = UserPassAuth(email, password)
 
-        if (format not in ("json", "xml")):
+        if not auth:
+            auth = NoAuth()
+
+        if (format not in ("json", "xml", "")):
             raise TwitterError("Unknown data format '%s'" %(format))
         TwitterCall.__init__(
             self, auth, format, domain, "", agent, 
             secure=secure)
 
-__all__ = ["Twitter", "TwitterError"]
+__all__ = ["Twitter", "TwitterError", "TwitterHTTPError"]