Skip to content

Commit

Permalink
store created resources inside client instance instead of fetching th…
Browse files Browse the repository at this point in the history
…em each time to find a resource by path
  • Loading branch information
mgoria committed Oct 3, 2015
1 parent bb470eb commit de90fcc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
62 changes: 41 additions & 21 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default class Client {
this._fetcher = fetcher;
this.region = region;
this.secretAccessKey = secretAccessKey;

this._resources = {};
}

/**
Expand Down Expand Up @@ -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;
});
}

/**
Expand All @@ -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;
});
}

/**
Expand All @@ -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;
});
}
}

/**
Expand Down Expand Up @@ -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)));
}

Expand Down Expand Up @@ -375,7 +395,7 @@ export default class Client {
});
});
} else {
return Promise.resolve();
return Promise.resolve(this._resources[restapiId]);
}
}

Expand Down

0 comments on commit de90fcc

Please sign in to comment.