2017-10-18 15:20:21 +02:00
|
|
|
import { HttpClient , HttpHeaders, HttpParams } from '@angular/common/http';
|
2017-11-01 18:18:27 +01:00
|
|
|
import { TokenService, TokenProvider } from './login/token.service'
|
2017-10-18 15:20:21 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-10-27 16:08:10 +02:00
|
|
|
|
2017-11-01 10:37:42 +01:00
|
|
|
|
2017-11-02 16:00:53 +01:00
|
|
|
|
2017-11-03 18:57:06 +01:00
|
|
|
/*
|
2017-11-01 10:09:47 +01:00
|
|
|
protocol: string = "http";
|
2017-10-27 09:48:53 +02:00
|
|
|
hostname: string ="dl010.madgik.di.uoa.gr" ;//"localhost";//"dl010.madgik.di.uoa.gr";//
|
|
|
|
port: number = 8080;//8080;//
|
2017-11-01 10:09:47 +01:00
|
|
|
webappname: string = "dmp-backend";//"dmp-backend-new";//
|
2017-11-03 18:57:06 +01:00
|
|
|
*/
|
2017-10-27 16:08:10 +02:00
|
|
|
|
2017-10-27 09:48:53 +02:00
|
|
|
|
2017-11-02 15:19:12 +01:00
|
|
|
|
2017-11-03 18:57:06 +01:00
|
|
|
protocol: string = "http";
|
2017-10-27 11:52:27 +02:00
|
|
|
hostname: string = "dionysus.di.uoa.gr" ;
|
|
|
|
port: number = 7070;
|
2017-11-03 18:57:06 +01:00
|
|
|
webappname: string = "dmp-backend";
|
2017-11-02 15:19:12 +01:00
|
|
|
|
2017-10-23 15:09:50 +02:00
|
|
|
|
2017-10-18 15:20:21 +02:00
|
|
|
|
|
|
|
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/";
|
|
|
|
|
|
|
|
|
2017-10-27 10:20:35 +02:00
|
|
|
public proxy_get(path : string, params? : any){
|
|
|
|
var options = this.createOptions(params);
|
2017-10-18 15:20:21 +02:00
|
|
|
return this.http.get<any>(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<any>(this.loginPath + path, JSON.stringify(data), options);
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:20:35 +02:00
|
|
|
public get(path : string, params? : any){
|
|
|
|
var options = this.createOptions(params);
|
2017-10-18 15:20:21 +02:00
|
|
|
return this.http.get<any>(this.restPath + path, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-27 10:20:35 +02:00
|
|
|
public post(path : string, data : any, params? : any){
|
|
|
|
var options = this.createOptions(params);
|
2017-10-18 15:20:21 +02:00
|
|
|
return this.http.post<any>(this.restPath + path, JSON.stringify(data), options);
|
|
|
|
}
|
|
|
|
|
2017-10-27 10:20:35 +02:00
|
|
|
private createOptions(parameters : any){
|
2017-10-18 15:20:21 +02:00
|
|
|
var token = this.tokenService.getToken();
|
|
|
|
var provider: TokenProvider = this.tokenService.getProvider();
|
|
|
|
|
2017-10-27 10:20:35 +02:00
|
|
|
var params = new HttpParams();
|
|
|
|
|
|
|
|
if(parameters != null){
|
|
|
|
Object.entries(parameters).forEach( entry => {
|
|
|
|
params = params.set(entry[0], entry[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-18 15:20:21 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|