config.service.ts 1.62 KB
Newer Older
Steven Zou's avatar
Steven Zou committed
1 2 3 4 5 6 7
import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/toPromise';

import { Configuration } from './config';

const configEndpoint = "/api/configurations";
8 9
const emailEndpoint = "/api/email/ping";
const ldapEndpoint = "/api/ldap/ping";
Steven Zou's avatar
Steven Zou committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

@Injectable()
export class ConfigurationService {
    private headers: Headers = new Headers({
        "Accept": 'application/json',
        "Content-Type": 'application/json'
    });
    private options: RequestOptions = new RequestOptions({
        'headers': this.headers
    });

    constructor(private http: Http) { }

    public getConfiguration(): Promise<Configuration> {
        return this.http.get(configEndpoint, this.options).toPromise()
        .then(response => response.json() as Configuration)
        .catch(error => Promise.reject(error));
    }

    public saveConfiguration(values: any): Promise<any> {
        return this.http.put(configEndpoint, JSON.stringify(values), this.options)
        .toPromise()
        .then(response => response)
        .catch(error => Promise.reject(error));
    }
35 36 37 38 39 40 41 42 43 44 45 46 47 48

    public testMailServer(mailSettings: any): Promise<any> {
        return this.http.post(emailEndpoint, JSON.stringify(mailSettings), this.options)
        .toPromise()
        .then(response => response)
        .catch(error => Promise.reject(error));
    }

    public testLDAPServer(ldapSettings: any): Promise<any> {
         return this.http.post(ldapEndpoint, JSON.stringify(ldapSettings), this.options)
        .toPromise()
        .then(response => response)
        .catch(error => Promise.reject(error));
    }
Steven Zou's avatar
Steven Zou committed
49
}