Commit 61ff268d authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Fixed authentication.

parent 269eba3e
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 = host
if secure:
self._schema = 'https'
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):
self._auth = False
if secure:
self._http = httplib.HTTPSConnection(host)
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'))
def friends_timeline(self, since_id=None, max_id=None, count=None, page=None):
return self._fetch('statuses/friends_timeline.json')
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,, urllib.urlencode(parameters))
if self._parameters:
_url = '%s?%s' % (url, urllib.urlencode(parameters))
_url = '%s://%s/%s' % (self._schema,, 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
return urllib2.urlopen(req).read()
except urllib2.HTTPError, e:
raise TweepError(parse_error(
self._http.request(method, _url, body=post, headers=self._headers)
resp = self._http.getresponse()
if resp.status != 200:
raise TweepError(parse_error(
......@@ -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)
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
......@@ -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))
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(_parse_item(type, obj))
return types
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment