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 { 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
  getRepositoriesOfCountry(country: string, mode: string): Observable<Repository[]> {
27
    let url = `${this.apiUrl}getRepositoriesByCountry/${country}/${mode}`;
28
    console.log(`knocking on: ${url}`);
29
    return this.http.get(url)
30
      .map( res => <Repository[]>res.json())
31
      .catch(this.handleError);
32
  }
33

    
34
  getRepositoriesOfUser(userEmail: string): Observable<Repository[]> {
35
    let url = `${this.apiUrl}getRepositoriesOfUser/${userEmail}/0/100`;
36
    console.log(`knocking on: ${url}`);
37
    return this.http.get(url)
38
      .map( res => <Repository[]>res.json())
39
      .do(res => console.log(`counted ${res.length} repositories`))
40
      .catch(this.handleError);
41
  }
42

    
43

    
44
  getRepositoryById(id: string): Observable<Repository> {
45
    let url = `${this.apiUrl}getRepositoryById/${id}`;
46
    console.log(`knocking on: ${url}`);
47
    return this.http.get(url)
48
      .map( res => <Repository>res.json())
49
      .do(res => console.log(`got repository with name: ${res.officialName}`))
50
      .catch(this.handleError);
51
  }
52

    
53
  getRepositoryInterface(id: string): Observable<RepositoryInterface[]>{
54
    let url = `${this.apiUrl}getRepositoryInterface/${id}`;
55
    console.log(`knocking on: ${url}`);
56
    return this.http.get(url)
57
      .map( res => <RepositoryInterface[]>res.json())
58
      .catch(this.handleError);
59
  }
60

    
61
  getUrlsOfUserRepos(userEmail: string): Observable<string[]>{
62
    let url = `${this.apiUrl}getUrlsOfUserRepos/${userEmail}/0/100/`;
63
    console.log(`knocking on: ${url}`);
64
    return this.http.get(url)
65
      .map( res => <string[]>res.json())
66
      .catch(this.handleError);
67
  }
68

    
69
  getTimezones(): Observable<Timezone[]>{
70
    let url = `${this.apiUrl}getTimezones`;
71
    console.log(`knocking on: ${url}`);
72
    return this.http.get(url)
73
      .map( res => <Timezone[]>res.json())
74
      .catch(this.handleError);
75
  }
76

    
77
  getTypologies(): Observable<string[]>{
78
    let url = `${this.apiUrl}getTypologies`;
79
    console.log(`knocking on: ${url}`);
80
    return this.http.get(url)
81
      .map( res => <string[]>res.json())
82
      .catch(this.handleError);
83
  }
84

    
85
  getCountries(): Observable<Country[]> {
86
    let url = `${this.apiUrl}getCountries`;
87
    console.log(`knocking on: ${url}`);
88
    return this.http.get(url)
89
      .map( res => <Country[]>res.json())
90
      .catch(this.handleError);
91
  }
92

    
93

    
94
  getCompatibilityClasses (mode: string): Observable<Map<string,string>> {
95
    let url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
96
    console.log(`knocking on: ${url}`);
97
    return this.http.get(url)
98
      .map( res => <Map<string,string>>res.json())
99
      .catch(this.handleError);
100
  }
101

    
102
  getDatasourceClasses(mode: string): Observable<Map<string,string>>{
103
    let url = `${this.apiUrl}getDatasourceClasses/${mode}`;
104
    console.log(`knocking on: ${url}`);
105
    return this.http.get(url)
106
      .map( res => <Map<string,string>>res.json())
107
      .catch(this.handleError);
108
  }
109

    
110

    
111
  getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
112
    let url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
113
    console.log(`knocking on: ${url}`);
114
    return this.http.get(url)
115
      .map( res => <MetricsInfo>res.json())
116
      .catch(this.handleError);
117
  }
118

    
119
  updateEnglishName(id: string, englishname: string): Observable<string>{
120
    let url = `${this.apiUrl}updateEnglishName?id=${id}&officialName=DSpace&englishname=${englishname}`;
121
    console.log(`knocking on: ${url}`);
122
    httpOptions.withCredentials = true;
123
    return this.http.post(url,httpOptions)
124
      .map( res => {
125
        console.log(`responded ${res.statusText}`);
126
        return res.status.toString();
127
      })
128
      .catch(this.handleError).share();
129
  }
130

    
131
  updateLongtitude(id: string, longtitude: string): Observable<string>{
132
    let url = `${this.apiUrl}updateLongtitude`;
133
    console.log(`knocking on: ${url}`);
134
    let body = JSON.stringify({
135
      id : id,
136
      logntitude: longtitude
137
    });
138
    console.log(`sending ${body}`);
139
    httpOptions.withCredentials = true;
140

    
141
    return this.http.post(url,body,httpOptions)
142
      .map( res => <string>res.json())
143
      .catch(this.handleError).share();
144
  }
145

    
146
  updateLatitude(id: string, latitude: string): Observable<string>{
147
    let url = `${this.apiUrl}updateLatitude`;
148
    console.log(`knocking on: ${url}`);
149
    let body = JSON.stringify({
150
      id : id,
151
      latitude: latitude
152
    });
153
    console.log(`sending ${body}`);
154
    httpOptions.withCredentials = true;
155

    
156
    return this.http.post(url,body,httpOptions)
157
      .map( res => <string>res.json())
158
      .catch(this.handleError).share();
159
  }
160

    
161
  updateLogoUrl(id: string, logoUrl: string): Observable<string>{
162
    let url = `${this.apiUrl}updateLogoUrl`;
163
    console.log(`knocking on: ${url}`);
164
    let body = JSON.stringify({
165
      id : id,
166
      logoUrl: logoUrl
167
    });
168
    console.log(`sending ${body}`);
169
    httpOptions.withCredentials = true;
170

    
171
    return this.http.post(url, body, httpOptions)
172
      .map( res => <string>res.json())
173
      .catch(this.handleError).share();
174
  }
175

    
176
  updateTimezone(id: string, timezone: string): Observable<string>{
177
    let url = `${this.apiUrl}updateTimezone`;
178
    console.log(`knocking on: ${url}`);
179
    let body = JSON.stringify({
180
      id : id,
181
      timezone: timezone
182
    });
183
    console.log(`sending ${body}`);
184
    httpOptions.withCredentials = true;
185

    
186
    return this.http.post(url, body, httpOptions)
187
      .map( res => <string>res.json())
188
      .catch(this.handleError).share();
189
  }
190

    
191

    
192
  private handleError(error: Response | any) {
193
    // In a real world app, we might use a remote logging infrastructure
194
    // We'd also dig deeper into the error to get a better message
195
    let errMsg = "";
196
    console.log('E R R O R !!');
197
    console.log(error);
198
    if (error instanceof Response) {
199
      const body = error.text() || '';
200
      //const err = body.error || JSON.stringify(body);
201
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
202
      console.log(errMsg);
203
    } else {
204
      errMsg = (error.message) ? error.message :
205
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
206
      console.error(errMsg); // log to console instead
207
    }
208
    return Observable.throw(errMsg);
209
  }
210

    
211
}
(7-7/8)