1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http, Response, Headers} from '@angular/http';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import {OpenaireProperties} from '../utils/properties/openaireProperties';
|
5
|
import 'rxjs/add/observable/of';
|
6
|
import 'rxjs/add/operator/do';
|
7
|
import 'rxjs/add/operator/share';
|
8
|
import { } from '../shared/cache.service';
|
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: Http) {}
|
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
|
.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
|
.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
|
.map(res => this.addInfo(res, info))
|
33
|
.map(res => new Blob([res['_body']], { type: 'text/html' }))
|
34
|
.do(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: Response) {
|
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 Observable.throw(error || 'Server error');
|
53
|
}
|
54
|
|
55
|
|
56
|
}
|