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 { Country, MetricsInfo, Repository, RepositoryInterface, Timezone } from '../domain/typeScriptClasses';
|
15
|
import { apiUrl } from '../domain/tempAPI';
|
16
|
|
17
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
18
|
let httpOptions = new RequestOptions({ headers: headers });
|
19
|
|
20
|
@Injectable ()
|
21
|
export class RepositoryService {
|
22
|
private apiUrl = apiUrl + '/repository/';
|
23
|
|
24
|
constructor(private http: Http) { }
|
25
|
|
26
|
addInterface(datatype: string, repoId: string, newInterface: RepositoryInterface): Observable<RepositoryInterface> {
|
27
|
let url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}`;
|
28
|
console.log(`knocking on: ${url}`);
|
29
|
console.log(`sending ${JSON.stringify(newInterface)}`);
|
30
|
httpOptions.withCredentials = true;
|
31
|
return this.http.post(url,newInterface,httpOptions)
|
32
|
.map( res => <RepositoryInterface>res.json())
|
33
|
.catch(this.handleError);
|
34
|
}
|
35
|
|
36
|
updateInterface(interfaceInfo: RepositoryInterface): Observable<string> {
|
37
|
let url = `${this.apiUrl}updateInterface`;
|
38
|
console.log(`knocking on: ${url}`);
|
39
|
console.log(`sending ${JSON.stringify(interfaceInfo)}`);
|
40
|
httpOptions.withCredentials = true;
|
41
|
return this.http.post(url,interfaceInfo,httpOptions)
|
42
|
.map( res => res.status.toString())
|
43
|
.catch(this.handleError);
|
44
|
}
|
45
|
|
46
|
deleteInterface(id: string): Observable<string> {
|
47
|
let url = `${this.apiUrl}deleteInterface/${id}`;
|
48
|
console.log(`knocking on: ${url}`);
|
49
|
httpOptions.withCredentials = true;
|
50
|
return this.http.delete(url,httpOptions)
|
51
|
.map( res => res.status.toString() )
|
52
|
.catch(this.handleError);
|
53
|
}
|
54
|
|
55
|
addRepository(datatype: string, newRepository: Repository): Observable<Repository> {
|
56
|
let url = `${this.apiUrl}addRepository?datatype=${datatype}`;
|
57
|
console.log(`knocking on: ${url}`);
|
58
|
console.log(`sending ${JSON.stringify(newRepository)}`);
|
59
|
httpOptions.withCredentials = true;
|
60
|
return this.http.post(url,newRepository,httpOptions)
|
61
|
.map( res => <Repository>res.json())
|
62
|
.catch(this.handleError);
|
63
|
}
|
64
|
|
65
|
updateRepository(repoInfo: Repository): Observable<string> {
|
66
|
let url = `${this.apiUrl}updateRepository`;
|
67
|
console.log(`knocking on: ${url}`);
|
68
|
console.log(`sending ${JSON.stringify(repoInfo)}`);
|
69
|
httpOptions.withCredentials = true;
|
70
|
return this.http.post(url,repoInfo,httpOptions)
|
71
|
.map( res => res.status.toString())
|
72
|
.catch(this.handleError);
|
73
|
}
|
74
|
|
75
|
getRepositoriesOfCountry(country: string, mode: string): Observable<Repository[]> {
|
76
|
let url = `${this.apiUrl}getRepositoriesByCountry/${country}/${mode}`;
|
77
|
console.log(`knocking on: ${url}`);
|
78
|
return this.http.get(url)
|
79
|
.map( res => <Repository[]>res.json())
|
80
|
.catch(this.handleError);
|
81
|
}
|
82
|
|
83
|
getRepositoriesOfUser(userEmail: string): Observable<Repository[]> {
|
84
|
let url = `${this.apiUrl}getRepositoriesOfUser/${userEmail}/0/100`;
|
85
|
console.log(`knocking on: ${url}`);
|
86
|
return this.http.get(url)
|
87
|
.map( res => <Repository[]>res.json())
|
88
|
.do(res => console.log(`counted ${res.length} repositories`))
|
89
|
.catch(this.handleError);
|
90
|
}
|
91
|
|
92
|
|
93
|
getRepositoryById(id: string): Observable<Repository> {
|
94
|
let url = `${this.apiUrl}getRepositoryById/${id}`;
|
95
|
console.log(`knocking on: ${url}`);
|
96
|
return this.http.get(url)
|
97
|
.map( res => <Repository>res.json())
|
98
|
.do(res => console.log(`got repository with name: ${res.officialName}`))
|
99
|
.catch(this.handleError);
|
100
|
}
|
101
|
|
102
|
getRepositoryInterface(id: string): Observable<RepositoryInterface[]>{
|
103
|
let url = `${this.apiUrl}getRepositoryInterface/${id}`;
|
104
|
console.log(`knocking on: ${url}`);
|
105
|
return this.http.get(url)
|
106
|
.map( res => <RepositoryInterface[]>res.json())
|
107
|
.catch(this.handleError);
|
108
|
}
|
109
|
|
110
|
getUrlsOfUserRepos(userEmail: string): Observable<string[]>{
|
111
|
let url = `${this.apiUrl}getUrlsOfUserRepos/${userEmail}/0/100/`;
|
112
|
console.log(`knocking on: ${url}`);
|
113
|
return this.http.get(url)
|
114
|
.map( res => <string[]>res.json())
|
115
|
.catch(this.handleError);
|
116
|
}
|
117
|
|
118
|
getTimezones(): Observable<Timezone[]>{
|
119
|
let url = `${this.apiUrl}getTimezones`;
|
120
|
console.log(`knocking on: ${url}`);
|
121
|
return this.http.get(url)
|
122
|
.map( res => <Timezone[]>res.json())
|
123
|
.catch(this.handleError);
|
124
|
}
|
125
|
|
126
|
getTypologies(): Observable<string[]>{
|
127
|
let url = `${this.apiUrl}getTypologies`;
|
128
|
console.log(`knocking on: ${url}`);
|
129
|
return this.http.get(url)
|
130
|
.map( res => <string[]>res.json())
|
131
|
.catch(this.handleError);
|
132
|
}
|
133
|
|
134
|
getCountries(): Observable<Country[]> {
|
135
|
let url = `${this.apiUrl}getCountries`;
|
136
|
console.log(`knocking on: ${url}`);
|
137
|
return this.http.get(url)
|
138
|
.map( res => <Country[]>res.json())
|
139
|
.catch(this.handleError);
|
140
|
}
|
141
|
|
142
|
|
143
|
getCompatibilityClasses (mode: string): Observable<Map<string,string>> {
|
144
|
let url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
|
145
|
console.log(`knocking on: ${url}`);
|
146
|
return this.http.get(url)
|
147
|
.map( res => <Map<string,string>>res.json())
|
148
|
.catch(this.handleError);
|
149
|
}
|
150
|
|
151
|
getDatasourceClasses(mode: string): Observable<Map<string,string>>{
|
152
|
let url = `${this.apiUrl}getDatasourceClasses/${mode}`;
|
153
|
console.log(`knocking on: ${url}`);
|
154
|
return this.http.get(url)
|
155
|
.map( res => <Map<string,string>>res.json())
|
156
|
.catch(this.handleError);
|
157
|
}
|
158
|
|
159
|
|
160
|
getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
|
161
|
let url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
|
162
|
console.log(`knocking on: ${url}`);
|
163
|
return this.http.get(url)
|
164
|
.map( res => <MetricsInfo>res.json())
|
165
|
.catch(this.handleError);
|
166
|
}
|
167
|
|
168
|
updateEnglishName(id: string, englishname: string): Observable<string>{
|
169
|
let url = `${this.apiUrl}updateEnglishName?id=${id}&officialName=DSpace&englishname=${englishname}`;
|
170
|
console.log(`knocking on: ${url}`);
|
171
|
httpOptions.withCredentials = true;
|
172
|
return this.http.post(url,httpOptions)
|
173
|
.map( res => {
|
174
|
console.log(`responded ${res.statusText}`);
|
175
|
return res.status.toString();
|
176
|
})
|
177
|
.catch(this.handleError).share();
|
178
|
}
|
179
|
|
180
|
updateLongtitude(id: string, longtitude: string): Observable<string>{
|
181
|
let url = `${this.apiUrl}updateLongtitude`;
|
182
|
console.log(`knocking on: ${url}`);
|
183
|
let body = JSON.stringify({
|
184
|
id : id,
|
185
|
logntitude: longtitude
|
186
|
});
|
187
|
console.log(`sending ${body}`);
|
188
|
httpOptions.withCredentials = true;
|
189
|
|
190
|
return this.http.post(url,body,httpOptions)
|
191
|
.map( res => <string>res.json())
|
192
|
.catch(this.handleError).share();
|
193
|
}
|
194
|
|
195
|
updateLatitude(id: string, latitude: string): Observable<string>{
|
196
|
let url = `${this.apiUrl}updateLatitude`;
|
197
|
console.log(`knocking on: ${url}`);
|
198
|
let body = JSON.stringify({
|
199
|
id : id,
|
200
|
latitude: latitude
|
201
|
});
|
202
|
console.log(`sending ${body}`);
|
203
|
httpOptions.withCredentials = true;
|
204
|
|
205
|
return this.http.post(url,body,httpOptions)
|
206
|
.map( res => <string>res.json())
|
207
|
.catch(this.handleError).share();
|
208
|
}
|
209
|
|
210
|
updateLogoUrl(id: string, logoUrl: string): Observable<string>{
|
211
|
let url = `${this.apiUrl}updateLogoUrl`;
|
212
|
console.log(`knocking on: ${url}`);
|
213
|
let body = JSON.stringify({
|
214
|
id : id,
|
215
|
logoUrl: logoUrl
|
216
|
});
|
217
|
console.log(`sending ${body}`);
|
218
|
httpOptions.withCredentials = true;
|
219
|
|
220
|
return this.http.post(url, body, httpOptions)
|
221
|
.map( res => <string>res.json())
|
222
|
.catch(this.handleError).share();
|
223
|
}
|
224
|
|
225
|
updateTimezone(id: string, timezone: string): Observable<string>{
|
226
|
let url = `${this.apiUrl}updateTimezone`;
|
227
|
console.log(`knocking on: ${url}`);
|
228
|
let body = JSON.stringify({
|
229
|
id : id,
|
230
|
timezone: timezone
|
231
|
});
|
232
|
console.log(`sending ${body}`);
|
233
|
httpOptions.withCredentials = true;
|
234
|
|
235
|
return this.http.post(url, body, httpOptions)
|
236
|
.map( res => <string>res.json())
|
237
|
.catch(this.handleError).share();
|
238
|
}
|
239
|
|
240
|
|
241
|
private handleError(error: Response | any) {
|
242
|
// In a real world app, we might use a remote logging infrastructure
|
243
|
// We'd also dig deeper into the error to get a better message
|
244
|
let errMsg = "";
|
245
|
console.log('E R R O R !!');
|
246
|
console.log(error);
|
247
|
if (error instanceof Response) {
|
248
|
const body = error.text() || '';
|
249
|
//const err = body.error || JSON.stringify(body);
|
250
|
errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
|
251
|
console.log(errMsg);
|
252
|
} else {
|
253
|
errMsg = (error.message) ? error.message :
|
254
|
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
255
|
console.error(errMsg); // log to console instead
|
256
|
}
|
257
|
return Observable.throw(errMsg);
|
258
|
}
|
259
|
|
260
|
}
|