From d6f37681ce8afbe929d84ddacd3da711b3d8ee44 Mon Sep 17 00:00:00 2001 From: Vishal Banthia Date: Wed, 2 Dec 2015 22:56:35 +0900 Subject: [PATCH] move devices endpoint from app unit to api unit --- lib/units/api/config/default.yaml | 1 + lib/units/api/controllers/device.js | 61 +++++++++++++++++++ lib/units/api/swagger/api_v1.yaml | 52 +++++++++++++++- lib/units/app/index.js | 47 -------------- .../components/stf/device/device-service.js | 4 +- 5 files changed, 115 insertions(+), 50 deletions(-) create mode 100644 lib/units/api/controllers/device.js diff --git a/lib/units/api/config/default.yaml b/lib/units/api/config/default.yaml index 0cd0513f..d289e535 100644 --- a/lib/units/api/config/default.yaml +++ b/lib/units/api/config/default.yaml @@ -26,6 +26,7 @@ swagger: swagger_controllers: - onError: json_error_handler - cors + - swagger_params_parser - swagger_security - _swagger_validate - express_compatibility diff --git a/lib/units/api/controllers/device.js b/lib/units/api/controllers/device.js new file mode 100644 index 00000000..b830fd4f --- /dev/null +++ b/lib/units/api/controllers/device.js @@ -0,0 +1,61 @@ +var Promise = require('bluebird') + +var dbapi = require('../../../db/api') +var logger = require('../../../util/logger') +var datautil = require('../../../util/datautil') + +var log = logger.createLogger('api:contoller:device') + +module.exports = { + getDevices: getDevices +, getDeviceBySerial: getDeviceBySerial +}; + +function getDevices(req, res) { + dbapi.loadDevices() + .then(function(cursor) { + return Promise.promisify(cursor.toArray, cursor)() + .then(function(list) { + list.forEach(function(device) { + datautil.normalize(device, req.user) + }) + + res.json({ + success: true + , devices: list + }) + }) + }) + .catch(function(err) { + log.error('Failed to load device list: ', err.stack) + res.json(500, { + success: false + }) + }) +} + +function getDeviceBySerial(req, res) { + var serial = req.swagger.params.serial.value + + dbapi.loadDevice(serial) + .then(function(device) { + if (device) { + datautil.normalize(device, req.user) + res.json({ + success: true + , device: device + }) + } + else { + res.json(404, { + success: false + }) + } + }) + .catch(function(err) { + log.error('Failed to load device "%s": ', req.params.serial, err.stack) + res.json(500, { + success: false + }) + }) +} diff --git a/lib/units/api/swagger/api_v1.yaml b/lib/units/api/swagger/api_v1.yaml index 0821c842..07cee6b4 100644 --- a/lib/units/api/swagger/api_v1.yaml +++ b/lib/units/api/swagger/api_v1.yaml @@ -9,7 +9,7 @@ info: contact: url: http://openstf.io/ email: contact@openstf.io -basePath: /api/v1/ +basePath: /api/v1 schemes: - http - https @@ -33,6 +33,42 @@ paths: description: Unexpected Error schema: $ref: "#/definitions/ErrorResponse" + /devices: + x-swagger-router-controller: device + get: + summary: Device List + description: List of all the STF devices including Disconnected and Offline + operationId: getDevices + responses: + "200": + description: List of Devices + schema: + $ref: "#/definitions/DeviceListResponse" + default: + description: Unexpected Error + schema: + $ref: "#/definitions/ErrorResponse" + /devices/{serial}: + x-swagger-router-controller: device + get: + summary: Device Information + description: Device Information + operationId: getDeviceBySerial + parameters: + - name: serial + in: path + description: Device Serial + required: true + type: string + responses: + "200": + description: Device Information + schema: + $ref: "#/definitions/DeviceResponse" + default: + description: Unexpected Error + schema: + $ref: "#/definitions/ErrorResponse" definitions: UserResponse: @@ -41,6 +77,20 @@ definitions: properties: user: type: object + DeviceListResponse: + required: + - devices + properties: + devices: + type: array + items: + type: object + DeviceResponse: + required: + - device + properties: + device: + type: object ErrorResponse: required: - message diff --git a/lib/units/app/index.js b/lib/units/app/index.js index 81cbcc24..ac95ebe3 100644 --- a/lib/units/app/index.js +++ b/lib/units/app/index.js @@ -146,53 +146,6 @@ module.exports = function(options) { }) }) - app.get('/app/api/v1/devices', function(req, res) { - dbapi.loadDevices() - .then(function(cursor) { - return Promise.promisify(cursor.toArray, cursor)() - .then(function(list) { - list.forEach(function(device) { - datautil.normalize(device, req.user) - }) - - res.json({ - success: true - , devices: list - }) - }) - }) - .catch(function(err) { - log.error('Failed to load device list: ', err.stack) - res.json(500, { - success: false - }) - }) - }) - - app.get('/app/api/v1/devices/:serial', function(req, res) { - dbapi.loadDevice(req.params.serial) - .then(function(device) { - if (device) { - datautil.normalize(device, req.user) - res.json({ - success: true - , device: device - }) - } - else { - res.json(404, { - success: false - }) - } - }) - .catch(function(err) { - log.error('Failed to load device "%s": ', req.params.serial, err.stack) - res.json(500, { - success: false - }) - }) - }) - app.get('/app/api/v1/accessTokens', function(req, res) { dbapi.loadAccessTokens(req.user.email) .then(function(cursor) { diff --git a/res/app/components/stf/device/device-service.js b/res/app/components/stf/device/device-service.js index 95aeb632..0ab5b6b2 100644 --- a/res/app/components/stf/device/device-service.js +++ b/res/app/components/stf/device/device-service.js @@ -165,7 +165,7 @@ module.exports = function DeviceServiceFactory($http, socket, EnhanceDeviceServi , digest: false }) - oboe('/app/api/v1/devices') + oboe('/api/v1/devices') .node('devices[*]', function(device) { tracker.add(device) }) @@ -190,7 +190,7 @@ module.exports = function DeviceServiceFactory($http, socket, EnhanceDeviceServi } deviceService.load = function(serial) { - return $http.get('/app/api/v1/devices/' + serial) + return $http.get('/api/v1/devices/' + serial) .then(function(response) { return response.data.device })