Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response, Headers} from '@angular/http';
3
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5

    
6

    
7

    
8
import {map, tap} from "rxjs/operators";
9

    
10
@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
    constructor(private http: HttpClient) {}
14
//text/html
15
    //On the service:
16
   downloadCSVFile(url: string){
17
     //var headers = new Headers();
18
     //headers.append('responseType', 'arraybuffer');
19
     return this.http.get(url)
20
                .pipe(map(res => new Blob([res['_body']], { type: 'text/csv' })));
21
   }
22
   getCSVResponse(url: string){
23
     //var headers = new Headers();
24
     //headers.append('responseType', 'arraybuffer');
25
     return this.http.get(url)
26
                .pipe(map(res => res['_body']));
27
   }
28
   downloadHTMLFile(url: string, info: string){
29
     //var headers = new Headers();
30
     //headers.append('responseType', 'arraybuffer');
31
     return this.http.get(url)
32
                .pipe(map(res => this.addInfo(res, info)))
33
                .pipe(map(res => new Blob([res['_body']], { type: 'text/html' })))
34
                .pipe(tap(res => console.log(res)))
35
   }
36

    
37
   addInfo(res:any, info:string) {
38
       /*
39
       var para = res.document.createElement("P");                       // Create a <p> element
40
       var t = res.document.createTextNode("This is a paragraph");       // Create a text node
41
       para.appendChild(t);                                          // Append the text to <p>
42
       res.document.body.appendChild(para);
43
       */
44
       res['_body'] = info+res['_body'];
45
       return res;
46
   }
47

    
48
    private handleError (error: HttpErrorResponse) {
49
    // in a real world app, we may send the error to some remote logging infrastructure
50
    // instead of just logging it to the console
51
        console.log(error);
52
        return throwError(error  || 'Server error');
53
    }
54

    
55

    
56
}
(14-14/23)