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 { HttpHeaders } from '@angular/common/http';
11

    
12
import { Observable } from 'rxjs/Observable';
13

    
14
import { Country, PiwikInfo, Repository, RepositoryInterface, Timezone, Topic } from '../domain/typeScriptClasses';
15
import 'rxjs/add/operator/map';
16
import { Http, Response } from '@angular/http';
17
import { AuthenticationService } from './authentication.service';
18

    
19
const httpOptions = {
20
  headers: new HttpHeaders().set('Content-Type', 'application/json')
21
};
22

    
23
@Injectable ()
24
export class RepositoryService {
25
/*  private apiUrl = 'http://195.134.66.230:8380/uoa-repository-manager-service';*/
26
  private apiUrl = 'http://194.177.192.121:8380/uoa-repository-manager-service';
27

    
28
  constructor(private http: Http) { }
29

    
30
  getRepositoriesOfCountry(country: string, mode: string): Observable<Repository[]> {
31
    let url = `${this.apiUrl}/repository/getRepositoriesByCountry/${country}/${mode}`;
32
    console.log(`knocking on: ${url}`);
33
    return this.http.get(url)
34
      .map( res => <Repository[]>res.json())
35
      .catch(this.handleError);
36
  }
37

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

    
47

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

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

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

    
73
  getTimezones(): Observable<Timezone[]>{
74
    let url = `${this.apiUrl}/repository/getTimezones`;
75
    console.log(`knocking on: ${url}`);
76
    return this.http.get(url)
77
      .map( res => <Timezone[]>res.json())
78
      .catch(this.handleError);
79
  }
80

    
81
  getPiwikInfo(id: string): Observable<PiwikInfo> {
82
    let url = `${this.apiUrl}/piwik/getPiwikSiteForRepo/${id}`;
83
    console.log(`knocking on: ${url}`);
84
    return this.http.get(url)
85
      .map( piwik => <PiwikInfo>piwik.json() )
86
      .catch(this.handleError);
87
  }
88

    
89
  getTopicsForDataSource(name: string): Observable<Topic[]> {
90
    let url = `${this.apiUrl}/broker/getTopicsForDatasource/${name}`;
91
    console.log(`knocking on: ${url}`);
92
    return this.http.get(url)
93
      .map( res => <Topic[]>res.json())
94
      .catch(this.handleError);
95
  }
96

    
97
  getCountries(): Observable<Country[]> {
98
    let url = `${this.apiUrl}/repository/getCountries`;
99
    console.log(`knocking on: ${url}`);
100
    return this.http.get(url)
101
      .map( res => <Country[]>res.json())
102
      .catch(this.handleError);
103
  }
104

    
105
  getPiwikSitesForRepos(): Observable<PiwikInfo[]> {
106
    let url = `${this.apiUrl}/piwik/getPiwikSitesForRepos`;
107
    console.log(`knocking on: ${url}`);
108
    return this.http.get(url)
109
      .map( res => <PiwikInfo[]>res.json())
110
      .catch(this.handleError);
111
  }
112

    
113
  private handleError(error: Response | any) {
114
    // In a real world app, we might use a remote logging infrastructure
115
    // We'd also dig deeper into the error to get a better message
116
    let errMsg = "";
117
    console.log(error);
118
    if (error instanceof Response) {
119
      const body = error.text() || '';
120
      //const err = body.error || JSON.stringify(body);
121
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
122
    } else {
123
      errMsg = (error.message) ? error.message :
124
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
125
      console.error(errMsg); // log to console instead
126
    }
127
    return Observable.throw(errMsg);
128
  }
129
}
(4-4/5)