syncthing/src/app/db-status.service.ts

53 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-03-17 14:46:32 +01:00
import { Injectable } from '@angular/core';
2020-03-18 03:37:39 +01:00
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
2020-03-17 14:46:32 +01:00
import { CookieService } from './cookie.service';
2020-03-18 03:37:39 +01:00
import { Observable, of } from 'rxjs';
import { map, retry, catchError } from 'rxjs/operators';
2020-03-17 14:46:32 +01:00
import { environment } from '../environments/environment'
2020-03-18 03:37:39 +01:00
import { apiURL, apiRetry } from './api-utils'
import { FolderStatus, Folder } from './folder'
2020-03-17 14:46:32 +01:00
@Injectable({
providedIn: 'root'
})
export class DbStatusService {
2020-03-18 03:37:39 +01:00
private folderStatus: Object = {};
private headers: HttpHeaders;
2020-03-17 14:46:32 +01:00
private dbStatusUrl = environment.production ? apiURL + 'rest/db/status' : 'api/dbStatus';
constructor(private http: HttpClient, private cookieService: CookieService) {
this.headers = new HttpHeaders(this.cookieService.getCSRFHeader())
2020-03-17 14:46:32 +01:00
}
2020-03-18 03:37:39 +01:00
getFolderStatus(id: string): Observable<FolderStatus> {
let httpOptions: { headers: HttpHeaders } |
{ headers: HttpHeaders, params: HttpParams };
2020-03-18 03:37:39 +01:00
if (id) {
httpOptions = {
headers: this.headers,
params: new HttpParams().set('folder', id)
};
} else {
httpOptions = { headers: this.headers };
2020-03-18 03:37:39 +01:00
}
2020-03-17 14:46:32 +01:00
return this.http
.get<FolderStatus>(this.dbStatusUrl, httpOptions)
2020-03-18 03:37:39 +01:00
.pipe(
retry(apiRetry),
map(res => {
// Remove from array in developement
// in-memory-web-api returns arrays
if (!environment.production) {
const a: any = res as any;
if (a.length > 0) {
return res[0];
}
}
2020-03-18 03:37:39 +01:00
return res;
})
2020-03-17 14:46:32 +01:00
);
}
2020-03-18 03:37:39 +01:00
}