import { Http , Headers , Response } from '@angular/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/delay' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/retryWhen' ; import 'rxjs/add/operator/scan' ; import 'rxjs/add/observable/of' ; import { FunctionsResponse } from './../models/functions-response' ; import { FunctionTemplate } from '../models/function-template' ; import { DesignerSchema } from '../models/designer-schema' ; import { UserService } from './user.service' ; import { Constants } from '../models/constants' ; import { Cache } from '../decorators/cache.decorator' ; import { GlobalStateService } from './global-state.service' ; import { UIResource , ITryAppServiceTemplate } from '../models/ui-resource' ; import { Cookie } from 'ng2-cookies/ng2-cookies' ; import { Site } from '../models/arm/site' ; import { ArmObj } from '../models/arm/arm-obj' ; import { FunctionAppContext } from 'app/shared/function-app-context' ; @ Injectable ( ) export class TryFunctionsService { private token : string ; public isEasyAuthEnabled : boolean ; public selectedFunction : string ; public selectedLanguage : string ; public selectedProvider : string ; public selectedFunctionName : string ; public isMultiKeySupported = true ; private tryAppServiceUrl = 'https://tryappservice.azure.com' ; public functionContainer : ArmObj < Site > ; public functionAppContext : FunctionAppContext ; constructor ( private _http , private _userService , private _globalStateService ) { if ( ! _globalStateService . showTryView ) { this . _userService . getStartupInfo ( ) . subscribe ( info => { this . token = info . token ; } ) ; } if ( Cookie . get ( 'TryAppServiceToken' ) ) { this . _globalStateService . TryAppServiceToken = Cookie . get ( 'TryAppServiceToken' ) ; const templateId = Cookie . get ( 'templateId' ) ; this . selectedFunction = templateId . split ( '-' ) [ 0 ] . trim ( ) ; this . selectedLanguage = templateId . split ( '-' ) [ 1 ] . trim ( ) ; this . selectedProvider = Cookie . get ( 'provider' ) ; this . selectedFunctionName = Cookie . get ( 'functionName' ) ; } } @ Cache ( ) getTemplates ( ) { try { if ( localStorage . getItem ( 'dev-templates' ) ) { const devTemplate = JSON . parse ( localStorage . getItem ( 'dev-templates' ) ) ; return Observable . of ( devTemplate ) ; } } catch ( e ) { console . error ( e ) ; } let url = `${ Constants . serviceHost }api/templates?runtime='latest'` ; return this . _http . get ( url , { headers : this . getPortalHeaders ( ) } ) . retryWhen ( this . retryAntares ) . map ( r => { const object = r . json ( ) ; return < FunctionTemplate [ ] > object ; } ) ; } @ Cache ( ) getDesignerSchema ( ) { return this . _http . get ( 'mocks/function-json-schema.json' ) . retryWhen ( this . retryAntares ) . map ( r => < DesignerSchema > r . json ( ) ) ; } getTrialResource ( provider ? ) { const url = this . tryAppServiceUrl + '/api/resource?appServiceName=Function' + ( provider ? '&provider=' + provider : '' ) ; return this . _http . get ( url , { headers : this . getTryAppServiceHeaders ( ) } ) . retryWhen ( this . retryGetTrialResource ) . map ( r => < UIResource > r . json ( ) ) ; } createTrialResource ( selectedTemplate , provider , functionName ) { const url = this . tryAppServiceUrl + '/api/resource?appServiceName=Function' + ( provider ? '&provider=' + provider : '' ) + '&templateId=' + encodeURIComponent ( selectedTemplate . id ) + '&functionName=' + encodeURIComponent ( functionName ) + '&trial=true' ; const template = < ITryAppServiceTemplate > { name : selectedTemplate . id , appService : 'Function' , language : selectedTemplate . metadata . language , githubRepo : '' , } ; return this . _http . post ( url , JSON . stringify ( template ) , { headers : this . getTryAppServiceHeaders ( ) } ) . retryWhen ( this . retryCreateTrialResource ) . map ( r => < UIResource > r . json ( ) ) ; } private getPortalHeaders ( contentType ? ) { contentType = contentType || 'application/json' ; const headers = new Headers ( ) ; headers . append ( 'Content-Type' , contentType ) ; headers . append ( 'Accept' , 'application/json,*/*' ) ; if ( this . token ) { headers . append ( 'client-token' , this . token ) ; headers . append ( 'portal-token' , this . token ) ; } return headers ; } private getTryAppServiceHeaders ( contentType ? ) { contentType = contentType || 'application/json' ; const headers = new Headers ( ) ; headers . append ( 'Content-Type' , contentType ) ; headers . append ( 'Accept' , 'application/json,*/*' ) ; if ( this . _globalStateService . TryAppServiceToken ) { headers . append ( 'Authorization' , `Bearer ${ this . _globalStateService . TryAppServiceToken }` ) ; } else { headers . append ( 'ms-x-user-agent' , 'Functions/' ) ; } return headers ; } private retryAntares ( error ) { return error . scan ( ( errorCount , err ) => { if ( err . isHandled || err . status < 500 || errorCount >= 10 ) { throw err ; } else { return errorCount + 1 ; } } , 0 ) . delay ( 1000 ) ; } private retryCreateTrialResource ( error ) { return error . scan ( ( errorCount , err ) => { if ( err . status === 400 || err . status === 403 || errorCount >= 10 ) { throw err ; } else { return errorCount + 1 ; } } , 0 ) . delay ( 1000 ) ; } private retryGetTrialResource ( error ) { return error . scan ( ( errorCount , err ) => { if ( err . status === 403 || errorCount >= 10 ) { throw err ; } else { return errorCount + 1 ; } } , 0 ) . delay ( 1000 ) ; } }