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

    
25
  constructor(private http: Http) { }
26

    
27

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

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

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

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

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

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

    
90

    
91
}
(6-6/8)