<commit_before>import { Injectable } from '@angular/core';
import { Headers, Http, Response, RequestOptions } from '@angular/http';

import { Program } from './programs/program';

import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

export class ApiResponse {
    constructor(
	public status: string,
	public uri: string,
	public errors: string[]) {
    }
}

@Injectable()
export class ApiService {

    // Observable resources
    private updateSource = new Subject<boolean>();
    // observable streams
    updateAnnounce$ = this.updateSource.asObservable();

    constructor(private http: Http) { }

    announceUpdate() {
	//console.log('API announcing update');
	this.updateSource.next(true);
    }

    getPrograms(): Observable<Program[]> {
	return this.http.get('/api/programs')
	    .map(this.extractData)
	    .catch(this.handleError);
    }

    addProgram(data: Object): Observable<ApiResponse> {
	let body = JSON.stringify(data);
	let headers = new Headers({'Content-Type': 'application/json'});
	let options = new RequestOptions({headers: headers});

	return this.http.post('/api/programs', body, options)
	    .map((res:Response) => {
		//console.log('Catching result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    saveProgram(data: Object): Observable<ApiResponse> {
	let body = JSON.stringify(data);
	let headers = new Headers({'Content-Type': 'application/json'});
	let options = new RequestOptions({headers: headers});

	return this.http.post(`/api/programs/${data['id']}`, body, options)
	    .map((res:Response) => {
		//console.log('Catching result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    delProgram(progid: number): Observable<ApiResponse> {
	return this.http.delete(`/api/programs/${progid}`)
	    .map((res:Response) => {
		//console.log('Delete result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    getProgram(progid: number): Observable<Program> {
	return this.http.get(`/api/programs/${progid}`)
	    .map((res:Response) => res.json());
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    loadProgram(data: Object): Observable<ApiResponse> {
	let body = JSON.stringify(data);
	let headers = new Headers({'Content-Type': 'application/json'});
	let options = new RequestOptions({headers: headers});

	return this.http.post('/api/brewd/loadProgram', body, options)
	    .map((res:Response) => {
		//console.log('Catching result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    private extractData(res: Response) {
	let body = res.json()
	return body.data || {};
    }

    private handleError(error: Response | any ) {
	let errMsg: string;<commit_msg>[ui] Program loading endpoint changed, followed up<commit_after>import { Injectable } from '@angular/core';
import { Headers, Http, Response, RequestOptions } from '@angular/http';

import { Program } from './programs/program';

import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

export class ApiResponse {
    constructor(
	public status: string,
	public uri: string,
	public errors: string[]) {
    }
}

@Injectable()
export class ApiService {

    // Observable resources
    private updateSource = new Subject<boolean>();
    // observable streams
    updateAnnounce$ = this.updateSource.asObservable();

    constructor(private http: Http) { }

    announceUpdate() {
	//console.log('API announcing update');
	this.updateSource.next(true);
    }

    getPrograms(): Observable<Program[]> {
	return this.http.get('/api/programs')
	    .map(this.extractData)
	    .catch(this.handleError);
    }

    addProgram(data: Object): Observable<ApiResponse> {
	let body = JSON.stringify(data);
	let headers = new Headers({'Content-Type': 'application/json'});
	let options = new RequestOptions({headers: headers});

	return this.http.post('/api/programs', body, options)
	    .map((res:Response) => {
		//console.log('Catching result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    saveProgram(data: Object): Observable<ApiResponse> {
	let body = JSON.stringify(data);
	let headers = new Headers({'Content-Type': 'application/json'});
	let options = new RequestOptions({headers: headers});

	return this.http.post(`/api/programs/${data['id']}`, body, options)
	    .map((res:Response) => {
		//console.log('Catching result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    delProgram(progid: number): Observable<ApiResponse> {
	return this.http.delete(`/api/programs/${progid}`)
	    .map((res:Response) => {
		//console.log('Delete result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    getProgram(progid: number): Observable<Program> {
	return this.http.get(`/api/programs/${progid}`)
	    .map((res:Response) => res.json());
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    loadProgram(data: Object): Observable<ApiResponse> {
	let body = JSON.stringify(data);
	let headers = new Headers({'Content-Type': 'application/json'});
	let options = new RequestOptions({headers: headers});

	return this.http.post('/api/brewd/program', body, options)
	    .map((res:Response) => {
		//console.log('Catching result, ', res.status);
		return res.json();
	    });
	    //.catch((error:any) => Observable.throw(error.json().error || 'Servererror'));
    }

    private extractData(res: Response) {
	let body = res.json()
	return body.data || {};
    }

    private handleError(error: Response | any ) {
	let errMsg: string;
{'fraction_non_alphanumeric': 0.17965125579907215, 'fraction_numerical': 0.0, 'mean_word_length': 3.9501187648456058, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6791342', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1916, 'n_words': 393}