1
|
/*
|
2
|
* Created by myrto on 12/05/2017
|
3
|
*/
|
4
|
|
5
|
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
|
6
|
import { Injectable } from '@angular/core';
|
7
|
import { environment } from '../../environments/environment';
|
8
|
import { PiwikInfo } from '../domain/typeScriptClasses';
|
9
|
import { Observable } from 'rxjs';
|
10
|
import {URLParameter} from '../domain/url-parameter';
|
11
|
import {PiwikInfoPage} from '../domain/page-content';
|
12
|
|
13
|
const headerOptions = {
|
14
|
headers : new HttpHeaders().set('Content-Type', 'application/json')
|
15
|
.set('Accept', 'application/json'),
|
16
|
withCredentials: true
|
17
|
};
|
18
|
|
19
|
@Injectable ()
|
20
|
export class PiwikService {
|
21
|
private apiUrl = environment.API_ENDPOINT + '/piwik/';
|
22
|
|
23
|
constructor(private httpClient: HttpClient) { }
|
24
|
|
25
|
|
26
|
approvePiwikSite(repositoryId: string) {
|
27
|
const url = `${this.apiUrl}approvePiwikSite/${repositoryId}`;
|
28
|
console.log(`knocking on: ${url}`);
|
29
|
return this.httpClient.get(url, {withCredentials: true, responseType: 'text'});
|
30
|
}
|
31
|
|
32
|
enableMetricsForRepository(repoName: string, repoWebsite: string, piwik: PiwikInfo): Observable<PiwikInfo> {
|
33
|
let url = `${this.apiUrl}enableMetricsForRepository`;
|
34
|
url = `${url}?officialName=${encodeURIComponent(repoName)}&repoWebsite=${encodeURIComponent(repoWebsite)}`;
|
35
|
console.log(`knocking on: ${url}`);
|
36
|
console.log(`sending ${JSON.stringify(piwik)}`);
|
37
|
|
38
|
return this.httpClient.post<PiwikInfo>(url, JSON.stringify(piwik), headerOptions);
|
39
|
}
|
40
|
|
41
|
getOpenaireId(id: string) {
|
42
|
const url = `${this.apiUrl}getOpenaireId/${id}`;
|
43
|
console.log(`knocking on: ${url}`);
|
44
|
|
45
|
return this.httpClient.get(url, {withCredentials: true, responseType: 'text'});
|
46
|
}
|
47
|
|
48
|
getPiwikInfo(id: string): Observable<PiwikInfo> {
|
49
|
const url = `${this.apiUrl}getPiwikSiteForRepo/${id}`;
|
50
|
console.log(`knocking on: ${url}`);
|
51
|
|
52
|
return this.httpClient.get<PiwikInfo>(url, headerOptions);
|
53
|
}
|
54
|
|
55
|
getPiwikSitesForRepos(urlParams: URLParameter[]): Observable<PiwikInfoPage> {
|
56
|
const url = `${this.apiUrl}getPiwikSitesForRepos`;
|
57
|
console.log(`knocking on: ${url}`);
|
58
|
let params = new HttpParams();
|
59
|
for (const urlParameter of urlParams) {
|
60
|
for (const value of urlParameter.value) {
|
61
|
params = params.append(urlParameter.key, value);
|
62
|
}
|
63
|
}
|
64
|
|
65
|
return this.httpClient.get<PiwikInfoPage>(url, {params, withCredentials: true});
|
66
|
}
|
67
|
|
68
|
markPiwikSiteAsValidated (repositoryId: string) {
|
69
|
const url = `${this.apiUrl}markPiwikSiteAsValidated/${repositoryId}`;
|
70
|
console.log(`knocking on: ${url}`);
|
71
|
const body = {};
|
72
|
|
73
|
return this.httpClient.post(url, body, { withCredentials: true, responseType: 'text' });
|
74
|
}
|
75
|
|
76
|
savePiwikInfo(piwik: PiwikInfo): Observable<PiwikInfo> {
|
77
|
const url = `${this.apiUrl}savePiwikInfo`;
|
78
|
console.log(`knocking on: ${url}`);
|
79
|
|
80
|
return this.httpClient.post<PiwikInfo>(url, piwik, headerOptions);
|
81
|
}
|
82
|
|
83
|
|
84
|
}
|