#!/usr/bin/python
"""This is a Python client library for the Dojo Learning REST API.
For more information and documentation, visit:
http://www.dojolearning.com/developers
Requires the simplejson package, available here:
http://pypi.python.org/pypi/simplejson
Example usage:
from dojolearning import DojoLearning
dojo = DojoLearning ('user', 'apikey')
topics = dojo.lesson_topics ();
for topic in topics:
print '
%s
' % topic['name']
lessons = dojo.lesson_list (topic['id'], 0)
for lesson in lessons:
print '%s
' % (lesson['url'], lesson['title'])
"""
__author__ = 'John Luxford '
__copyright__ = 'Copyright (c) 2008 Dojo Learning'
__license__ = 'LGPL, http://opensource.org/licenses/lgpl-2.1.php'
__version__ = '1.0'
import new
import urllib
import simplejson
class transplant:
def __init__ (self, method, host, method_name = None):
self.host = host
self.method = method
setattr (host, method_name or method.__name__, self)
def __call__ (self, *args, **kwargs):
nargs = [self.host]
nargs.extend (args)
return apply (self.method, nargs, kwargs)
# Dojo Learning client class.
class DojoLearning:
uri = 'http://api.dojolearning.com/rest-action/%s?user=%s&apikey=%s%s'
post_uri = 'http://api.dojolearning.com/rest-action/%s'
post_args = 'user=%s&apikey=%s%s'
user = ''
apikey = ''
errno = 0
error = ''
method_list = []
# Constructor method.
def __init__ (self, user, apikey):
self.user = user
self.apikey = apikey
# Generate the dynamic methods
self.method_list = self.send_and_receive ('', '')
for k, v in self.method_list.iteritems ():
self.add_method (k, v)
# Send, receive and parse the request.
def send_and_receive (self, request, params):
# determine request type
if self.method_list and self.method_list[request]:
method = self.method_list[request]['method']
else:
method = 'GET'
if method == 'GET':
# handle GET requests
url = self.uri % (request, self.user, self.apikey, params)
try:
req = urllib.urlopen (url)
except IOError:
self.errno = 401
self.error = 'Unauthorized'
return None
else:
# handle POST requests
url = self.post_uri % (request)
params = self.post_args % (self.user, self.apikey, params)
try:
req = urllib.urlopen (url, params)
except IOError:
self.errno = 401
self.error = 'Unauthorized'
return None
res = req.read ()
return simplejson.loads (res)
# Dynamically add methods to the object
def add_method (self, name, data):
method_name = name.replace ('.', '_')
code = "def " + method_name + ' (self'
for param in data['parameters']:
code += ', ' + param
code += "):\n"
code += "\tm = '" + name + "'\n"
code += "\tp = ''\n"
if len (data['parameters']) > 0:
for k in data['parameters']:
code += "\tp += '&" + k + "=' + urllib.quote_plus(str(" + k + "))\n"
code += "\treturn self.send_and_receive (m, p)\n"
code += "\n\n"
#print code
exec (code)
func = eval(method_name);
transplant (func, self, method_name)
if __name__ == '__main__':
dojo = DojoLearning ('user', 'apikey')
topics = dojo.lesson_topics ()
for topic in topics:
print '%s
' % topic['name']
lessons = dojo.lesson_list (topic['id'], 0)
for lesson in lessons:
print '%s
' % (lesson['url'], lesson['title'])