Autodetect username inside the API.me() method.
authorJosh Roesslein <jroesslein@gmail.com>
Thu, 13 Aug 2009 20:39:16 +0000 (15:39 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Thu, 13 Aug 2009 20:39:16 +0000 (15:39 -0500)
CHANGES
ROADMAP
tweepy/api.py

diff --git a/CHANGES b/CHANGES
index f3fe1c7bcec351424f83fe03215e4f4b122ef25c..7ed169cc2f51a5a92749169079e5e99d44d48c8f 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,9 @@ during upgrade will be listed here.
 + User:
     + follow()
     + unfollow()
++ API:
+    + __init__() signature change; no longer accepts 'username' parameter
+      which is now autodetected.
 + Fixes
     + User.following is now set to False instead of None
       when user is not followed.
diff --git a/ROADMAP b/ROADMAP
index e2b61d0695fc7d9fbb0cde0ee5aad676cc293cdb..85f3b133d416c9a293f9345b83d67e506dcbdc9e 100644 (file)
--- a/ROADMAP
+++ b/ROADMAP
@@ -9,5 +9,5 @@ The plan of attack for the next version of Tweepy.
 + rate limit governor
 + prepare for social graph changes mentioned on mailinglist
 + finish search api
-+ autodetect authenticated user's ID
++ autodetect authenticated user's ID [DONE]
 
index 0a74ab177a6647a771996c758b926e7d541b4a2b..7f39cfccc4e1112c27f1249212f2e49008344fdc 100644 (file)
@@ -9,16 +9,19 @@ from . error import TweepError
 """Twitter API"""
 class API(object):
 
-  def __init__(self, auth_handler=None, username=None, host='twitter.com', cache=None,
+  def __init__(self, auth_handler=None, host='twitter.com', cache=None,
                 secure=False, api_root='', validate=True):
+    # you may access these freely
     self.auth_handler = auth_handler
-    self.username = username
     self.host = host
     self.api_root = api_root
     self.cache = cache
     self.secure = secure
     self.validate = validate
 
+    # not a good idea to touch these
+    self._username = None
+
   """Get public timeline"""
   public_timeline = bind_api(
       path = '/statuses/public_timeline.json',
@@ -84,10 +87,18 @@ class API(object):
 
   """Get authenticated user"""
   def me(self):
-    if self.username:
-      return self.get_user(screen_name=self.username)
-    else:
-      return None
+    # if username not fetched, go get it...
+    if self._username is None:
+      if self.auth_handler is None:
+        raise TweepError('Authentication required')
+
+      try:
+        user = bind_api(path='/account/verify_credentials.json', parser=parse_user)(self)
+      except TweepError, e:
+        raise TweepError('Failed to fetch username: %s' % e)
+      self._username = user.screen_name
+
+    return self.get_user(screen_name=self._username)
 
   """Show friends"""
   friends = bind_api(