1
|
/*
|
2
|
* Created by myrto on 12/05/2017
|
3
|
*/
|
4
|
|
5
|
/*
|
6
|
* !!! USING TEMPORARY API ADDRESS AND USER
|
7
|
*/
|
8
|
|
9
|
import { Injectable } from '@angular/core';
|
10
|
import { Observable } from 'rxjs/Observable';
|
11
|
import 'rxjs/add/operator/map';
|
12
|
import { Http, Response, Headers, RequestOptions } from '@angular/http';
|
13
|
|
14
|
import {
|
15
|
AdvQueryObject, BrowseEntry, EventsPage, OpenaireSubscription, Repository, SimpleSubscriptionDesc, Subscription,
|
16
|
Term
|
17
|
} from '../domain/typeScriptClasses';
|
18
|
import { apiUrl } from '../domain/tempAPI';
|
19
|
|
20
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
21
|
let httpOptions = new RequestOptions({ headers: headers });
|
22
|
|
23
|
@Injectable ()
|
24
|
export class BrokerService {
|
25
|
private apiUrl = apiUrl + '/broker/';
|
26
|
|
27
|
constructor(private http: Http) { }
|
28
|
|
29
|
advancedShowEvents(page: number,size: number,searchParams: AdvQueryObject): Observable<EventsPage>{
|
30
|
let url = `${this.apiUrl}advancedShowEvents/${page}/${size}`;
|
31
|
console.log(`knocking on: ${url}`);
|
32
|
let body = searchParams;
|
33
|
console.log(`sending ${JSON.stringify(body)}`);
|
34
|
httpOptions.withCredentials = true;
|
35
|
return this.http.post(url,body,httpOptions)
|
36
|
.map( res => <EventsPage>res.json())
|
37
|
.catch(this.handleError).share();
|
38
|
}
|
39
|
|
40
|
getDatasourcesOfUser(userEmail: string): Observable<Repository[]> {
|
41
|
let url = `${this.apiUrl}getDatasourcesOfUser?user=${userEmail}&includeShared=true&includeByOthers=true`;
|
42
|
console.log(`knocking on: ${url}`);
|
43
|
|
44
|
httpOptions.withCredentials = true;
|
45
|
return this.http.post(url,httpOptions)
|
46
|
.map( res => <Repository[]>res.json())
|
47
|
.catch(this.handleError).share();
|
48
|
}
|
49
|
|
50
|
getDnetTopics(): Observable<Map<string,Term>> {
|
51
|
let url = `${this.apiUrl}getDnetTopics`;
|
52
|
console.log(`knocking on: ${url}`);
|
53
|
return this.http.get(url)
|
54
|
.map( res => <Map<string,Term>>res.json())
|
55
|
.catch(this.handleError);
|
56
|
}
|
57
|
|
58
|
getNotificationsBySubscriptionId(subId: string, page: number, size: number): Observable<EventsPage> {
|
59
|
let url = `${this.apiUrl}getNotificationsBySubscriptionId/${subId}/${page}/${size}`;
|
60
|
console.log(`knocking on: ${url}`);
|
61
|
return this.http.get(url)
|
62
|
.map( res => <EventsPage>res.json())
|
63
|
.catch(this.handleError);
|
64
|
}
|
65
|
|
66
|
getSimpleSubscriptionsOfUser(userEmail: string): Observable<Map<string,SimpleSubscriptionDesc>> {
|
67
|
let url = `${this.apiUrl}getSimpleSubscriptionsOfUser/${userEmail}/`;
|
68
|
console.log(`knocking on: ${url}`);
|
69
|
return this.http.get(url)
|
70
|
.map( res => <Map<string,SimpleSubscriptionDesc>>res.json())
|
71
|
.catch(this.handleError);
|
72
|
}
|
73
|
|
74
|
getSubscription(subId: string): Observable<Subscription> {
|
75
|
let url = `${this.apiUrl}getSubscription/${subId}`;
|
76
|
console.log(`knocking on: ${url}`);
|
77
|
return this.http.get(url)
|
78
|
.map( res => <Subscription>res.json())
|
79
|
.catch(this.handleError);
|
80
|
}
|
81
|
|
82
|
getSubscriptionsOfUser(userEmail: string): Observable<Map<string, Subscription>> {
|
83
|
let url = `${this.apiUrl}getSubscriptionsOfUser/${userEmail}/`;
|
84
|
console.log(`knocking on: ${url}`);
|
85
|
return this.http.get(url)
|
86
|
.map( res => <Map<string,Subscription>>res.json())
|
87
|
.catch(this.handleError);
|
88
|
}
|
89
|
|
90
|
getTopicsForDataSource(name: string): Observable<BrowseEntry[]> {
|
91
|
let url = `${this.apiUrl}getTopicsForDatasource/${name}`;
|
92
|
console.log(`knocking on: ${url}`);
|
93
|
return this.http.get(url)
|
94
|
.map( res => <BrowseEntry[]>res.json())
|
95
|
.catch(this.handleError);
|
96
|
}
|
97
|
|
98
|
/* NOT WORKING AND PROBABLY NOT NEEDED
|
99
|
showEvents(repoName: string, topic: string, page: number): Observable<EventsPage> {
|
100
|
let url = `${this.apiUrl}showEvents/{datasourceName}/{topic}/{page}?datasourceName=${repoName}&topic=${topic}&page=${page}`;
|
101
|
console.log(`knocking on: ${url}`);
|
102
|
return this.http.get(url)
|
103
|
.map( res => <EventsPage>res.json())
|
104
|
.catch(this.handleError);
|
105
|
}
|
106
|
*/
|
107
|
|
108
|
/*CHECK IF sub is sent as body*/
|
109
|
subscribeToEvent(sub: OpenaireSubscription): Observable<string>{
|
110
|
let url = `${this.apiUrl}subscribe`;
|
111
|
console.log(`knocking on: ${url}`);
|
112
|
httpOptions.withCredentials = true;
|
113
|
return this.http.post(url,sub,httpOptions)
|
114
|
.map( res => res.status.toString())
|
115
|
.catch(this.handleError);
|
116
|
}
|
117
|
|
118
|
unsubscribe(subscriptionId: string): Observable<string> {
|
119
|
let url = `${this.apiUrl}unsubscribe/${subscriptionId}`;
|
120
|
console.log(`knocking on: ${url}`);
|
121
|
httpOptions.withCredentials = true;
|
122
|
return this.http.post(url,httpOptions)
|
123
|
.map( res => res.status.toString())
|
124
|
.catch(this.handleError);
|
125
|
}
|
126
|
|
127
|
|
128
|
private handleError(error: Response | any) {
|
129
|
// In a real world app, we might use a remote logging infrastructure
|
130
|
// We'd also dig deeper into the error to get a better message
|
131
|
let errMsg = "";
|
132
|
console.log('E R R O R !!!');
|
133
|
console.log(error);
|
134
|
if (error instanceof Response) {
|
135
|
const body = error.text() || '';
|
136
|
//const err = body.error || JSON.stringify(body);
|
137
|
errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
|
138
|
console.log(errMsg);
|
139
|
} else {
|
140
|
errMsg = (error.message) ? error.message :
|
141
|
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
142
|
console.error(errMsg); // log to console instead
|
143
|
}
|
144
|
return Observable.throw(errMsg);
|
145
|
}
|
146
|
|
147
|
}
|