Project

General

Profile

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

    
5

    
6

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

    
9
@Injectable()
10
export class ReportsService {
11
    // 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";
12
    constructor(private http: HttpClient) {}
13

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

    
28
  /**
29
   * @deprecated not used
30
   */
31
   downloadHTMLFile(url: string, info: string){
32
     //var headers = new Headers();
33
     //headers.append('responseType', 'arraybuffer');
34
     return this.http.get(url)
35
                .pipe(map(res => this.addInfo(res, info)))
36
                .pipe(map(res => new Blob([res['_body']], { type: 'text/html' })))
37
                .pipe(tap(res => console.log(res)))
38
   }
39

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

    
54
    private handleError (error: HttpErrorResponse) {
55
    // in a real world app, we may send the error to some remote logging infrastructure
56
    // instead of just logging it to the console
57
        console.log(error);
58
        return throwError(error  || 'Server error');
59
    }
60

    
61

    
62
}
(14-14/23)