1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 10:19:30 +02:00

Add API endpoints for loading devices.

This commit is contained in:
Simo Kinnunen 2014-01-31 04:27:04 +09:00
parent 52ae7765c8
commit c119e2c3d2
2 changed files with 38 additions and 0 deletions

View file

@ -69,3 +69,11 @@ module.exports.saveDeviceIdentity = function(serial, identity) {
, display: identity.display , display: identity.display
})) }))
} }
module.exports.loadDevices = function() {
return db.run(r.table('devices'))
}
module.exports.loadDevice = function(serial) {
return db.run(r.table('devices').get(serial))
}

View file

@ -92,6 +92,36 @@ module.exports = function(options) {
res.render('index') res.render('index')
}) })
app.get('/api/v1/devices', function(req, res) {
dbapi.loadDevices()
.then(function(cursor) {
return Promise.promisify(cursor.toArray, cursor)()
.then(function(list) {
res.json(list)
})
})
.catch(function(err) {
log.error('Failed to load device list: ', err.stack)
res.send(500)
})
})
app.get('/api/v1/devices/:serial', function(req, res) {
dbapi.loadDevice(req.params.serial)
.then(function(device) {
if (device) {
res.json(device)
}
else {
res.send(404)
}
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
res.send(500)
})
})
io.set('authorization', (function() { io.set('authorization', (function() {
var parse = Promise.promisify(express.cookieParser(options.secret)) var parse = Promise.promisify(express.cookieParser(options.secret))
return function(handshake, accept) { return function(handshake, accept) {