argos/dmp-admin/src/app/services/rest-base.ts

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-10-16 17:05:25 +02:00
import { HttpClient , HttpHeaders, HttpParams } from '@angular/common/http';
import { TokenService, TokenProvider } from './login/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();
}
2017-10-27 09:48:33 +02:00
/*
*/
2017-10-16 17:05:25 +02:00
protocol: string = "http";
2018-01-18 11:27:30 +01:00
hostname: string = "dl043.madgik.di.uoa.gr";
port: number = 8080;
2017-12-22 14:46:36 +01:00
webappname: string = "";
restpath: string = "";
2017-10-16 17:05:25 +02:00
2017-10-27 09:48:33 +02:00
/*
protocol: string = "http";
hostname: string = "dl010.madgik.di.uoa.gr";
port: number = 8080;
webappname: string = "dmp-backend";
restpath: string = "rest";
*/
2017-10-16 17:05:25 +02:00
2017-10-17 11:26:58 +02:00
loginPath : string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/login/";
2017-10-27 10:20:35 +02:00
restPath: string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/"+this.restpath+"/";
2017-10-16 17:05:25 +02:00
2017-10-17 11:13:10 +02:00
public login(path : string, data : any){
2017-10-17 11:26:58 +02:00
let options = { headers: new HttpHeaders().set('Content-Type', 'application/json') };
return this.http.post<any>(this.loginPath + path, JSON.stringify(data), options);
2017-10-17 11:13:10 +02:00
}
2017-10-16 17:05:25 +02:00
2017-10-27 09:48:33 +02:00
public get(path : string, params? : any){
var options = this.createOptions(params);
2017-10-27 10:20:35 +02:00
return this.http.get<any>(this.restPath + path, options);
2017-10-16 17:05:25 +02:00
}
2017-10-27 09:48:33 +02:00
public post(path : string, data : any, params? : any){
var options = this.createOptions(params);
2017-10-27 10:20:35 +02:00
return this.http.post<any>(this.restPath + path, JSON.stringify(data), options);
2017-10-16 17:05:25 +02:00
}
2017-10-27 09:48:33 +02:00
private createOptions(parameters : any){
2017-10-16 17:05:25 +02:00
var token = this.tokenService.getToken();
var provider: TokenProvider = this.tokenService.getProvider();
2017-10-17 10:17:24 +02:00
var csrfToken : string = this.tokenService.getCSRFToken();
2017-10-27 09:48:33 +02:00
var params = new HttpParams();
if(parameters != null){
Object.entries(parameters).forEach( entry => {
params = params.set(entry[0], entry[1]);
});
}
2017-10-17 10:17:24 +02:00
var headers;
2017-10-16 17:05:25 +02:00
if(provider == TokenProvider.google)
2017-10-17 10:17:24 +02:00
headers = new HttpHeaders().set('Content-Type', 'application/json').set('X-CSRF-Token', csrfToken).set("google-token", token);
if(provider == TokenProvider.native)
headers = new HttpHeaders().set('Content-Type', 'application/json').set('X-CSRF-Token', csrfToken).set("native-token", token);
2017-10-16 17:05:25 +02:00
let options = { params: params, headers: headers };
return options;
}
2017-10-27 09:48:33 +02:00
2017-10-16 17:05:25 +02:00
}