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
|
return this.http.get(url)
|
48
|
.map( oaId => oaId['_body'].toString() )
|
49
|
.catch(this.handleError);
|
50
|
}
|
51
|
|
52
|
getPiwikInfo(id: string): Observable<PiwikInfo> {
|
53
|
let url = `${this.apiUrl}getPiwikSiteForRepo/${id}`;
|
54
|
console.log(`knocking on: ${url}`);
|
55
|
return this.http.get(url)
|
56
|
.map( piwik => <PiwikInfo>piwik.json() )
|
57
|
.catch(this.handleError);
|
58
|
}
|
59
|
|
60
|
getPiwikSitesForRepos(): Observable<PiwikInfo[]> {
|
61
|
let url = `${this.apiUrl}getPiwikSitesForRepos`;
|
62
|
console.log(`knocking on: ${url}`);
|
63
|
return this.http.get(url)
|
64
|
.map( res => <PiwikInfo[]>res.json())
|
65
|
.catch(this.handleError);
|
66
|
}
|
67
|
|
68
|
|
69
|
markPiwikSiteAsValidated (repositoryId: string) {
|
70
|
let url = `${this.apiUrl}markPiwikSiteAsValidated/${repositoryId}`;
|
71
|
console.log(`knocking on: ${url}`);
|
72
|
httpOptions.withCredentials = true;
|
73
|
return this.http.post(url,httpOptions)
|
74
|
.map(res => res.status.toString())
|
75
|
.catch(this.handleError);
|
76
|
}
|
77
|
|
78
|
savePiwikInfo(piwik: PiwikInfo): Observable<PiwikInfo>{
|
79
|
let url = `${this.apiUrl}savePiwikInfo`;
|
80
|
console.log(`knocking on: ${url}`);
|
81
|
httpOptions.withCredentials = true;
|
82
|
return this.http.post(url,piwik,httpOptions)
|
83
|
.map( res => <PiwikInfo>res.json() )
|
84
|
.catch(this.handleError);
|
85
|
}
|
86
|
|
87
|
private handleError(error: Response | any) {
|
88
|
// In a real world app, we might use a remote logging infrastructure
|
89
|
// We'd also dig deeper into the error to get a better message
|
90
|
let errMsg = "";
|
91
|
console.log('E R R O R !!!');
|
92
|
console.log(error);
|
93
|
if (error instanceof Response) {
|
94
|
const body = error.text() || '';
|
95
|
//const err = body.error || JSON.stringify(body);
|
96
|
errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
|
97
|
} else {
|
98
|
errMsg = (error.message) ? error.message :
|
99
|
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
100
|
console.error(errMsg); // log to console instead
|
101
|
}
|
102
|
return Observable.throw(errMsg);
|
103
|
}
|
104
|
|
105
|
|
106
|
}
|