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, timeout} 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(
20
                  timeout(10000),
21
                  map(res => new Blob([res], { type: 'text/csv' })));
22
   }
23
   getCSVResponse(url: string){
24
     //var headers = new Headers();
25
     //headers.append('responseType', 'arraybuffer');
26
     return this.http.get(url, {responseType: 'text'})
27
                .pipe(timeout(10000));
28
                //.pipe(map(res => res));
29
   }
30

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

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

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

    
64

    
65
}
(14-14/23)