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