import { HttpClient , HttpHeaders, HttpParams } from '@angular/common/http'; import { TokenService, TokenProvider } from './token.service' import 'rxjs/Rx'; declare var X2JS: any; export class RestBase { xml2jsonOBJ: any; static get parameters() { return [HttpClient, TokenService] } constructor(public http : HttpClient, public tokenService : TokenService) { this.xml2jsonOBJ = new X2JS(); } protocol: string = "http"; hostname: string ="dl010.madgik.di.uoa.gr" ;//"localhost";//"dl010.madgik.di.uoa.gr";// port: number = 8080;//8080;// webappname: string = "dmp-backend";//"dmp-backend-new";// proxyPath : string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/proxy/"; loginPath : string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/login/"; restPath: string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/rest/"; public proxy_get(path : string){ var options = this.createOptions(); return this.http.get(this.proxyPath +"proxy?url="+ path, options); } public login(path : string, data : any){ let options = { headers: new HttpHeaders().set('Content-Type', 'application/json') }; return this.http.post(this.loginPath + path, JSON.stringify(data), options); } public get(path : string){ var options = this.createOptions(); return this.http.get(this.restPath + path, options); } public post(path : string, data : any) { var options = this.createOptions(); return this.http.post(this.restPath + path, JSON.stringify(data), options); } private createOptions(){ var token = this.tokenService.getToken(); var provider: TokenProvider = this.tokenService.getProvider(); //var csrfToken : string = this.tokenService.getCSRFToken(); const params = new HttpParams(); var headers; if(provider == TokenProvider.google) headers = new HttpHeaders().set('Content-Type', 'application/json').set("google-token", token); if(provider == TokenProvider.native) headers = new HttpHeaders().set('Content-Type', 'application/json').set("native-token", token); let options = { params: params, headers: headers }; return options; } }