From c8df0b2f9e7ad550c213342883efa2825466628c Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Wed, 11 Mar 2015 17:07:18 +0000 Subject: [PATCH] Make use of the new cache feature of the request backend for pinejs-client 1.2.0 to make use of an ETags based cache for requests to the api. --- package.json | 2 +- src/application.coffee | 9 +++++---- src/bootstrap.coffee | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 614e505d..e103b526 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "pubnub": "~3.6.4", "randomstring": "~1.0.3", "request": "^2.51.0", - "pinejs-client": "^1.0.0", + "pinejs-client": "^1.2.0", "sqlite3": "~3.0.4", "tty.js": "~0.2.13", "typed-error": "~0.1.0" diff --git a/src/application.coffee b/src/application.coffee index a6ea29cb..24f356da 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -13,7 +13,8 @@ logger = require './lib/logger' {docker} = dockerUtils PLATFORM_ENDPOINT = url.resolve(config.apiEndpoint, '/ewa/') -resinAPI = new PlatformAPI(PLATFORM_ENDPOINT) +resinApi = new PlatformAPI(PLATFORM_ENDPOINT) +cachedResinApi = resinApi.clone({}, cache: {}) knex('config').select('value').where(key: 'uuid').then ([ uuid ]) -> logger.init( @@ -173,7 +174,7 @@ exports.update = update = -> .then ([ [ apiKey ], [ uuid ], apps ]) -> apiKey = apiKey.value uuid = uuid.value - resinAPI.get( + cachedResinApi.get( resource: 'application' options: expand: 'environment_variable' @@ -289,7 +290,7 @@ getDeviceID = do -> knex('config').select('value').where(key: 'uuid') ]) .spread ([{value: apiKey}], [{value: uuid}]) -> - resinAPI.get( + resinApi.get( resource: 'device' options: select: 'id' @@ -326,7 +327,7 @@ exports.updateDeviceState = updateDeviceState = do -> stateDiff = getStateDiff() if _.size(stateDiff) is 0 return - resinAPI.patch + resinApi.patch resource: 'device' id: deviceID body: stateDiff diff --git a/src/bootstrap.coffee b/src/bootstrap.coffee index 97f91b9f..509b81e6 100644 --- a/src/bootstrap.coffee +++ b/src/bootstrap.coffee @@ -9,7 +9,7 @@ PlatformAPI = require 'pinejs-client' fs = Promise.promisifyAll(require('fs')) PLATFORM_ENDPOINT = url.resolve(config.apiEndpoint, '/ewa/') -resinAPI = new PlatformAPI(PLATFORM_ENDPOINT) +resinApi = new PlatformAPI(PLATFORM_ENDPOINT) registerDevice = (apiKey, userId, applicationId, deviceType, uuid) -> # I'd be nice if the UUID matched the output of a SHA-256 function, but although the length limit of the CN @@ -19,7 +19,7 @@ registerDevice = (apiKey, userId, applicationId, deviceType, uuid) -> if not uuid? uuid = crypto.pseudoRandomBytes(31).toString('hex') - resinAPI.post( + resinApi.post( resource: 'device' body: user: userId