Project

General

Profile

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

    
5

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

    
11
import { PiwikInfo } from '../domain/typeScriptClasses';
12
import { apiUrl } from '../domain/tempAPI';
13

    
14

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

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

    
23
  constructor(private http: Http) { }
24

    
25

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

    
34
  enableMetricsForRepository(repoName: string, repoWebsite: string, piwik: PiwikInfo) {
35
    let url = `${this.apiUrl}enableMetricsForRepository?officialName=${encodeURIComponent(repoName)}&repoWebsite=${encodeURIComponent(repoWebsite)}`;
36
    console.log(`knocking on: ${url}`);
37
    console.log(`sending ${JSON.stringify(piwik)}`);
38
    httpOptions.withCredentials = true;
39
    return this.http.post(url,JSON.stringify(piwik),httpOptions)
40
      .map( res => res.status.toString() )
41
      .catch(this.handleError);
42
  }
43

    
44
  getOpenaireId(id: string): Observable<string> {
45
    let url = `${this.apiUrl}getOpenaireId/${id}`;
46
    console.log(`knocking on: ${url}`);
47
    httpOptions.withCredentials = true;
48
    return this.http.get(url, httpOptions)
49
      .map( oaId => oaId['_body'].toString() )
50
      .catch(this.handleError);
51
  }
52

    
53
  getPiwikInfo(id: string): Observable<PiwikInfo> {
54
    let url = `${this.apiUrl}getPiwikSiteForRepo/${id}`;
55
    console.log(`knocking on: ${url}`);
56
    httpOptions.withCredentials = true;
57
    return this.http.get(url, httpOptions)
58
      .map( piwik => <PiwikInfo>piwik.json() )
59
      .catch(this.handleError);
60
  }
61

    
62
  getPiwikSitesForRepos(): Observable<PiwikInfo[]> {
63
    let url = `${this.apiUrl}getPiwikSitesForRepos`;
64
    console.log(`knocking on: ${url}`);
65
    httpOptions.withCredentials = true;
66
    return this.http.get(url, httpOptions)
67
      .map( res => <PiwikInfo[]>res.json())
68
      .catch(this.handleError);
69
  }
70

    
71

    
72
  markPiwikSiteAsValidated (repositoryId: string) {
73
    let url = `${this.apiUrl}markPiwikSiteAsValidated/${repositoryId}`;
74
    console.log(`knocking on: ${url}`);
75
    httpOptions.withCredentials = true;
76
    return this.http.post(url,httpOptions)
77
      .map(res => res.status.toString())
78
      .catch(this.handleError);
79
  }
80

    
81
  savePiwikInfo(piwik: PiwikInfo): Observable<PiwikInfo>{
82
    let url = `${this.apiUrl}savePiwikInfo`;
83
    console.log(`knocking on: ${url}`);
84
    httpOptions.withCredentials = true;
85
    return this.http.post(url,piwik,httpOptions)
86
      .map( res => <PiwikInfo>res.json() )
87
      .catch(this.handleError);
88
  }
89

    
90
  private handleError(error: Response | any) {
91
    // In a real world app, we might use a remote logging infrastructure
92
    // We'd also dig deeper into the error to get a better message
93
    let errMsg = "";
94
    console.log('E R R O R !!!');
95
    console.log(error);
96
    if (error instanceof Response) {
97
      const body = error.text() || '';
98
      //const err = body.error || JSON.stringify(body);
99
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
100
    } else {
101
      errMsg = (error.message) ? error.message :
102
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
103
      console.error(errMsg); // log to console instead
104
    }
105
    return Observable.throw(errMsg);
106
  }
107

    
108

    
109
}
(6-6/8)