openaire-library/claims/claim-utils/service/searchCrossref.service.ts

97 lines
3.8 KiB
TypeScript

import {throwError as observableThrowError} from 'rxjs';
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {ClaimEntity, ClaimResult} from '../claimHelper.class';
import {map} from "rxjs/operators";
import {EnvProperties} from "../../../utils/properties/env-properties";
@Injectable()
export class SearchCrossrefService {
constructor( private http: HttpClient ) {}
searchCrossrefResults(term: string, size: number, page: number, properties: EnvProperties, parse: boolean = false): any {
let url = properties.searchCrossrefAPIURL + '?query=' + term + '&rows=' + size + '&offset=' + (size * (page - 1));
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
.pipe(map(request => request['message']))
.pipe(map(request => [request["total-results"], parse ? this.parse(request.items) : request]))
//.catch(this.handleError);
}
searchCrossrefByDOIs(DOIs: string[], properties: EnvProperties, parse: boolean = false): any {
var doisParams = "";
for (var i = 0; i < DOIs.length; i++) {
doisParams += (doisParams.length > 0 ? "," : "") + 'doi:' + DOIs[i];
}
let url = properties.searchCrossrefAPIURL + '?filter=' + doisParams;
return this.http.get(url)
//.map(request => <any> request.json().message)
.pipe(map(request => request['message']))
.pipe(map(request => [request["total-results"], parse ? this.parse(request['items']) : request]))
//.catch(this.handleError);
}
searchCrossrefByMultipleDOIs(dois: string[], properties: EnvProperties, parse: boolean = false): any {
let url = properties.searchCrossrefAPIURL + '?filter=doi:';
for (var i = 0; i < dois.length; i++) {
url = url + (url.length == 0 ? '' : ',') + 'doi:' + dois[i];
}
url = properties.searchCrossrefAPIURL + '?filter=' + url;
return this.http.get(url)
//.map(request => <any> request.json().message)
.pipe(map(request => request['message']))
.pipe(map( request => [request["total-results"], parse ? this.parse(request.items) : request]))
//.catch(this.handleError);
}
private handleError(error: any) {
// 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 observableThrowError(error || 'Server error');
}
parse(response): ClaimEntity[] {
const results: ClaimEntity[] = [];
for (let i = 0; i < response.length; i++) {
const item = response[i];
const entity: ClaimEntity = new ClaimEntity();
entity.result = new ClaimResult();
entity.result.publisher = null;
entity.result.journal = null;
entity.result.DOI = item.DOI;
entity.id = item.DOI;
entity.title = item.title;
entity.result.url = item.URL;
entity.result.source = 'crossref';
entity.type = 'publication';
if (item['published-print']) {
entity.result.date = item['published-print']['date-parts'][0][0];
}
entity.result.accessRights = "OPEN";
entity.result.publisher = item.publisher;
entity.result.journal = null;
entity.result.record = item;
if (item.author) {
entity.result.authors = [];
for (let j = 0; j < item.author.length; j++) {
entity.result.authors.push(item.author[j].family + ", " + item.author[j].given);
}
}
if (item.editor) {
for (let j = 0; j < item.editor.length; j++) {
entity.result.editors.push(item.editor[j].family + ", " + item.editor[j].given);
}
}
results.push(entity);
}
return results;
}
}