/**
 * Created by jayhamilton on 1/18/17.
 */ import { Injectable } from '@angular/core' ; import { throwError , Observable } from 'rxjs' ; import { catchError } from "rxjs/operators" ; import { ErrorHandler } from '../error/error-handler' ; import { HttpClient , HttpErrorResponse , HttpParams } from '@angular/common/http' ; import { environment } from '../../environments/environment' ; @ Injectable ( ) export class RuntimeService { watsonMicroserviceURL : string ; connectivityTestURL : string ; serviceVersionURL : string ; testURL = "http://localhost:8080" ; static handleError ( err ) { const errMsg = { status : '-1' , statusText : '' , resource : '' } ; if ( err . error instanceof Error ) { errMsg . statusText = err . error . message ; console . log ( 'Client error' ) ; } else { errMsg . status = err . status ; errMsg . statusText = 'A backend error occurred. In all likelihood the server/api service is not running.' ; errMsg . resource = err . url ; } return throwError ( ErrorHandler . getErrorObject ( errMsg ) ) ; } constructor ( private _http ) { this . configure ( ) ; } configure ( ) { if ( environment . production ) { this . watsonMicroserviceURL = '/classify' ; this . connectivityTestURL = '/connectTest' ; this . serviceVersionURL = '/version' ; } else { this . connectivityTestURL = this . testURL + '/connectTest' ; this . watsonMicroserviceURL = this . testURL + '/classify' ; this . serviceVersionURL = this . testURL + '/version' ; } } testURLResponse ( url ) { return this . _http . get ( url , { responseType : 'text' } ) . pipe ( catchError ( RuntimeService . handleError ) ) ; } testConnectivity ( host , port ) { let p = new HttpParams ( ) ; p = p . append ( 'host' , host ) ; p = p . append ( 'port' , port ) ; return this . _http . get ( this . connectivityTestURL , { params : p } ) . pipe ( catchError ( RuntimeService . handleError ) ) ; } param { string } aiStatement returns { Observable < any > } callWitAI ( aiStatement ) { console . log ( 'running WitAi' ) ; if ( ! environment . production ) { let p = new HttpParams ( ) ; if ( ! localStorage . getItem ( 'Wit.aiToken' ) ) { return ; } p = p . append ( 'v' , '20171128' ) ; p = p . append ( 'q' , aiStatement ) ; p = p . append ( 'access_token' , localStorage . getItem ( 'Wit.aiToken' ) ) ; return this . _http . jsonp ( 'https://api.wit.ai/message?' + p . toString ( ) , 'callback' ) . pipe ( catchError ( RuntimeService . handleError ) ) ; } } callback ( data ) { } RestController CrossOrigin RequestMapping RequestParam RequestParam RequestParam param { string } aiStatement returns { Observable < any > } callWatsonAI ( aiStatement ) { console . log ( 'running Watson' ) ; if ( ! localStorage . getItem ( 'ibmwatsoncid' ) ) { return ; } let p = new HttpParams ( ) ; p = p . append ( 'userid' , localStorage . getItem ( 'ibmwatsonuid' ) ) ; p = p . append ( 'password' , localStorage . getItem ( 'ibmwatsonpwd' ) ) ; p = p . append ( 'classifier_id' , localStorage . getItem ( 'ibmwatsoncid' ) ) ; p = p . append ( 'data' , aiStatement ) ; return this . _http . get ( this . watsonMicroserviceURL , { params : p } ) . pipe ( catchError ( RuntimeService . handleError ) ) ; } }