cloudapi-gql
Advanced tools
Comparing version 4.3.1 to 4.4.0
@@ -1,2 +0,1 @@ | ||
[ | ||
@@ -3,0 +2,0 @@ "absolved", |
'use strict'; | ||
const Hasha = require('hasha'); | ||
const Assert = require('assert'); | ||
@@ -17,2 +18,4 @@ const Crypto = require('crypto'); | ||
const cache = {}; | ||
class CloudApi { | ||
@@ -49,3 +52,38 @@ constructor ({ token, url }) { | ||
async fetch (path = '/', options = {}, request) { | ||
_getCache(method = '', path, options) { | ||
if (method.toLowerCase() !== 'get') { | ||
return; | ||
} | ||
const ref = Hasha(JSON.stringify({ method, path, options })); | ||
const { val, when } = cache[ref] || {}; | ||
const now = new Date().getTime(); | ||
if (!when) { | ||
return; | ||
} | ||
// cache is no logner fresh | ||
if (now - when > 9000) { | ||
delete cache[ref]; | ||
return val; | ||
} | ||
return val; | ||
} | ||
_setCache(method = '', path, options, payload) { | ||
if (method.toLowerCase() !== 'get') { | ||
return; | ||
} | ||
const ref = Hasha(JSON.stringify({ method, path, options })); | ||
cache[ref] = { | ||
when: new Date().getTime(), | ||
val: payload | ||
}; | ||
} | ||
async fetch(path = '/', options = {}, request) { | ||
const wreckOptions = { | ||
@@ -62,5 +100,11 @@ json: true, | ||
const method = options.method && options.method.toLowerCase() || 'get'; | ||
const cached = this._getCache(method, path, wreckOptions); | ||
if (cached) { | ||
return cached; | ||
} | ||
try { | ||
const { payload } = await this._wreck[method](path, wreckOptions); | ||
this._setCache(method, path, options, payload); | ||
return payload; | ||
@@ -67,0 +111,0 @@ } catch (ex) { |
@@ -179,2 +179,7 @@ 'use strict'; | ||
const machinesList = await CloudApi('/machines', { query }, request); | ||
if (!queryAST) { | ||
return machinesList; | ||
} | ||
const field = ForceArray(queryAST.fieldNodes) | ||
@@ -630,3 +635,18 @@ .filter(({ name }) => { return name.value === 'machines'; }) | ||
ImageFile: { | ||
compression: ({ compression }) => { return (compression ? compression.toUpperCase() : compression); } | ||
compression: ({ compression }) => { | ||
return (compression ? compression.toUpperCase() : compression); | ||
} | ||
}, | ||
Network: { | ||
machines: async ({ fabric, id }, args, request) => { | ||
if (!fabric) { | ||
return []; | ||
} | ||
const machines = await internals.resolvers.Query.machines({}, {}, request); | ||
return ForceArray(machines).filter(({ networks }) => { | ||
return ForceArray(networks).some(network => network === id); | ||
}); | ||
} | ||
} | ||
@@ -633,0 +653,0 @@ }); |
{ | ||
"name": "cloudapi-gql", | ||
"version": "4.3.1", | ||
"version": "4.4.0", | ||
"license": "MPL-2.0", | ||
@@ -5,0 +5,0 @@ "repository": "github:yldio/joyent-portal", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
217152
2310