import * as request from 'request-promise' ; import { IReturnEntity } from './IReturnEntity' ; import { IEntity } from './IEntity' ; class DataService { constructor ( private table_name , private azure_storage_uri , private azure_storage_sas ) { } returns { Promise < string > } memberof public async createTableIfNotExists ( ) { try { const tableExists = await this . checkIfTableExists ( ) ; if ( tableExists ) { return this . table_name ; } else { const URI = `${ this . azure_storage_uri }/tables?${ this . azure_storage_sas }` ; let options = { method : 'POST' , uri : URI , headers : { 'Content-Type' : 'application/json;odata=nometadata' } , body : { TableName : this . table_name } , json : true , resolveWithFullResponse : true } ; const results = await request ( options ) ; const tablesResult = JSON . parse ( results ) ; return tablesResult . table_name ; } } catch ( error ) { throw new Error ( error ) ; } } returns { Promise < boolean > } memberof public async checkIfTableExists ( ) { try { const URI = `${ this . azure_storage_uri }/tables?$filter=TableName eq '${ this . table_name }'&${ this . azure_storage_sas }` ; let options = { method : 'GET' , uri : URI , headers : { 'Content-Type' : 'application/json' , Accept : 'application/json' } } ; const results = await request ( options ) ; const tablesResult = JSON . parse ( results ) ; return ( tablesResult . value && tablesResult . value . length ) > 0 ? true : false ; } catch ( error ) { throw new Error ( error ) ; } } param { IEntity } entity returns { Promise < IReturnEntity > } memberof public async insertEntity ( entity ) { try { await this . createTableIfNotExists ( ) ; const URI = `${ this . azure_storage_uri }/${ this . table_name }?${ this . azure_storage_sas }` ; let options = { method : 'POST' , uri : URI , headers : { 'Content-Type' : 'application/json' , Accept : 'application/json' } , body : { PartitionKey : entity . PartitionKey , RowKey : entity . RowKey , SubscriptionId : entity . SubscriptionId , ExpirationDateTime : entity . ExpirationDateTime } , json : true , resolveWithFullResponse : true } ; const results = await request ( options ) ; const entityAddedResult = JSON . parse ( results ) ; return entityAddedResult ; } catch ( error ) { throw new Error ( error ) ; } } param { IEntity } entity returns { Promise < void > } memberof public async insertOrUpdateEntity ( entity ) { try { await this . createTableIfNotExists ( ) ; const URI = `${ this . azure_storage_uri }/${ this . table_name }(PartitionKey='${ entity . PartitionKey }', RowKey='${ entity . RowKey }')?${ this . azure_storage_sas }` ; let options = { method : 'PUT' , uri : URI , headers : { 'Content-Type' : 'Application/json' , Accept : 'Aplication/json' } , body : { PartitionKey : entity . PartitionKey , RowKey : entity . RowKey , SubscriptionId : entity . SubscriptionId , ExpirationDateTime : entity . ExpirationDateTime } , json : true , resolveWithFullResponse : true } ; const results = await request ( options ) ; } catch ( error ) { throw new Error ( error ) ; } } param { IEntity } entity returns { Promise < void > } memberof public async updateEntity ( entity ) { try { await this . createTableIfNotExists ( ) ; const URI = `${ this . azure_storage_uri }/${ this . table_name }(PartitionKey='${ entity . PartitionKey }', RowKey='${ entity . RowKey }')?${ this . azure_storage_sas }` ; let options = { method : 'PUT' , uri : URI , headers : { 'Content-Type' : 'Application/json' , Accept : 'Application/json' , 'if-match' : '*' } , body : { PartitionKey : entity . PartitionKey , RowKey : entity . RowKey , SubscriptionId : entity . SubscriptionId , ExpirationDateTime : entity . ExpirationDateTime } , json : true , resolveWithFullResponse : true } ; const results = await request ( options ) ; } catch ( error ) { throw new Error ( error ) ; } } param { IEntity } entity returns { Promise < void > } memberof public async removeEntity ( entity ) { try { await this . createTableIfNotExists ( ) ; const URI = `${ this . azure_storage_uri }/${ this . table_name }(PartitionKey='${ entity . PartitionKey }', RowKey='${ entity . RowKey }')?${ this . azure_storage_sas }` ; let options = { method : 'DELETE' , uri : URI , headers : { 'Content-Type' : 'application/json' , Accept : 'application/json' } } ; const results = await request ( options ) ; } catch ( error ) { throw new Error ( error ) ; } } param { string } partitionKey param { string } rowKey returns { Promise < IReturnEntity > } memberof public async getEntity ( partitionKey , rowKey ) { try { await this . createTableIfNotExists ( ) ; const URI = `${ this . azure_storage_uri }/${ this . table_name }(PartitionKey='${ partitionKey }', RowKey='${ rowKey }')?${ this . azure_storage_sas }` ; console . log ( 'URI' , URI ) ; let options = { method : 'GET' , uri : URI , headers : { 'Content-Type' : 'application/json' , Accept : 'application/json' } } ; var results = await request ( options ) ; var entityResult = JSON . parse ( results ) ; return entityResult ; } catch ( error ) { if ( error . statusCode == 404 ) { return entityResult = undefined ; } throw new Error ( error ) ; } } param { string } query returns { Promise < IReturnEntity [ ] > } memberof public async listEntities ( query ? ) { try { let _query = '' ; await this . createTableIfNotExists ( ) ; if ( query ) { _query = `&$filter=${ query }` ; } const URI = `${ this . azure_storage_uri }/${ this . table_name }()?${ this . azure_storage_sas }${ _query }` ; let options = { method : 'GET' , uri : URI , headers : { 'Content-Type' : 'application/json;odata=nometadata' , Accept : 'application/json' } } ; const results = JSON . parse ( await request ( options ) ) ; const entitiesListResults = results . value ; return entitiesListResults ; } catch ( error ) { throw new Error ( error ) ; } } } export default DataService ;