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

63 lines
2.1 KiB
TypeScript

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();
}
protocol: string = "http";
hostname: string = "localhost";//"dl010.madgik.di.uoa.gr";//
port: number = 7070;//8080;//
webappname: string = "dmp-backend";//"dmp-backend-new";//
restpath: string = "rest";
loginPath : string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/";
restBasePath: string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/"+this.restpath+"/";
public login(path : string, data : any){
return this.http.post<any>(this.loginPath + path, JSON.stringify(data));
}
public get(path : string){
var options = this.createOptions();
return this.http.get<any>(this.restBasePath + path, options);
}
public post(path : string, data : any){
var options = this.createOptions();
return this.http.post<any>(this.restBasePath + 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('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);
let options = { params: params, headers: headers };
return options;
}
}