Project

General

Profile

1 50169 argiro.kok
import {Injectable} from '@angular/core';
2
import {Http, Response, Headers} from '@angular/http';
3 55964 argiro.kok
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5 50169 argiro.kok
6 55964 argiro.kok
7
8
import {map, tap} from "rxjs/operators";
9
10 50169 argiro.kok
@Injectable()
11
export class ReportsService {
12
    // url:string = "http://beta.services.openaire.eu:8480/search/rest/v2/api/publications?format=csv&page=0&size=3&q=(%22test%22)&fq=instancetypename%20exact%20%22Dataset%22";
13 55964 argiro.kok
    constructor(private http: HttpClient) {}
14 56115 konstantin
15 50169 argiro.kok
    //On the service:
16
   downloadCSVFile(url: string){
17 55964 argiro.kok
     //var headers = new Headers();
18
     //headers.append('responseType', 'arraybuffer');
19 56115 konstantin
     return this.http.get(url, {responseType: 'text'})
20
                .pipe(map(res => new Blob([res], { type: 'text/csv' })));
21 50169 argiro.kok
   }
22
   getCSVResponse(url: string){
23 55964 argiro.kok
     //var headers = new Headers();
24
     //headers.append('responseType', 'arraybuffer');
25 56115 konstantin
     return this.http.get(url, {responseType: 'text'});
26
                //.pipe(map(res => res));
27 50169 argiro.kok
   }
28 56115 konstantin
29
  /**
30
   * @deprecated not used
31
   */
32 50169 argiro.kok
   downloadHTMLFile(url: string, info: string){
33 55964 argiro.kok
     //var headers = new Headers();
34
     //headers.append('responseType', 'arraybuffer');
35 50169 argiro.kok
     return this.http.get(url)
36 55964 argiro.kok
                .pipe(map(res => this.addInfo(res, info)))
37
                .pipe(map(res => new Blob([res['_body']], { type: 'text/html' })))
38
                .pipe(tap(res => console.log(res)))
39 50169 argiro.kok
   }
40
41 56115 konstantin
  /**
42
   * @deprecated not used
43
   */
44 50169 argiro.kok
   addInfo(res:any, info:string) {
45
       /*
46
       var para = res.document.createElement("P");                       // Create a <p> element
47
       var t = res.document.createTextNode("This is a paragraph");       // Create a text node
48
       para.appendChild(t);                                          // Append the text to <p>
49
       res.document.body.appendChild(para);
50
       */
51
       res['_body'] = info+res['_body'];
52
       return res;
53
   }
54
55 55964 argiro.kok
    private handleError (error: HttpErrorResponse) {
56 50169 argiro.kok
    // in a real world app, we may send the error to some remote logging infrastructure
57
    // instead of just logging it to the console
58
        console.log(error);
59 55964 argiro.kok
        return throwError(error  || 'Server error');
60 50169 argiro.kok
    }
61
62
63
}