Skip to content
Snippets Groups Projects
settings.service.ts 3.15 KiB
Newer Older
import {Injectable} from '@angular/core';
import {ConfigurationService} from "../env/configuration.service";
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";
import {QueueSetting, Setting, WorkflowSetting} from "../model/setting";

@Injectable({
  providedIn: 'root'
})
export class SettingsService {

  constructor(private configService: ConfigurationService, private http: HttpClient) {
  }

  public getSettings(): Observable<Setting> {
    return this.http.get<Setting>(this.configService.config.url + '/services/settings', {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public setSettings(settings: Setting): Observable<Setting> {
    return this.http.post<Setting>(this.configService.config.url + '/services/settings', settings, {observe: "response"}).pipe(
      map(response => {
        if (response.status === 204) {
          // this is a post which returns nothing, can't use response.body
          return settings;
        }
        // if for some completely weird reason, we do have response content, send it on, it's probably an error
        return response.body
      }));
  }

  public getQueueSettings(): Observable<Array<QueueSetting>> {
    return this.http.get<Array<QueueSetting>>(this.configService.config.url + '/services/job/queueSettings', {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public setQueueSettings(queueSetting: QueueSetting): Observable<QueueSetting> {
    return this.http.put<QueueSetting>(this.configService.config.url + '/services/job/queues/' + queueSetting.name + '/settings', queueSetting, {observe: "response"}).pipe(
      map(respnse => {
        return queueSetting;
      }));
  }

  public getWorkflowSettings(): Observable<Array<WorkflowSetting>> {
    return this.http.get<Array<WorkflowSetting>>(this.configService.config.url + '/services/job/queueWorkflows', {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public createFile(queue: string, name: string): Observable<any> {
    let headers = new HttpHeaders();
    let type, data;
    if (name.endsWith('json')) {
      headers = headers.append('Content-Type', 'application/json');
      data = '{}';
    } else if (name.endsWith('xml')) {
      headers = headers.append('Content-Type', 'text/xml');
      data = '<?xml version="1.0" encoding="utf-8"?>\n<?xml-stylesheet type="text/xsl" href="/VlassMngr/PPR/ppr2html.xsl"?>\n<SciPipeRequest/>\n';
    } else {
      headers = headers.append('Content-Type', 'text/plain');
      data = "your new template";
    }
    return this.http.post(this.configService.config.url + "/services/job/queues/" + queue + "/workflow/files/" + name, data, {
      observe: "response",
      headers
    }).pipe(map(response => {
      return response;
    }));
  }

  public deleteFile(queue: string, name: string): Observable<any> {
    return this.http.delete(this.configService.config.url + "/services/job/queues/" + queue + "/workflow/files/" + name, {observe: "response"}).pipe(map(response => {
      return response.status;
    }))
  }

}