diff --git a/package.json b/package.json index 89acfa5..57cc4ca 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "mocha": "^2.2.5" }, "dependencies": { - "mg-stackable-fetcher": "0.4.*", + "stackable-fetcher": "^0.3.0", "stackable-fetcher-aws-signer-v4": "0.0.1" } } diff --git a/src/client.js b/src/client.js index e0a371e..a7c38ac 100644 --- a/src/client.js +++ b/src/client.js @@ -25,6 +25,8 @@ export default class Client { this._fetcher = fetcher; this.region = region; this.secretAccessKey = secretAccessKey; + + this._resources = {}; } /** @@ -59,7 +61,12 @@ export default class Client { return this.getFetcher().post( `${this._getBaseUrl()}/restapis/${restapiId}/resources/${parentId}`, { pathPart: pathPart } - ).then(body => new Resource(body)); + ).then((body) => { + let resource = new Resource(body); + this._resources[resource.source.path] = resource; + + return resource; + }); } /** @@ -85,7 +92,15 @@ export default class Client { return this.getFetcher().post( `${this._getBaseUrl()}/restapis`, { name: name } - ).then(body => new Restapi(body)); + ).then((body) => { + let api = new Restapi(body); + this._resources[api.source.id] = {}; + + console.log('createRestapi - api - ', api); + // @todo - add root resource to resources map obj + + return api; + }); } /** @@ -110,26 +125,32 @@ export default class Client { } /** - * @todo Use Array.prototype.find polyfill instead of forEach * @param {String} path - * @param {String} restapiId * @return {Promise} */ - findResourceByPath({ path, restapiId }) { - return this.listResources({ - restapiId: restapiId, - qsParams: { - limit: 500, + findResourceByPath({path, restapiId}) { + if (path !== '/') { + let resource = null; + + if (this._resources.hasOwnProperty(restapiId) && this._resources[restapiId].hasOwnProperty(path)) { + resource = this._resources[restapiId][path]; } - }).then((resources) => { - let matchedResource; - resources.forEach((resource) => { - if (resource.source.path === path) { - matchedResource = resource; - } + + Promise.resolve(resource); + } else { + // @temp + return this.listResources({ + restapiId: restapiId, + }).then((resources) => { + let matchedResource; + resources.forEach((resource) => { + if (resource.source.path === path) { + matchedResource = resource; + } + }); + return matchedResource; }); - return matchedResource; - }); + } } /** @@ -186,10 +207,9 @@ export default class Client { * @param {String} restapiId * @return {Promise} */ - listResources({ restapiId, qsParams = {} }) { + listResources({ restapiId }) { return this.getFetcher().get( - `${this._getBaseUrl()}/restapis/${restapiId}/resources`, - qsParams + `${this._getBaseUrl()}/restapis/${restapiId}/resources` ).then(body => body.item.map(source => new Resource(source))); } @@ -375,7 +395,7 @@ export default class Client { }); }); } else { - return Promise.resolve(); + return Promise.resolve(this._resources[restapiId]); } }