I had some time since several nights (thanks kids to be sick…) to start watching how to implement a REST API using node.js (my new friend).
I plan to create an OW2 API this year; but before I started to wrap some existing services as proof of concept. The first feature focuses on how to get projects data from the gitorious instance we run at OW2. There is no clear API for gitorious but after some googling and source code search, it looks that there is a read-only XML API. Since I am targeting node.js, let’s translate this API into a JSON one: xml2js will do the job.
This results in a first library (gitoriou.js) which just call the gitorious instance and translates the XML data into JSON without any other data mapping. The following gist is showing how to get information from a project:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Use the OW2.org gitorious instance for samples. | |
* | |
* Copyright(c) 2013 Christophe Hamerling <christophe.hamerling@gmail.com> | |
* MIT Licensed | |
*/ | |
var Gitorious = require('../index').Gitorious | |
, _ = require('underscore'); | |
var client = new Gitorious({url:'http://gitorious.ow2.org'}); | |
console.log('# Getting OW2 Shelbie information : ') | |
client.getProject('ow2-shelbie', function(err, result) { | |
if (err) { | |
console.log('Error:', err); | |
return; | |
} | |
// console.log(JSON.stringify(result, null, 4)); | |
console.log('## Title:', result.project.title); | |
console.log('### Repositories'); | |
_.each(result.project.repositories.mainlines.repository, function(item) { | |
console.log(' – ', item.clone_url); | |
}) | |
}); |
I talked about having a REST API, let’s use express.js for that and let’s run it on Heroku (this platform rules, just took 2 minutes to create, deploy and run the service and it is up at http://ow2apisample.herokuapp.com/).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* OW2 API Sample | |
* | |
* Copyright(c) 2013 Christophe Hamerling <christophe.hamerling@gmail.com> | |
* MIT Licensed | |
*/ | |
var express = require('express') | |
, http = require('http') | |
, path = require('path') | |
, app = express(); | |
var Gitorious = require('gitoriou.js').Gitorious; | |
var config = { | |
url : 'http://gitorious.ow2.org', | |
port : 3000 | |
} | |
var client = new Gitorious(config); | |
app.configure(function() { | |
app.set('port', process.env.PORT || config.port); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.static(path.join(__dirname, '../public'))); | |
}); | |
app.configure('development', function() { | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
app.configure('production', function() { | |
app.use(express.errorHandler()); | |
}); | |
app.get('/projects', function(req, res) { | |
client.getProjects(function(err, result) { | |
if (err) { | |
res.json(500, err); | |
} else { | |
res.json(200, result); | |
} | |
}); | |
}); | |
app.get('/project/:name', function(req, res) { | |
client.getProject(req.param('name'), function(err, result) { | |
if (err) { | |
res.json(500, err); | |
} else { | |
res.json(200, result); | |
} | |
}); | |
}); | |
var server = http.createServer(app); | |
server.listen(app.get('port'), function () { | |
console.log('OW2 Server is started and listening on', app.get('port')); | |
}); |
To get information (as JSON) about the Jasmine project, just HTTP GET http://ow2apisample.herokuapp.com/project/ow2-jasmine
If you master any scripting language, you will be able to get all the repositories URLs from the JSON response, if not you can use the ow2git project to clone them all. Assuming node.js and npm are available on your system (you already have java, ruby and every other stuff installed, why not adding node?), you can install the binary:
npm install -g ow2git
and then clone all the repositories from any gitorious project
ow2git –clone ow2-jasmine
Will clone all the ow2-jasmine repositories into your current folder.
I am really impressed by node.js runtime, tools and community. Even if all of this is just a proof of concept, not really well designed, you can have something running quickly without many effort. Time to get something real. Soon…