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 { HttpHeaders } from '@angular/common/http';
|
11
|
|
12
|
import { Observable } from 'rxjs/Observable';
|
13
|
|
14
|
import { PiwikInfo } from '../domain/typeScriptClasses';
|
15
|
import 'rxjs/add/operator/map';
|
16
|
import { Http, Response } from '@angular/http';
|
17
|
|
18
|
const httpOptions = {
|
19
|
headers: new HttpHeaders().set('Content-Type', 'application/json')
|
20
|
};
|
21
|
|
22
|
@Injectable ()
|
23
|
export class PiwikService {
|
24
|
/* private apiUrl = 'http://195.134.66.230:8380/uoa-repository-manager-service';*/
|
25
|
private apiUrl = 'http://194.177.192.121:8380/uoa-repository-manager-service';
|
26
|
|
27
|
constructor(private http: Http) { }
|
28
|
|
29
|
getPiwikInfo(id: string): Observable<PiwikInfo> {
|
30
|
let url = `${this.apiUrl}/piwik/getPiwikSiteForRepo/${id}`;
|
31
|
console.log(`knocking on: ${url}`);
|
32
|
return this.http.get(url)
|
33
|
.map( piwik => <PiwikInfo>piwik.json() )
|
34
|
.catch(this.handleError);
|
35
|
}
|
36
|
|
37
|
getPiwikSitesForRepos(): Observable<PiwikInfo[]> {
|
38
|
let url = `${this.apiUrl}/piwik/getPiwikSitesForRepos`;
|
39
|
console.log(`knocking on: ${url}`);
|
40
|
return this.http.get(url)
|
41
|
.map( res => <PiwikInfo[]>res.json())
|
42
|
.catch(this.handleError);
|
43
|
}
|
44
|
|
45
|
private handleError(error: Response | any) {
|
46
|
// In a real world app, we might use a remote logging infrastructure
|
47
|
// We'd also dig deeper into the error to get a better message
|
48
|
let errMsg = "";
|
49
|
console.log(error);
|
50
|
if (error instanceof Response) {
|
51
|
const body = error.text() || '';
|
52
|
//const err = body.error || JSON.stringify(body);
|
53
|
errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
|
54
|
} else {
|
55
|
errMsg = (error.message) ? error.message :
|
56
|
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
57
|
console.error(errMsg); // log to console instead
|
58
|
}
|
59
|
return Observable.throw(errMsg);
|
60
|
}
|
61
|
}
|