Project

General

Profile

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
  Aggregations, Country, MetricsInfo, Repository, RepositoryInterface, Timezone,
16
  Typology
17
} from '../domain/typeScriptClasses';
18
import { apiUrl } from '../domain/tempAPI';
19
import { timezones } from '../domain/timezones';
20
import { typologies } from '../domain/typologies';
21

    
22
let headers = new Headers({ 'Content-Type': 'application/json' });
23
let httpOptions = new RequestOptions({ headers: headers });
24

    
25
@Injectable ()
26
export class RepositoryService {
27
  /*private apiUrl = apiUrl + '/repository/';*/
28
  private apiUrl = process.env.API_ENDPOINT + '/repository/';
29

    
30
  constructor(private http: Http) { }
31

    
32
  addInterface(datatype: string, repoId: string, newInterface: RepositoryInterface): Observable<RepositoryInterface> {
33
    let url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}`;
34
    console.log(`knocking on: ${url}`);
35
    console.log(`sending ${JSON.stringify(newInterface)}`);
36
    httpOptions.withCredentials = true;
37
    return this.http.post(url,newInterface,httpOptions)
38
      .map( res => <RepositoryInterface>res.json())
39
      .catch(this.handleError);
40
  }
41

    
42
  updateInterface(repoId: string, interfaceInfo: RepositoryInterface): Observable<RepositoryInterface> {
43
    let url = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}`;
44
    console.log(`knocking on: ${url}`);
45
    console.log(`sending ${JSON.stringify(interfaceInfo)}`);
46
    httpOptions.withCredentials = true;
47
    return this.http.post(url,interfaceInfo,httpOptions)
48
      .map( res => <RepositoryInterface>res.json())
49
      .catch(this.handleError);
50
  }
51

    
52
  deleteInterface(id: string): Observable<string> {
53
    let url = `${this.apiUrl}deleteInterface/?id=${id}`;
54
    console.log(`knocking on: ${url}`);
55
    httpOptions.withCredentials = true;
56
    return this.http.delete(url,httpOptions)
57
      .map( res => res.status.toString() )
58
      .catch(this.handleError);
59
  }
60

    
61
  addRepository(datatype: string, newRepository: Repository): Observable<Repository> {
62
    let url = `${this.apiUrl}addRepository?datatype=${datatype}`;
63
    console.log(`knocking on: ${url}`);
64
    console.log(`sending ${JSON.stringify(newRepository)}`);
65
    httpOptions.withCredentials = true;
66
    return this.http.post(url,newRepository,httpOptions)
67
      .map( res => <Repository>res.json())
68
      .catch(this.handleError);
69
  }
70

    
71
  updateRepository(repoInfo: Repository): Observable<Repository> {
72
    let url = `${this.apiUrl}updateRepository`;
73
    console.log(`knocking on: ${url}`);
74
    console.log(`sending ${JSON.stringify(repoInfo)}`);
75
    httpOptions.withCredentials = true;
76
    return this.http.post(url,repoInfo,httpOptions)
77
      .map( res => <Repository>res.json())
78
      .catch(this.handleError);
79
  }
80

    
81
  getRepositoriesOfCountry(country: string, mode: string): Observable<Repository[]> {
82
    let url = `${this.apiUrl}getRepositoriesByCountry/${country}/${mode}`;
83
    console.log(`knocking on: ${url}`);
84
    return this.http.get(url)
85
      .map( res => <Repository[]>res.json())
86
      .catch(this.handleError);
87
  }
88

    
89
  getRepositoriesOfUser(userEmail: string): Observable<Repository[]> {
90
    let url = `${this.apiUrl}getRepositoriesOfUser/${userEmail}/0/100`;
91
    console.log(`knocking on: ${url}`);
92
    return this.http.get(url)
93
      .map( res => <Repository[]>res.json())
94
      .do(res => console.log(`counted ${res.length} repositories`))
95
      .catch(this.handleError);
96
  }
97

    
98

    
99
  getRepositoryById(id: string): Observable<Repository> {
100
    let url = `${this.apiUrl}getRepositoryById/${id}`;
101
    console.log(`knocking on: ${url}`);
102
    return this.http.get(url)
103
      .map( res => <Repository>res.json())
104
      .do(res => console.log(`got repository with name: ${res.officialName}`))
105
      .catch(this.handleError);
106
  }
107

    
108
  getRepositoryInterface(id: string): Observable<RepositoryInterface[]>{
109
    let url = `${this.apiUrl}getRepositoryInterface/${id}`;
110
    console.log(`knocking on: ${url}`);
111
    return this.http.get(url)
112
      .map( res => <RepositoryInterface[]>res.json())
113
      .catch(this.handleError);
114
  }
115

    
116

    
117
  getUrlsOfUserRepos(userEmail: string): Observable<string[]>{
118
    let url = `${this.apiUrl}getUrlsOfUserRepos/${userEmail}/0/100/`;
119
    console.log(`knocking on: ${url}`);
120
    return this.http.get(url)
121
      .map( res => <string[]>res.json())
122
      .catch(this.handleError);
123
  }
124

    
125
  getRepositoryAggregations(id: string): Observable<Aggregations>{
126
    let url = `${this.apiUrl}getRepositoryAggregations/${id}`;
127
    console.log(`knocking on: ${url}`);
128
    return this.http.get(url)
129
      .map(res => <Aggregations>res.json())
130
      .catch(this.handleError);
131
  }
132

    
133
getTimezones(): Observable<Timezone[]>{
134
/*    let url = `${this.apiUrl}getTimezones`;
135
    console.log(`knocking on: ${url}`);
136
    return this.http.get(url)
137
      .map( res => <Timezone[]>res.json())
138
      .catch(this.handleError);*/
139
    return Observable.of(<Timezone[]>timezones);
140
  }
141

    
142
  getTypologies(): Observable<Typology[]>{
143
/*    let url = `${this.apiUrl}getTypologies`;
144
    console.log(`knocking on: ${url}`);
145
    return this.http.get(url)
146
      .map( res => <string[]>res.json())
147
      .catch(this.handleError);*/
148
    return Observable.of(<Typology[]>typologies);
149
  }
150

    
151
  getCountries(): Observable<Country[]> {
152
    let url = `${this.apiUrl}getCountries`;
153
    console.log(`knocking on: ${url}`);
154
    return this.http.get(url)
155
      .map( res => <Country[]>res.json())
156
      .catch(this.handleError);
157
  }
158

    
159

    
160
  getCompatibilityClasses (mode: string): Observable<Map<string,string>> {
161
    let url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
162
    console.log(`knocking on: ${url}`);
163
    return this.http.get(url)
164
      .map( res => <Map<string,string>>res.json())
165
      .catch(this.handleError);
166
  }
167

    
168
  getDatasourceClasses(mode: string): Observable<Map<string,string>>{
169
    let url = `${this.apiUrl}getDatasourceClasses/${mode}`;
170
    console.log(`knocking on: ${url}`);
171
    return this.http.get(url)
172
      .map( res => <Map<string,string>>res.json())
173
      .catch(this.handleError);
174
  }
175

    
176

    
177
  getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
178
    let url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
179
    console.log(`knocking on: ${url}`);
180
    return this.http.get(url)
181
      .map( res => <MetricsInfo>res.json())
182
      .catch(this.handleError);
183
  }
184

    
185
  updateEnglishName(id: string, englishname: string): Observable<string>{
186
    let url = `${this.apiUrl}updateEnglishName?id=${id}&officialName=DSpace&englishname=${englishname}`;
187
    console.log(`knocking on: ${url}`);
188
    httpOptions.withCredentials = true;
189
    return this.http.post(url,httpOptions)
190
      .map( res => {
191
        console.log(`responded ${res.statusText}`);
192
        return res.status.toString();
193
      })
194
      .catch(this.handleError).share();
195
  }
196

    
197
  updateLongtitude(id: string, longtitude: string): Observable<string>{
198
    let url = `${this.apiUrl}updateLongtitude`;
199
    console.log(`knocking on: ${url}`);
200
    let body = JSON.stringify({
201
      id : id,
202
      logntitude: longtitude
203
    });
204
    console.log(`sending ${body}`);
205
    httpOptions.withCredentials = true;
206

    
207
    return this.http.post(url,body,httpOptions)
208
      .map( res => <string>res.json())
209
      .catch(this.handleError).share();
210
  }
211

    
212
  updateLatitude(id: string, latitude: string): Observable<string>{
213
    let url = `${this.apiUrl}updateLatitude`;
214
    console.log(`knocking on: ${url}`);
215
    let body = JSON.stringify({
216
      id : id,
217
      latitude: latitude
218
    });
219
    console.log(`sending ${body}`);
220
    httpOptions.withCredentials = true;
221

    
222
    return this.http.post(url,body,httpOptions)
223
      .map( res => <string>res.json())
224
      .catch(this.handleError).share();
225
  }
226

    
227
  updateLogoUrl(id: string, logoUrl: string): Observable<string>{
228
    let url = `${this.apiUrl}updateLogoUrl`;
229
    console.log(`knocking on: ${url}`);
230
    let body = JSON.stringify({
231
      id : id,
232
      logoUrl: logoUrl
233
    });
234
    console.log(`sending ${body}`);
235
    httpOptions.withCredentials = true;
236

    
237
    return this.http.post(url, body, httpOptions)
238
      .map( res => <string>res.json())
239
      .catch(this.handleError).share();
240
  }
241

    
242
  updateTimezone(id: string, timezone: string): Observable<string>{
243
    let url = `${this.apiUrl}updateTimezone`;
244
    console.log(`knocking on: ${url}`);
245
    let body = JSON.stringify({
246
      id : id,
247
      timezone: timezone
248
    });
249
    console.log(`sending ${body}`);
250
    httpOptions.withCredentials = true;
251

    
252
    return this.http.post(url, body, httpOptions)
253
      .map( res => <string>res.json())
254
      .catch(this.handleError).share();
255
  }
256

    
257
  getListLatestUpdate(mode: string): Observable<string> {
258
    let url = `${this.apiUrl}getListLatestUpdate/${mode}`;
259
    console.log(`knocking on: ${url}`);
260
    return this.http.get(url)
261
      .map( res => res.json()['lastCollectionDate'])
262
      .catch(this.handleError);
263
  }
264

    
265

    
266
  private handleError(error: Response | any) {
267
    // In a real world app, we might use a remote logging infrastructure
268
    // We'd also dig deeper into the error to get a better message
269
    let errMsg = "";
270
    console.log('E R R O R !!');
271
    console.log(error);
272
    if (error instanceof Response) {
273
      const body = error.text() || '';
274
      //const err = body.error || JSON.stringify(body);
275
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
276
      console.log(errMsg);
277
    } else {
278
      errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
279
      console.error(errMsg); // log to console instead
280
    }
281
    return Observable.throw(errMsg);
282
  }
283

    
284
}
(7-7/8)