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 { Injectable } from '@angular/core';
10
import { Http, Headers, RequestOptions } from '@angular/http';
11
import { Observable } from 'rxjs/Observable';
12
import { InterfaceInformation, JobForValidation, RuleSet, StoredJob } from '../domain/typeScriptClasses';
13
import { apiUrl } from '../domain/tempAPI';
14

    
15

    
16
let headers = new Headers({ 'Content-Type': 'application/json' });
17
let httpOptions = new RequestOptions({ headers: headers });
18

    
19
@Injectable ()
20
export class ValidatorService {
21

    
22
  /*private apiUrl = apiUrl + '/validator/';*/
23
  private apiUrl = process.env.API_ENDPOINT + '/validator/';
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}getRuleSets/${mode}`;
30
    console.log(`knocking on: ${url}`);
31
    return this.http.get(url,httpOptions)
32
      .map(res => <RuleSet[]>res.json())
33
      .catch(this.handleError);
34
  }
35

    
36

    
37
  getSetsOfRepository(baseUrl: string): Observable<string[]> {
38
    let url = `${this.apiUrl}getSetsOfRepository?url=${baseUrl}`;
39
    console.log(`knocking on: ${url}`);
40
    return this.http.get(url,httpOptions)
41
      .map(res => <string[]>res.json())
42
      .catch(this.handleError);
43
  }
44

    
45
  getStoredJobsNew(userEmail: string,
46
                   jobType:string,
47
                   offset: string,
48
                   limit: string,
49
                   dateFrom: string,
50
                   dateTo: string,
51
                   validationStatus: string): Observable<StoredJob[]> {
52
    let url = `${this.apiUrl}getStoredJobsNew?user=${userEmail}&jobType=${encodeURI(jobType)}&offset=${offset}&limit=${limit}&dateFrom=${dateFrom}&dateTo=${dateTo}&validationStatus=${validationStatus}`;
53
    console.log(`knocking on: ${url}`);
54
    return this.http.get(url,httpOptions)
55
      .map(res => <StoredJob[]>res.json())
56
      .catch(this.handleError);
57
  }
58

    
59
  /* returns true if there is a repository containing the baseUrl */
60
  identifyRepository(baseUrl: string): Observable<boolean> {
61
    let url = `${this.apiUrl}identifyRepository?url=${baseUrl}`;
62
    console.log(`knocking on: ${url}`);
63
    return this.http.get(url,httpOptions)
64
      .map(res => <boolean>res.json())
65
      .catch(this.handleError);
66
  }
67

    
68
  getInterfaceInformation(baseUrl: string): Observable<InterfaceInformation> {
69
    let url = `${this.apiUrl}getInterfaceInformation?baseUrl=${encodeURIComponent(baseUrl)}`;
70
    console.log(`knocking on: ${url}`);
71
    return this.http.get(url,httpOptions)
72
      .map(res => <InterfaceInformation>res.json())
73
      .catch(this.handleError);
74
  }
75

    
76
  reSubmitJobForValidation(id: string): Observable<string> {
77
    let url = `${this.apiUrl}reSubmitJobForValidation/${id}`;
78
    console.log(`knocking on: ${url}`);
79

    
80
    httpOptions.withCredentials = true;
81
    return this.http.post(url,httpOptions)
82
      .map(res => {
83
        console.log(`responded ${res.status}`);
84
        return res.status.toString();
85
      })
86
      .catch(this.handleError);
87
  }
88

    
89
  submitJobForValidation(job: JobForValidation): Observable<string> {
90
    let url = `${this.apiUrl}submitJobForValidation`;
91
    console.log(`knocking on: ${url}`);
92
    let body = JSON.stringify(job);
93
    httpOptions.withCredentials = true;
94
    return this.http.post(url,body,httpOptions)
95
      .map(res => {
96
        console.log(`responded ${res.status}`);
97
        return res.status.toString();
98
      })
99
      .catch(this.handleError);
100
  }
101

    
102

    
103
/* from omtd project */
104
  private handleError(error: Response | any) {
105
    // In a real world app, we might use a remote logging infrastructure
106
    // We'd also dig deeper into the error to get a better message
107
    let errMsg = "";
108
    console.log('E R R O R !!!');
109
    console.log(error);
110
    if (error instanceof Response) {
111
      const body = error.text() || '';
112
      //const err = body.error || JSON.stringify(body);
113
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
114
    } else {
115
      errMsg = (error.message) ? error.message :
116
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
117
      console.error(errMsg); // log to console instead
118
    }
119
    return Observable.throw(errMsg);
120
  }
121
}
(8-8/8)