openaire-library/services/reports.service.ts

55 lines
2.0 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response, Headers} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
@Injectable()
export class ReportsService {
// 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";
constructor(private http: Http) {}
//text/html
//On the service:
downloadCSVFile(url: string){
var headers = new Headers();
headers.append('responseType', 'arraybuffer');
return this.http.get(url)
.map(res => new Blob([res['_body']], { type: 'text/csv' }));
}
getCSVResponse(url: string){
var headers = new Headers();
headers.append('responseType', 'arraybuffer');
return this.http.get(url)
.map(res => res['_body']);
}
downloadHTMLFile(url: string, info: string){
var headers = new Headers();
headers.append('responseType', 'arraybuffer');
return this.http.get(url)
.map(res => this.addInfo(res, info))
.map(res => new Blob([res['_body']], { type: 'text/html' }))
.do(res => console.log(res))
}
addInfo(res:any, info:string) {
/*
var para = res.document.createElement("P"); // Create a <p> element
var t = res.document.createTextNode("This is a paragraph"); // Create a text node
para.appendChild(t); // Append the text to <p>
res.document.body.appendChild(para);
*/
res['_body'] = info+res['_body'];
return res;
}
private handleError (error: Response) {
// in a real world app, we may send the error to some remote logging infrastructure
// instead of just logging it to the console
console.log(error);
return Observable.throw(error || 'Server error');
}
}