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

86 lines
3.3 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {Jsonp, URLSearchParams, RequestOptions, Headers} from '@angular/http';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {Claim} from '../claim';
import {OpenaireProperties} from '../../../utils/properties/openaireProperties';
import {AutoCompleteValue} from '../../../searchPages/searchUtils/searchHelperClasses.class';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import { } from '../../../shared/cache.service';
import { COOKIE } from '../../../login/utils/helper.class';
@Injectable()
export class ContextsService {
private baseUrl;
constructor(private http: Http ) {
this.baseUrl = OpenaireProperties.getClaimsAPIURL();
}
public getCommunities():any {
let url = this.baseUrl + 'communities';
let key = url;
console.info('ContextsService: request communities '+url);
return this.http.get(url, this.getAuthOptions())
.map(request => <any> request.json().data)
// .do(request => console.info("Get claims: offset = "))
.catch(this.handleError);
}
public getCategories(communityId :string):any {
console.info('ContextsService: request categories for community with id '+communityId);
let url= this.baseUrl + 'communities/' + communityId + '/categories';
let key = url;
return this.http.get(url, this.getAuthOptions())
.map(request => <any> request.json().data)
// .do(request => console.info("Get claims: offset = " ))
.catch(this.handleError);;
}
public getConcepts(categoryId :string, keyword: string, parsing:boolean):any {
console.info('ContextsService: request concept for category with id '+categoryId + ' and keyword '+ keyword);
let url= this.baseUrl + 'categories/' + categoryId+ "/concepts";
let key = url+"_parsing="+parsing;
return this.http.get(url, this.getAuthOptions())
.map(request => <any> request.json().data)
.catch(this.handleError)
.map(res => (parsing)?this.parse(res.concept):res.concept);
// .do(res => console.info("Result is "+ res.length ));
}
parse (data: any):AutoCompleteValue[] {
var array:AutoCompleteValue[] =[]
if(!Array.isArray(data) && data.id && data.label){
var value:AutoCompleteValue = new AutoCompleteValue();
value.id = data.id;
value.label = data.label;
array.push(value);
}
for(var i = 0; i < data.length; i++){
var value:AutoCompleteValue = new AutoCompleteValue();
value.id = data[i].id;
value.label = data[i].label;
array.push(value);
}
return array;
}
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');
}
private getAuthOptions():RequestOptions{
let headers = new Headers();
headers.append('X-XSRF-TOKEN', COOKIE.getCookie(COOKIE.cookieName_id));
let options = new RequestOptions({ headers: headers, withCredentials:true });
return options;
}
}