import { Injectable } from '@angular/core' ; import { HttpClient , HttpRequest , HttpEventType , HttpResponse } from '@angular/common/http' ; import { timeoutWith } from 'rxjs/operators' ; import 'rxjs/add/observable/throw' ; import { Observable } from "rxjs/Observable" ; interface ItemsResponse { Data : string [ ] ; } @ Injectable ( ) export class SurveyProvider { private readonly ownerId : string = encodeURI ( "ownerId" ) ; private readonly accessKey : string = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; constructor ( protected http ) { } testProgressRequest ( ) { const req = new HttpRequest ( 'GET' , 'https://jsonplaceholder.typicode.com/photos' , { reportProgress : true , } ) ; this . http . request ( req ) . subscribe ( event => { if ( event . type === HttpEventType . UploadProgress ) { const percentDone = Math . round ( 100 * event . loaded / event . total ) ; console . log ( `File is ${ percentDone }% uploaded.` ) ; } else if ( event instanceof HttpResponse ) { console . log ( 'File is completely uploaded!' ) ; } if ( event . type === HttpEventType . DownloadProgress ) { const kbLoaded = Math . round ( event . loaded / 1024 ) ; console . log ( `Download in progress! ${ kbLoaded }Kb loaded` ) ; } if ( event . type === HttpEventType . Response ) { console . log ( 'Data received' , event . body ) ; } } ) ; } getActiveSurveys ( ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/getActive?ownerId=' + this . ownerId + '&accessKey=' + this . accessKey ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to get surveys.' ) ) ) ) ; } getArchiveSurveys ( ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/getArchive?ownerId=' + this . ownerId + '&accessKey=' + this . accessKey ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to get surveys.' ) ) ) ) ; } getSurveyResults ( idSurvey ) { return this . http . get < ItemsResponse > ( 'https://dxsurvey.com/api/MySurveys/getSurveyResults/' + idSurvey + '?accessKey=' + this . accessKey ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to get survey results.' ) ) ) ) ; } deleteSurvey ( idSurvey ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/delete/' + idSurvey + '?accessKey=' + this . accessKey ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to delete survey.' ) ) ) ) ; } changeSurveyName ( idSurvey , newName ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/changeName/' + idSurvey + '?accessKey=' + this . accessKey + '&name=' + encodeURI ( newName ) ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to change survey name.' ) ) ) ) ; } restoreSurvey ( idSurvey ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/restore/' + idSurvey + '?accessKey=' + this . accessKey ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to restore survey.' ) ) ) ) ; } archiveSurvey ( idSurvey ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/archive/' + idSurvey + '?accessKey=' + this . accessKey ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to archive survey.' ) ) ) ) ; } createSurvey ( name ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/create?ownerId=' + this . ownerId + '&accessKey=' + this . accessKey + '&name=' + encodeURI ( name ) ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to create survey.' ) ) ) ) ; } deleteSurveyResult ( postId , instanceId ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/deleteResult?accessKey=' + this . accessKey + '&postId=' + postId + '&instanceId=' + instanceId ) . pipe ( timeoutWith ( 10000 , Observable . throw ( new Error ( 'Failed to create survey.' ) ) ) ) ; } makeSurveyResultsPublic ( idSurvey , makeResultPublic ) { return this . http . get ( 'https://dxsurvey.com/api/MySurveys/makeResultPublic/' + idSurvey + '?accessKey=' + this . accessKey + '&makeResultPublic=' + makeResultPublic ) . pipe ( timeoutWith ( 5000 , Observable . throw ( new Error ( 'Failed to make survey results public.' ) ) ) ) ; } }