Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default class ResourceConsumption {
- //@ngInject
- constructor($q, $resource, vpcDomainConsumptionAuth, TokenStore) {
- this.$resource = $resource;
- this.vpcDomainConsumptionAuth = vpcDomainConsumptionAuth;
- this.TokenStore = TokenStore;
- this.defer = $q.defer();
- this.promise = this.defer.promise;
- this.resource = null;
- this.activeToken = null;
- this._createResource();
- }
- query(params, data) {
- let $promise = this.promise
- .then(() => this._setActiveToken(params.uuid))
- .then(() => this.resource.query(params, data).$promise)
- .catch(e => {
- if(e.status !== 401) return;
- return this._setActiveToken(params.uuid, true)
- .then(() => this.resource.query(params, data).$promise);
- });
- return {$promise};
- }
- getResourceInfo(params) {
- let $promise = this.promise
- .then(() => this._setActiveToken(params.uuid))
- .then(() => this.resource.getResourceInfo(params).$promise)
- .catch(e => {
- if(e.status !== 401) return;
- return this._setActiveToken(params.uuid, true)
- .then(() => this.resource.getResourceInfo(params).$promise)
- })
- ;
- return {$promise};
- }
- _createResource() {
- return this.vpcDomainConsumptionAuth.getInfo().then(({url}) => {
- const URL = url + '/v1/resource/sel_project/:uuid/metric/:type/measures';
- const PARAMS = {
- uuid: '@uuid',
- type: '@type'
- };
- const ACTIONS = {
- query: {
- method: 'GET',
- isArray: true,
- headers: {
- 'X-Auth-Token': this._getActiveToken.bind(this)
- }
- },
- getResourceInfo: {
- method: 'GET',
- url: url + '/v1/resource/sel_project/:uuid',
- headers: {
- 'X-Auth-Token': this._getActiveToken.bind(this)
- }
- }
- };
- this.resource = this.$resource(URL, PARAMS, ACTIONS);
- this.defer.resolve(this.$resource);
- return this.promise;
- });
- }
- _getActiveToken() {
- if (!this.activeToken) {
- throw new Error('Active token doesn\'t set');
- }
- return this.activeToken;
- }
- _setActiveToken(projectId, renew = false) {
- return this.TokenStore
- .requestResellToken({projectId}, renew, false)
- .then(token => this.activeToken = token);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement