APIv2: add paths /api/v2/* and /api/v1/*. v1 has limited deprecated routes (only the things I know currently work), and we'll notify 3rd-partyists to migrate to apiv2 once it's documented and tested

This commit is contained in:
Tyler Renelle
2013-12-15 17:22:35 -07:00
parent 91e1287d06
commit 259f9033c4
21 changed files with 213 additions and 117 deletions
-76
View File
@@ -1,76 +0,0 @@
var express = require('express');
var router = new express.Router();
var _ = require('lodash');
var icalendar = require('icalendar');
var api = require('./user');
var auth = require('./auth');
/* ---------- Deprecated Paths ------------*/
var deprecatedMessage = 'This API is no longer supported, see https://github.com/lefnire/habitrpg/wiki/API for new protocol';
router.get('/:uid/up/:score?', function(req, res) {
return res.send(500, deprecatedMessage);
});
router.get('/:uid/down/:score?', function(req, res) {
return res.send(500, deprecatedMessage);
});
router.post('/users/:uid/tasks/:taskId/:direction', function(req, res) {
return res.send(500, deprecatedMessage);
});
/* Redirect to new API*/
var initDeprecated = function(req, res, next) {
req.headers['x-api-user'] = req.params.uid;
req.headers['x-api-key'] = req.body.apiToken;
return next();
};
router.post('/v1/users/:uid/tasks/:taskId/:direction', initDeprecated, auth.auth, api.score);
router.get('/v1/users/:uid/calendar.ics', function(req, res, next) {
return next() //disable for now
var apiToken, model, query, uid;
uid = req.params.uid;
apiToken = req.query.apiToken;
model = req.getModel();
query = model.query('users').withIdAndToken(uid, apiToken);
return query.fetch(function(err, result) {
var formattedIcal, ical, tasks, tasksWithDates;
if (err) {
return res.send(500, err);
}
tasks = result.get('tasks');
/* tasks = result[0].tasks*/
tasksWithDates = _.filter(tasks, function(task) {
return !!task.date;
});
if (_.isEmpty(tasksWithDates)) {
return res.send(500, "No events found");
}
ical = new icalendar.iCalendar();
ical.addProperty('NAME', 'HabitRPG');
_.each(tasksWithDates, function(task) {
var d, event;
event = new icalendar.VEvent(task.id);
event.setSummary(task.text);
d = new Date(task.date);
d.date_only = true;
event.setDate(d);
ical.addComponent(event);
return true;
});
res.type('text/calendar');
formattedIcal = ical.toString().replace(/DTSTART\:/g, 'DTSTART;VALUE=DATE:');
return res.send(200, formattedIcal);
});
});
module.exports = router;
+3 -6
View File
@@ -43,19 +43,16 @@ findTask = function(req, res) {
Export it also so we can call it from deprecated.coffee
*/
api.score = function(req, res, next) {
var task = findTask(req,res);
if (!task) return res.json(404, {err: "No task found."});
var id = req.params.id,
direction = req.params.direction,
user = res.locals.user,
task;
// Send error responses for improper API call
if (!id) return res.json(500, {err: ':id required'});
if (!id) return res.json(400, {err: ':id required'});
if (direction !== 'up' && direction !== 'down') {
if (direction == 'unlink') return next();
return res.json(500, {err: ":direction must be 'up' or 'down'"});
return res.json(400, {err: ":direction must be 'up' or 'down'"});
}
// If exists already, score it
if (task = user.tasks[id]) {
@@ -181,7 +178,7 @@ api.update = function(req, res, next) {
if (acceptablePUTPaths[k])
user.fns.dotSet(k, v);
else
errors.push("path `" + k + "` was not saved, as it's a protected path. Make sure to send `PUT /api/v1/user` request bodies as `{'set.this.path':value}` instead of `{set:{this:{path:value}}}`");
errors.push("path `" + k + "` was not saved, as it's a protected path. See https://github.com/HabitRPG/habitrpg/blob/develop/API.md for PUT /api/v2/user.");
return true;
});
user.save(function(err) {