2017-12-21 04:28:43 +00:00
|
|
|
import { Observable } from "rxjs/Observable";
|
2017-12-23 04:08:10 +00:00
|
|
|
import { SessionStorage } from "../SessionStorage";
|
2019-06-29 06:21:56 +00:00
|
|
|
import { HttpClient } from "@angular/common/http";
|
2017-12-21 04:28:43 +00:00
|
|
|
|
|
|
|
export class AuthedApi {
|
2019-07-01 05:05:33 +00:00
|
|
|
constructor(protected http: HttpClient, private mscAuth = false) {
|
2017-12-21 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-29 06:21:56 +00:00
|
|
|
protected authedGet<T>(url: string, qs?: any): Observable<T> {
|
2019-07-01 05:05:33 +00:00
|
|
|
const opts = this.fillAuthOptions(null, qs, null);
|
|
|
|
return this.http.get<T>(url, opts);
|
2017-12-21 04:28:43 +00:00
|
|
|
}
|
2017-12-24 04:40:01 +00:00
|
|
|
|
2019-06-29 06:21:56 +00:00
|
|
|
protected authedPost<T>(url: string, body?: any): Observable<T> {
|
2017-12-24 04:40:01 +00:00
|
|
|
if (!body) body = {};
|
2019-07-01 05:05:33 +00:00
|
|
|
const opts = this.fillAuthOptions(null, null, null);
|
|
|
|
return this.http.post<T>(url, body, opts);
|
2017-12-24 04:40:01 +00:00
|
|
|
}
|
2018-03-25 22:20:15 +00:00
|
|
|
|
2019-07-06 21:19:27 +00:00
|
|
|
protected authedPut<T>(url: string, body?: any): Observable<T> {
|
|
|
|
if (!body) body = {};
|
|
|
|
const opts = this.fillAuthOptions(null, null, null);
|
|
|
|
return this.http.put<T>(url, body, opts);
|
|
|
|
}
|
|
|
|
|
2019-06-29 06:21:56 +00:00
|
|
|
protected authedDelete<T>(url: string, qs?: any): Observable<T> {
|
2019-07-01 05:05:33 +00:00
|
|
|
const opts = this.fillAuthOptions(null, qs, null);
|
|
|
|
return this.http.delete<T>(url, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
private fillAuthOptions(opts: any, qs: any, headers: any): { headers: any, params: any } {
|
|
|
|
if (!opts) opts = {};
|
2018-03-25 22:20:15 +00:00
|
|
|
if (!qs) qs = {};
|
2019-07-01 05:05:33 +00:00
|
|
|
if (!headers) headers = {};
|
|
|
|
if (this.mscAuth) {
|
|
|
|
headers["Authorization"] = `Bearer ${SessionStorage.scalarToken}`;
|
|
|
|
} else {
|
|
|
|
qs["scalar_token"] = SessionStorage.scalarToken;
|
|
|
|
}
|
|
|
|
return Object.assign({}, opts, {params: qs, headers: headers});
|
2018-03-25 22:20:15 +00:00
|
|
|
}
|
2017-12-21 04:28:43 +00:00
|
|
|
}
|