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

59 lines
1.8 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();
}
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";
restBasePath: string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/"+this.restpath+"/";
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();
2017-10-17 10:17:24 +02:00
var csrfToken : string = this.tokenService.getCSRFToken();
2017-10-16 17:05:25 +02:00
const params = new HttpParams();
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;
}
}