import { Injectable } from "@angular/core" ; import { HttpClient , HttpErrorResponse , HttpHeaders , HttpParams } from "@angular/common/http" ; import { Response , ResponseContentType , Jsonp } from '@angular/http' ; import { Router } from "@angular/router" ; import { Observable } from "rxjs/Observable" ; import { ISubscription } from "rxjs/Subscription" ; import "rxjs/add/operator/catch" ; import "rxjs/add/operator/do" ; import { JobAnswer , JobBody , Format , Type , JobStatus , AccessToken } from "./revit-converter.models" ; @ Injectable ( ) export class RevitConverterService { errorMessage : string ; private _baseUrl = "https://developer.api.autodesk.com/modelderivative/v2/designdata/" ; constructor ( private _http , private router ) { } public setDelay ( delay ) { setTimeout ( function ( ) { console . log ( delay ) ; } , delay ) ; } public PostJobRequest ( access_token , objectId ) { let body = '{"input": {"urn": "' + btoa ( objectId ) . replace ( /=+$/ , "" ) + '"},"output": {"formats": [{"type": "ifc"}]}}' ; return this . _http . post < JobAnswer > ( this . _baseUrl + "job" , body , { headers : new HttpHeaders ( ) . set ( "Authorization" , "Bearer " + access_token ) . set ( "Content-Type" , "application/json" ) } ) . do ( data => console . log ( "All PostJobRequest: " + JSON . stringify ( data ) ) ) . catch ( this . handleError ) ; } public GetJobStatus ( access_token , urn ) { return this . _http . get < JobStatus > ( this . _baseUrl + urn + "/manifest" , { headers : new HttpHeaders ( ) . set ( "Authorization" , "Bearer " + access_token ) } ) . do ( data => console . log ( "All GetJobStatus: " + JSON . stringify ( data ) ) ) . catch ( this . handleError ) ; } public GetDerivative ( access_token , urn , derivative ) { return this . _http . get < any > ( this . _baseUrl + urn + "/manifest/" + derivative , { headers : new HttpHeaders ( ) . set ( "Authorization" , "Bearer " + access_token ) , responseType : 'blob' as 'json' } ) . catch ( this . handleError ) ; } public downloadFile ( data , fileName ) { let blob = new Blob ( [ data ] , { type : "text/csv" } ) ; let link = document . createElement ( 'a' ) ; link . href = window . URL . createObjectURL ( blob ) ; link . download = fileName ; link . click ( ) ; } public GetAccessToken ( ) { return this . _http . get < AccessToken > ( "https://forgefunction.azurewebsites.net/api/ForgeToken" , { } ) . do ( data => console . log ( "You have a token!" ) ) . catch ( this . handleError ) ; } /**
     * ConvertConversionProgress
     */ public ConvertConversionProgress ( progress ) { let value = Number ( progress . match ( /^\d*/ ) ) as number ; return value ; } DeleteBucket ( access_token , bucketId ) { return ( this . _http . delete < any > ( "https://developer.api.autodesk.com/oss/v2/buckets/" + bucketId , { headers : new HttpHeaders ( ) . set ( "Authorization" , "Bearer " + access_token ) } ) . catch ( this . handleError ) ) ; } private handleError ( err ) { let errorMessage = "" ; if ( err . error instanceof Error ) { errorMessage = `An error occurred: ${ err . error . message }` ; } else { errorMessage = `Server returned code: ${ err . status }, error message is: ${ err . message }` ; } console . log ( errorMessage ) ; return Observable . throw ( errorMessage ) ; } }