Project

General

Profile

1
/*
2
* Created by myrto on 1/24/2018
3
*/
4

    
5
/*
6
*  !!! USING TEMPORARY API ADDRESS AND USER
7
*/
8

    
9
import { HttpHeaders } from '@angular/common/http';
10
import { Injectable } from '@angular/core';
11
import { Http } from '@angular/http';
12
import { Observable } from 'rxjs/Observable';
13
import { RuleSet } from '../domain/typeScriptClasses';
14

    
15
const httpOptions = {
16
  headers: new HttpHeaders().set('Content-Type', 'application/json')
17
};
18

    
19
@Injectable ()
20
export class ValidatorService {
21

    
22
  /*  private apiUrl = 'http://195.134.66.230:8380/uoa-repository-manager-service';*/
23
  private apiUrl = 'http://194.177.192.121:8380/uoa-repository-manager-service';
24

    
25
  constructor(private http: Http) { }
26

    
27
  /* returns array of sets of rules according to mode (literature, data, cris) */
28
  getRuleSets(mode: string): Observable<RuleSet[]> {
29
    let url = `${this.apiUrl}/validator/getRuleSets/${mode}`;
30
    console.log(`knocking on: ${url}`);
31
    return this.http.get(url)
32
      .map(res => <RuleSet[]>res.json())
33
      .catch(this.handleError);
34
  }
35

    
36

    
37
  /* returns true if there is a repository containing the identifier */
38
  identifyRepository(identifier: string): Observable<boolean> {
39
    let url = `${this.apiUrl}/validator/identifyRepository/{url}?url=${identifier}`;
40
    console.log(`knocking on: ${url}`);
41
    return this.http.get(url)
42
      .map(res => <boolean>res.json())
43
      .catch(this.handleError);
44
  }
45

    
46

    
47
  /* from omtd project */
48
  private handleError(error: Response | any) {
49
    // In a real world app, we might use a remote logging infrastructure
50
    // We'd also dig deeper into the error to get a better message
51
    let errMsg = "";
52
    console.log(error);
53
    if (error instanceof Response) {
54
      const body = error.text() || '';
55
      //const err = body.error || JSON.stringify(body);
56
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
57
    } else {
58
      errMsg = (error.message) ? error.message :
59
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
60
      console.error(errMsg); // log to console instead
61
    }
62
    return Observable.throw(errMsg);
63
  }
64
}
(6-6/6)