Project

General

Profile

1
/*
2
* Created by myrto on 05/11/2018
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
import { ReportResponseWrapper } from '../domain/usageStatsClasses';
11

    
12

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

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

    
21
  constructor(private http: Http) { }
22

    
23

    
24
  getReportResponse(params: URLSearchParams): Observable<ReportResponseWrapper> {
25
    let url = `http://beta.services.openaire.eu/usagestats/sushilite/GetReport/?${params}`;
26
    console.log(`knocking on: ${url}`);
27
    httpOptions.withCredentials = true;
28
    return this.http.get(url, httpOptions)
29
      .map(res => <ReportResponseWrapper>res.json())
30
      .catch(this.handleError);
31
  }
32

    
33
  private handleError(error: Response | any) {
34
    // In a real world app, we might use a remote logging infrastructure
35
    // We'd also dig deeper into the error to get a better message
36
    let errMsg = "";
37
    console.log('E R R O R !!!');
38
    console.log(error);
39
    if (error instanceof Response) {
40
      const body = error.text() || '';
41
      //const err = body.error || JSON.stringify(body);
42
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
43
    } else {
44
      errMsg = (error.message) ? error.message :
45
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
46
      console.error(errMsg); // log to console instead
47
    }
48
    return Observable.throw(errMsg);
49
  }
50

    
51

    
52
}
(9-9/10)