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-17 02:15:09 +01:00
|
|
|
protocol: string = "http";
|
2017-12-09 23:31:07 +01:00
|
|
|
hostname: string ="localhost"
|
2017-11-27 14:40:16 +01:00
|
|
|
port: number = 8080;
|
2017-11-17 02:15:09 +01:00
|
|
|
webappname: string = "dmp-backend";
|
2017-11-14 19:46:34 +01:00
|
|
|
|
2017-11-22 18:33:29 +01:00
|
|
|
/*
|
2017-11-16 18:32:44 +01:00
|
|
|
protocol: string = "http";
|
|
|
|
hostname: string ="dl010.madgik.di.uoa.gr"
|
|
|
|
port: number = 8080;
|
|
|
|
webappname: string = "dmp-backend";
|
2017-11-20 10:10:17 +01:00
|
|
|
*/
|
2017-10-23 15:09:50 +02:00
|
|
|
|
2017-11-08 11:35:56 +01: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-11-08 14:33:14 +01:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-08 14:33:14 +01:00
|
|
|
public proxy_get_wQuery(path : string, query: string, params? : any){
|
|
|
|
var options = this.createOptions(params);
|
2017-11-10 11:54:15 +01:00
|
|
|
var queryPart = (query) ? "?search="+query : "";
|
|
|
|
return this.http.get<any>(this.proxyPath +"proxy?url=" + path + queryPart, options);
|
2017-11-08 14:33:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 11:54:15 +01:00
|
|
|
|
2017-10-18 15:20:21 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|