general improvements to get_forums and Forum()

- allow direct creation of Forum objects
- make Forum.api_key work better
This commit is contained in:
James Turk 2009-05-15 00:03:41 -04:00
parent ae88a58670
commit 16f2d7ff8c

View File

@ -18,6 +18,12 @@ except ImportError:
api_url = 'http://disqus.com/api/%s/' api_url = 'http://disqus.com/api/%s/'
def deunicode(ad):
return dict((str(k),v) for k,v in ad.iteritems())
class DisqusError(Exception):
''' base class for Disqus API errors '''
def apicall(method, params, http_method='GET'): def apicall(method, params, http_method='GET'):
params = urllib.urlencode(params) params = urllib.urlencode(params)
if http_method == 'GET': if http_method == 'GET':
@ -25,8 +31,10 @@ def apicall(method, params, http_method='GET'):
response = urllib2.urlopen(url).read() response = urllib2.urlopen(url).read()
else: else:
response = urllib2.urlopen(api_url % method, params).read() response = urllib2.urlopen(api_url % method, params).read()
msg = json.loads(response)['message'] obj = json.loads(response)
return msg if not obj['succeeded']:
raise DisqusError(obj['message'])
return obj['message']
class Thread(object): class Thread(object):
def __init__(self, d, forum_api_key): def __init__(self, d, forum_api_key):
@ -68,21 +76,25 @@ class Thread(object):
return apicall('create_post', params, 'POST') return apicall('create_post', params, 'POST')
class Forum(object): class Forum(object):
def __init__(self, d, user_key): def __init__(self, forum_api_key=None, id=None, name=None, shortname=None,
for k,v in d.iteritems(): created_at=None, user_api_key=None):
setattr(self, k, v) self.__dict__['api_key'] = forum_api_key
self._api_key = None self.id = id
self.user_api_key = user_key self.name = name
self.shortname = shortname
self.created_at = created_at
self.user_api_key = user_api_key
@property @property
def api_key(self): def api_key(self):
if not self._api_key: if not self.__dict__['api_key']:
print 'fetching api_key'
msg = apicall('get_forum_api_key', {'user_api_key':self.user_api_key, msg = apicall('get_forum_api_key', {'user_api_key':self.user_api_key,
'forum_id':self.id}) 'forum_id':self.id})
self._api_key = msg self.__dict__['api_key'] = msg
return self._api_key return self.__dict__['api_key']
def get_thread_list(self): def get_threads(self):
msg = apicall('get_thread_list', {'forum_api_key':self.api_key}) msg = apicall('get_thread_list', {'forum_api_key':self.api_key})
return [Thread(t, self.api_key) for t in msg] return [Thread(t, self.api_key) for t in msg]
@ -98,6 +110,6 @@ class Forum(object):
msg = apicall('thread_by_identifier', {'forum_api_key': self.api_key, 'title': title, 'identifier': identifier}, 'POST') msg = apicall('thread_by_identifier', {'forum_api_key': self.api_key, 'title': title, 'identifier': identifier}, 'POST')
return Thread(msg['thread'], self.api_key), msg['created'] return Thread(msg['thread'], self.api_key), msg['created']
def get_forum_list(user_key): def get_forums(user_api_key):
msg = apicall('get_forum_list', {'user_api_key':user_key}) msg = apicall('get_forum_list', {'user_api_key':user_api_key})
return [Forum(f, user_key) for f in msg] return [Forum(user_api_key=user_api_key, **deunicode(f)) for f in msg]