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, Repository, RepositoryInterface, Timezone, Topic } from '../domain/typeScriptClasses';
15
import 'rxjs/add/operator/map';
16
import { Http, Response } from '@angular/http';
17

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

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

    
27
  constructor(private http: Http) { }
28

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

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

    
46

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

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

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

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

    
80
  getTopicsForDataSource(name: string): Observable<Topic[]> {
81
    let url = `${this.apiUrl}/broker/getTopicsForDatasource/${name}`;
82
    console.log(`knocking on: ${url}`);
83
    return this.http.get(url)
84
      .map( res => <Topic[]>res.json())
85
      .catch(this.handleError);
86
  }
87

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

    
96
  private handleError(error: Response | any) {
97
    // In a real world app, we might use a remote logging infrastructure
98
    // We'd also dig deeper into the error to get a better message
99
    let errMsg = "";
100
    console.log(error);
101
    if (error instanceof Response) {
102
      const body = error.text() || '';
103
      //const err = body.error || JSON.stringify(body);
104
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
105
    } else {
106
      errMsg = (error.message) ? error.message :
107
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
108
      console.error(errMsg); // log to console instead
109
    }
110
    return Observable.throw(errMsg);
111
  }
112
}
(5-5/6)