From 61ff268de4c0d35e2618c70fbf65b7edbdbff1aa Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Sun, 5 Jul 2009 17:57:26 -0500 Subject: [PATCH] Fixed authentication. --- api.py | 49 +++++++++++++++++++++++++++---------------------- misc.py | 17 ++++++++++++++++- parsers.py | 18 +++++++++++------- 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/api.py b/api.py index 7db9bc4..24395f2 100644 --- 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 0a43223..db72838 100644 --- 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 """ diff --git a/parsers.py b/parsers.py index 45e45a0..b563d27 100644 --- a/parsers.py +++ b/parsers.py @@ -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 -- 2.25.1