Advertisement
Halakul

Untitled

Jun 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class ResourceConsumption {
  2.  
  3.   //@ngInject
  4.   constructor($q, $resource, vpcDomainConsumptionAuth, TokenStore) {
  5.     this.$resource = $resource;
  6.     this.vpcDomainConsumptionAuth = vpcDomainConsumptionAuth;
  7.     this.TokenStore = TokenStore;
  8.  
  9.     this.defer = $q.defer();
  10.     this.promise = this.defer.promise;
  11.     this.resource = null;
  12.     this.activeToken = null;
  13.  
  14.     this._createResource();
  15.   }
  16.  
  17.   query(params, data) {
  18.     let $promise = this.promise
  19.       .then(() => this._setActiveToken(params.uuid))
  20.       .then(() => this.resource.query(params, data).$promise)
  21.       .catch(e => {
  22.         if(e.status !== 401) return;
  23.         return this._setActiveToken(params.uuid, true)
  24.           .then(() => this.resource.query(params, data).$promise);
  25.       });
  26.  
  27.     return {$promise};
  28.   }
  29.  
  30.   getResourceInfo(params) {
  31.     let $promise = this.promise
  32.       .then(() => this._setActiveToken(params.uuid))
  33.       .then(() => this.resource.getResourceInfo(params).$promise)
  34.       .catch(e => {
  35.         if(e.status !== 401) return;
  36.         return this._setActiveToken(params.uuid, true)
  37.           .then(() => this.resource.getResourceInfo(params).$promise)
  38.       })
  39.     ;
  40.  
  41.     return {$promise};
  42.   }
  43.  
  44.   _createResource() {
  45.     return this.vpcDomainConsumptionAuth.getInfo().then(({url}) => {
  46.       const URL = url + '/v1/resource/sel_project/:uuid/metric/:type/measures';
  47.       const PARAMS = {
  48.         uuid: '@uuid',
  49.         type: '@type'
  50.       };
  51.       const ACTIONS = {
  52.         query: {
  53.           method: 'GET',
  54.           isArray: true,
  55.           headers: {
  56.             'X-Auth-Token': this._getActiveToken.bind(this)
  57.           }
  58.         },
  59.         getResourceInfo: {
  60.           method: 'GET',
  61.           url: url + '/v1/resource/sel_project/:uuid',
  62.           headers: {
  63.             'X-Auth-Token': this._getActiveToken.bind(this)
  64.           }
  65.         }
  66.       };
  67.  
  68.       this.resource = this.$resource(URL, PARAMS, ACTIONS);
  69.  
  70.       this.defer.resolve(this.$resource);
  71.  
  72.       return this.promise;
  73.     });
  74.   }
  75.  
  76.   _getActiveToken() {
  77.     if (!this.activeToken) {
  78.       throw new Error('Active token doesn\'t set');
  79.     }
  80.     return this.activeToken;
  81.   }
  82.  
  83.   _setActiveToken(projectId, renew = false) {
  84.     return this.TokenStore
  85.       .requestResellToken({projectId}, renew, false)
  86.       .then(token => this.activeToken = token);
  87.   }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement