import { Injectable } from '@angular/core' ; import { Http , Response , ResponseContentType } from '@angular/http' ; import { Headers , RequestOptions } from '@angular/http' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/catch' ; import 'rxjs/add/operator/map' ; import { environment } from '../../../environments/environment' ; import ResponseInfo from "../technical-info/ResponseInfo" ; import 'rxjs/add/observable/of' ; import { AuthenticationService } from "./authentication.service" ; import { HttpClient } from '@angular/common/http' ; @ Injectable ( ) export class RequestService { serverUrl : string = environment . BACKEND_URL ; documentsServerUrl : string = environment . DOCUMENTS_URL ; private headers : Headers ; constructor ( private http , private authenticationService , private httpClient ) { this . headers = new Headers ( { 'Content-Type' : 'application/json' } ) ; this . headers . append ( 'Accept' , 'application/json, text/xml' ) ; this . headers . append ( 'X-Requested-With' , 'XMLHttpRequest' ) ; } sendRequest ( url , model ) { let options = new RequestOptions ( { headers : this . headers } ) ; this . addAuthHeader ( this . headers ) ; return this . http . post ( this . serverUrl + url , model , options ) . map ( ( response ) => { let responseInfo = new ResponseInfo ( ) ; responseInfo . status = response . status ; if ( response . text ( ) ) { responseInfo . text = response . json ( ) ; } else { responseInfo . text = "" ; } responseInfo . error = false ; return responseInfo ; } ) . catch ( ( error ) => Observable . of ( this . buildErrorAnswer ( error ) ) ) ; } sendRequestForXML ( url , model ) { let hXML = new Headers ( { 'Content-Type' : 'application/json' } ) ; hXML . append ( 'Accept' , 'application/xml' ) ; hXML . append ( 'X-Requested-With' , 'XMLHttpRequest' ) ; let options = new RequestOptions ( { headers : hXML } ) ; this . addAuthHeader ( this . headers ) ; return this . http . post ( this . serverUrl + url , model , options ) . map ( ( response ) => { let responseInfo = new ResponseInfo ( ) ; responseInfo . status = response . status ; responseInfo . format = "xml" ; if ( response . text ( ) ) { responseInfo . text = response . text ( ) ; } else { responseInfo . text = "" ; } responseInfo . error = false ; return responseInfo ; } ) . catch ( ( error ) => Observable . of ( this . buildErrorAnswerXML ( error ) ) ) ; } sendGet ( url , server ? ) { let options = new RequestOptions ( { headers : this . headers } ) ; this . addAuthHeader ( this . headers ) ; let urlEndpoint = server ? server + url : this . serverUrl + url ; return this . http . get ( urlEndpoint , options ) . map ( ( response ) => { let responseInfo = new ResponseInfo ( ) ; responseInfo . status = response . status ; if ( response . text ( ) ) { responseInfo . text = response . json ( ) ; } else { responseInfo . text = "" ; } responseInfo . error = false ; return responseInfo ; } ) . catch ( ( error ) => Observable . of ( this . buildErrorAnswer ( error ) ) ) ; } sendGetForm ( url , model ) { let options = new RequestOptions ( { headers : this . headers } ) ; this . addAuthHeader ( this . headers ) ; return this . http . get ( this . serverUrl + url + '?' + model . toString ( ) , options ) . map ( ( response ) => { let responseInfo = new ResponseInfo ( ) ; responseInfo . status = response . status ; if ( response . text ( ) ) { responseInfo . text = response . json ( ) ; } else { responseInfo . text = "" ; } responseInfo . error = false ; return responseInfo ; } ) . catch ( ( error ) => Observable . of ( this . buildErrorAnswer ( error ) ) ) ; } sendGetType ( url , type ) { let hOctet = new Headers ( { 'Content-Type' : 'application/json' } ) ; hOctet . append ( 'Accept' , 'application/octet-stream' ) ; hOctet . append ( 'X-Requested-With' , 'XMLHttpRequest' ) ; let options = new RequestOptions ( { headers : hOctet , responseType : type } ) ; this . addAuthHeader ( this . headers ) ; return this . http . get ( this . serverUrl + url , options ) . map ( ( response ) => { return response ; } ) . catch ( ( error ) => Observable . of ( this . buildErrorAnswer ( error ) ) ) ; } simpleGet ( url ) { console . log ( "simple get" ) ; let options = new RequestOptions ( { headers : this . headers } ) ; this . addAuthHeader ( this . headers ) ; return this . http . get ( this . serverUrl + url , options ) . map ( ( response ) => { return response ; } ) . catch ( ( error ) => Observable . of ( error ) ) ; } simpleGetJson ( url ) { let options = new RequestOptions ( { headers : this . headers } ) ; this . addAuthHeader ( this . headers ) ; return this . http . get ( this . serverUrl + url , options ) . map ( ( response ) => { return response . json ( ) ; } , ( error ) => { error . json ( ) ; } ) ; } getText ( url ) { console . log ( 'this.getText:' , url ) ; const options = new RequestOptions ( { headers : this . headers } ) ; return this . http . get ( this . documentsServerUrl + '/' + url , options ) . map ( ( response ) => { return response . text ( ) ; } ) . catch ( ( error ) => Observable . of ( this . buildErrorAnswer ( error ) ) ) ; } getGitText ( url ) { const options = new RequestOptions ( { headers : this . headers } ) ; return this . http . get ( this . documentsServerUrl + '/' + url , options ) . map ( ( response ) => { console . log ( response ) ; return response . text ( ) ; } ) . catch ( ( error ) => Observable . of ( this . buildErrorAnswer ( error ) ) ) ; } buildErrorAnswer ( error ) { let responseInfo = new ResponseInfo ( ) ; responseInfo . status = error . status ; responseInfo . text = error . json ( ) ; responseInfo . error = true ; return responseInfo ; } buildErrorAnswerXML ( error ) { let responseInfo = new ResponseInfo ( ) ; responseInfo . status = error . status ; responseInfo . text = error . text ( ) ; responseInfo . error = true ; responseInfo . format = "xml" ; return responseInfo ; } addAuthHeader ( headers ) { if ( this . authenticationService . authorization ) { if ( ! this . headers . has ( 'Authorization' ) ) { headers . set ( 'Authorization' , 'Basic ' + this . authenticationService . authorization ) ; } } } getRestData ( url , server ? ) { let urlEndpoint = server ? server + url : this . serverUrl + url ; return this . httpClient . get ( urlEndpoint ) ; } }