Project

General

Profile

1
/*
2
* Created by myrto on 12/05/2017
3
*/
4

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

    
9
import { Injectable } from '@angular/core';
10
import { Observable } from 'rxjs/Observable';
11
import 'rxjs/add/operator/map';
12
import { Http, Headers, RequestOptions, Response } from '@angular/http';
13

    
14
import { PiwikInfo } from '../domain/typeScriptClasses';
15
import { apiUrl } from '../domain/tempAPI';
16

    
17

    
18
const headers = new Headers({ 'Content-Type': 'application/json' });
19
const httpOptions = new RequestOptions({ headers: headers });
20

    
21
@Injectable ()
22
export class PiwikService {
23
  /*private apiUrl = `${apiUrl}/piwik/`;*/
24
  private apiUrl = process.env.API_ENDPOINT + '/piwik/';
25

    
26
  constructor(private http: Http) { }
27

    
28

    
29
  approvePiwikSite(repositoryId: string): Observable<string> {
30
    let url = `${this.apiUrl}approvePiwikSite/${repositoryId}`;
31
    console.log(`knocking on: ${url}`);
32
    return this.http.get(url)
33
      .map(res => res.status.toString())
34
      .catch(this.handleError);
35
  }
36

    
37
  getOpenaireId(id: string): Observable<string> {
38
    let url = `${this.apiUrl}getOpenaireId/${id}`;
39
    console.log(`knocking on: ${url}`);
40
    return this.http.get(url)
41
      .map( oaId => oaId['_body'].toString() )
42
      .catch(this.handleError);
43
  }
44

    
45
  getPiwikInfo(id: string): Observable<PiwikInfo> {
46
    let url = `${this.apiUrl}getPiwikSiteForRepo/${id}`;
47
    console.log(`knocking on: ${url}`);
48
    return this.http.get(url)
49
      .map( piwik => <PiwikInfo>piwik.json() )
50
      .catch(this.handleError);
51
  }
52

    
53
  getPiwikSitesForRepos(): Observable<PiwikInfo[]> {
54
    let url = `${this.apiUrl}getPiwikSitesForRepos`;
55
    console.log(`knocking on: ${url}`);
56
    return this.http.get(url)
57
      .map( res => <PiwikInfo[]>res.json())
58
      .catch(this.handleError);
59
  }
60

    
61
  savePiwikInfo(piwik: PiwikInfo): Observable<string>{
62
    let url = `${this.apiUrl}savePiwikInfo`;
63
    console.log(`knocking on: ${url}`);
64
    httpOptions.withCredentials = true;
65
    return this.http.post(url,piwik,httpOptions)
66
      .map( res => {
67
        console.log(`responded ${res.statusText}`);
68
        return res.status.toString();
69
      })
70
      .catch(this.handleError).share();
71
  }
72

    
73
  private handleError(error: Response | any) {
74
    // In a real world app, we might use a remote logging infrastructure
75
    // We'd also dig deeper into the error to get a better message
76
    let errMsg = "";
77
    console.log('E R R O R !!!');
78
    console.log(error);
79
    if (error instanceof Response) {
80
      const body = error.text() || '';
81
      //const err = body.error || JSON.stringify(body);
82
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
83
    } else {
84
      errMsg = (error.message) ? error.message :
85
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
86
      console.error(errMsg); // log to console instead
87
    }
88
    return Observable.throw(errMsg);
89
  }
90

    
91

    
92
}
(6-6/8)