openaire-library/claims/claimsByToken/claimsByToken.service.ts

77 lines
2.9 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Jsonp, URLSearchParams,ResponseOptions, RequestOptions, Headers} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/do';
import { CustomOptions } from '../claim-utils/service/customOptions.class';
@Injectable()
export class ClaimsByTokenService {
constructor(private http: Http ) {}
getClaims(token: string, jwtToken: string, apiURL:string):any {
console.info("getClaims in service");
let url = apiURL+"project/claims?projectToken="+token;
let key = url;
return this.http.get(url, CustomOptions.getAuthOptions())
//.map(res => <any> res.text())
.map(request => <any> request.json());
}
/*
getClaims(email: string, token: string, user_token: string):any {
let url = OpenaireProperties.getClaimsAPIURL(); // What else?
let body = JSON.stringify( {"email": email, "token": token} );
console.warn('Json body: : '+body);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(url, body, options)
.map(res => res.json())
.do(request => console.info("Insert Response:"+request.status) )
.catch(this.handleError);
}
*/
updateClaimsCuration( selectedRight: Set<string>, selectedWrong: Set<string>, apiURL:string) {
let url = apiURL + "curate/bulk";
let claimsCurationInfo: any = []; //e.g.: [{"id":"2","approved":true},{"id":"1","approved":true}]
selectedRight.forEach(function(selected) {
console.info(selected);
let claimCurationInfo: {"id": string, "approved": boolean} = {"id": selected, "approved": true};
claimsCurationInfo.push(claimCurationInfo);
});
selectedWrong.forEach(function(selected) {
let claimCurationInfo: any = {"id": selected, "approved": false};
claimsCurationInfo.push(claimCurationInfo);
});
console.info("\n\n"+claimsCurationInfo);
let body = JSON.stringify( claimsCurationInfo );
console.warn('Json body: : '+body);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody())
.map(res => res.json())
.do(request => console.info("Insert Response:"+request.status) )
.catch(this.handleError);
}
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');
}
}