Fixed authentication.
authorJosh Roesslein <jroesslein@gmail.com>
Sun, 5 Jul 2009 22:57:26 +0000 (17:57 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Sun, 5 Jul 2009 22:57:26 +0000 (17:57 -0500)
api.py
misc.py
parsers.py

diff --git a/api.py b/api.py
index 7db9bc406d5ce93e1499ace01596b3e59b3c0bf4..24395f2a7157030d306fa0587c945badb2a34b8c 100644 (file)
--- a/api.py
+++ b/api.py
@@ -1,8 +1,8 @@
 import urllib
-import urllib2
+import httplib
 import base64
 
-from misc import TweepError, require_auth
+from misc import TweepError, require_auth, process_param
 from models import Status, User
 from parsers import *
 
@@ -16,11 +16,9 @@ class API(object):
                 user_class=User, status_class=Status):
     self._Status = status_class
     self._User = user_class
-    self.host = host
-    if secure:
-      self._schema = 'https'
-    else:
-      self._schema = 'http'
+    self._Status._User = self._User
+    self._parameters = None
+    self._post_data = None
 
     # Setup headers
     self._headers = {}
@@ -32,30 +30,37 @@ class API(object):
     else:
       self._auth = False
 
+    if secure:
+      self._http = httplib.HTTPSConnection(host)
+    else:
+      self._http = httplib.HTTPConnection(host)
+
   def public_timeline(self):
-    return parse_list(self._Status, self._fetch('statuses/public_timeline.json'))
+    return parse_list(self._Status, self._fetch('/statuses/public_timeline.json'))
 
   @require_auth
-  def friends_timeline(self, since_id=None, max_id=None, count=None, page=None):
-    return self._fetch('statuses/friends_timeline.json')
+  @process_param(['since_id'])
+  def friends_timeline(self, **kargs):
+    if self._parameters:
+      for k,v in self._parameters.items():
+        print k,v
+    #return parse_list(self._Status, self._fetch('/statuses/friends_timeline.json'))
 
-  def _fetch(self, url, parameters=None, post_data=None):
+  def _fetch(self, url, method='GET'):
     # Build the url
-    if parameters:
-      _url = '%s://%s/%s?%s' % (self._schema, self.host, urllib.urlencode(parameters))
+    if self._parameters:
+      _url = '%s?%s' % (url, urllib.urlencode(parameters))
     else:
-      _url = '%s://%s/%s' % (self._schema, self.host, url)
+      _url = url
 
     # Encode post data
     post = None
-    if post_data:
+    if self._post_data:
       post = urllib.encode(post_data)
 
-    # Build the request
-    req = urllib2.Request(_url, post, self._headers)
-
     # Send request
-    try:
-      return urllib2.urlopen(req).read()
-    except urllib2.HTTPError, e:
-      raise TweepError(parse_error(e.read()))
+    self._http.request(method, _url, body=post, headers=self._headers)
+    resp = self._http.getresponse()
+    if resp.status != 200:
+      raise TweepError(parse_error(resp.read()))
+    return resp.read()
diff --git a/misc.py b/misc.py
index 0a43223a694584d8ab3452985344e2aef94643c3..db72838c9894c8492734ff830a7a264e281be738 100644 (file)
--- a/misc.py
+++ b/misc.py
@@ -4,11 +4,26 @@ Only allow method to be called with authentication.
 def require_auth(func):
   def wrapper(instance, *args, **kargs):
     if instance._auth:
-      func(instance, *args, **kargs)
+      return func(instance, **kargs)
     else:
       print 'require auth'
   return wrapper
 
+"""
+Process parameters. Perform validation. Build parameter list.
+"""
+def process_param(allowed_param):
+  def decorator(func):
+    def wrapper(instance, **kargs):
+      if len(kargs):
+        instance._parameters = {}
+        for k,v in kargs.items():
+          if k in allowed_param:
+            instance._parameters[k] = v
+      return func(instance, **kargs)
+    return wrapper
+  return decorator
+
 """
 Tweepy exception
 """
index 45e45a0f9c437a3f3fd9dc385f039c0a97239b56..b563d27a283f61a66cff317080e785e2dee57e1d 100644 (file)
@@ -7,19 +7,23 @@ def parse_error(data):
 
   return json.loads(data)['error']
 
-def parse_item(type, data):
+def _parse_item(type, jsondata):
   t = type()
-  for k,v in json.loads(data).items():
-    setattr(t, k, v)
+  for k,v in jsondata.items():
+    if k == 'user':
+      setattr(t,k, _parse_item(type._User, v))
+    else:
+      setattr(t,k,v)
   return t
 
+def parse_item(type, data):
+  jsondata = json.loads(data)
+  return _parse_item(type, jsondata)
+
 def parse_list(type, data):
   types = []
 
   for obj in json.loads(data):
-    t = type()
-    for k,v in obj.items():
-      setattr(t, k, v)
-    types.append(t)
+    types.append(_parse_item(type, obj))
 
   return types