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

    
15
    //On the service:
16
   downloadCSVFile(url: string){
17
     //var headers = new Headers();
18
     //headers.append('responseType', 'arraybuffer');
19
     return this.http.get(url, {responseType: 'text'})
20
                .pipe(map(res => new Blob([res], { type: 'text/csv' })));
21
   }
22
   getCSVResponse(url: string){
23
     //var headers = new Headers();
24
     //headers.append('responseType', 'arraybuffer');
25
     return this.http.get(url, {responseType: 'text'});
26
                //.pipe(map(res => res));
27
   }
28

    
29
  /**
30
   * @deprecated not used
31
   */
32
   downloadHTMLFile(url: string, info: string){
33
     //var headers = new Headers();
34
     //headers.append('responseType', 'arraybuffer');
35
     return this.http.get(url)
36
                .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
   }
40

    
41
  /**
42
   * @deprecated not used
43
   */
44
   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
    private handleError (error: HttpErrorResponse) {
56
    // 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
        return throwError(error  || 'Server error');
60
    }
61

    
62

    
63
}
(13-13/21)