import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { BehaviorSubject , Observable , of } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { Quakeml } from '../quakeml' ; import { xmlToJson } from '../xml-to-json' ; /**
 * Service to parse the xml from the quakeml data
 */ @ Injectable ( ) export class QuakemlService { error : any = null ; quakeml$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ; constructor ( public httpClient ) { } param product getQuakeml ( product ) { try { if ( product . phasedata ) { product = product . phasedata ; } const quakeml = product . contents [ 'quakeml.xml' ] ; const options = { responseType : 'text' as 'text' } ; this . httpClient . get ( quakeml . url , options ) . pipe ( catchError ( this . handleError ( ) ) ) . subscribe ( response => { try { this . quakeml$ . next ( this . parseResponse ( response ) ) ; } catch ( e ) { this . error = e ; this . quakeml$ . next ( null ) ; } } ) ; } catch ( e ) { this . error = e ; this . quakeml$ . next ( null ) ; } } param response returns parseResponse ( response ) { if ( response === null ) { return null ; } return new Quakeml ( xmlToJson ( response ) ) ; } returns private handleError ( ) { return ( error ) : Observable < any > => { this . error = error ; return of ( null ) ; } ; } }