Project

General

Profile

1
/*
2
* Created by myrto on 12/05/2017
3
*/
4

    
5
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
6
import { Injectable } from '@angular/core';
7
import { environment } from '../../environments/environment';
8
import {
9
  AggregationDetails,
10
  Country, MetricsInfo,
11
  Repository,
12
  RepositoryInterface,
13
  RepositorySnippet, RepositorySummaryInfo,
14
  Timezone,
15
  Typology
16
} from '../domain/typeScriptClasses';
17
import { Observable, of } from 'rxjs';
18
import { timezones } from '../domain/timezones';
19
import { typologies } from '../domain/typologies';
20
import {URLParameter} from '../domain/url-parameter';
21

    
22
const headerOptions = {
23
  headers : new HttpHeaders().set('Content-Type', 'application/json')
24
                             .set('Accept', 'application/json'),
25
  withCredentials: true
26
};
27

    
28

    
29
@Injectable ()
30
export class RepositoryService {
31
  private apiUrl = environment.API_ENDPOINT + '/repository/';
32
  private dashboardAPIUrl = environment.API_ENDPOINT + '/dashboard/';
33

    
34
  constructor(private httpClient: HttpClient) { }
35

    
36
  addInterface(datatype: string, repoId: string, registeredBy: string, comment: string, newInterface: RepositoryInterface): Observable<RepositoryInterface> {
37
    const url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}&registeredBy=${registeredBy}&comment=${comment}`;
38
    console.log(`knocking on: ${url}`);
39
    console.log(`sending ${JSON.stringify(newInterface)}`);
40
    return this.httpClient.post<RepositoryInterface>(url, newInterface, headerOptions);
41
  }
42

    
43
  updateInterface(repoId: string, registeredBy: string, comment: string, interfaceInfo: RepositoryInterface): Observable<RepositoryInterface> {
44
    const url = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}&registeredBy=${registeredBy}&comment=${comment}`;
45
    console.log(`knocking on: ${url}`);
46
    console.log(`sending ${JSON.stringify(interfaceInfo)}`);
47
    return this.httpClient.post<RepositoryInterface>(url, interfaceInfo, headerOptions);
48
  }
49

    
50
  deleteInterface(id: string, registeredBy: string) {
51
    const url = `${this.apiUrl}deleteInterface/?id=${id}&registeredBy=${registeredBy}`;
52
    console.log(`knocking on: ${url}`);
53

    
54
    return this.httpClient.delete(url, {withCredentials: true, responseType: 'text'});
55
  }
56

    
57
  addRepository(datatype: string, newRepository: Repository): Observable<Repository> {
58
    const url = `${this.apiUrl}addRepository?datatype=${datatype}`;
59
    console.log(`knocking on: ${url}`);
60
    console.log(`sending ${JSON.stringify(newRepository)}`);
61
    return this.httpClient.post<Repository>(url, newRepository, headerOptions);
62
  }
63

    
64
  // updateRepository(repoInfo: Repository): Observable<Repository> {
65
  //   const url = `${this.apiUrl}updateRepository`;
66
  //   console.log(`knocking on: ${url}`);
67
  //   console.log(`sending ${JSON.stringify(repoInfo)}`);
68
  //   return this.httpClient.post<Repository>(url, repoInfo, headerOptions);
69
  // }
70

    
71
  updateRepository(repoInfo: Repository): Observable<Repository> {
72
    const url = `${this.apiUrl}updateRepository`;
73
    console.log(`knocking on: ${url}`);
74
    console.log(`sending ${JSON.stringify(repoInfo)}`);
75
    return this.httpClient.post<Repository>(url, repoInfo, headerOptions);
76
  }
77

    
78
  getRepositoriesOfCountry(country: string, mode: string): Observable<RepositorySnippet[]> {
79
    const url = `${this.apiUrl}getRepositoriesByCountry/${country}/${mode}`;
80
    console.log(`knocking on: ${url}`);
81
    return this.httpClient.get<RepositorySnippet[]>(url, headerOptions);
82
  }
83

    
84
  getRepositoriesOfUser(): Observable<RepositorySnippet[]> {
85
    const url = `${this.apiUrl}getRepositoriesOfUser/0/100`;
86
    console.log(`knocking on: ${url}`);
87
    return this.httpClient.get<RepositorySnippet[]>(url, headerOptions);
88
  }
89

    
90

    
91
  getRepositoryById(id: string): Observable<Repository> {
92
    const url = `${this.apiUrl}getRepositoryById/${id}`;
93
    console.log(`knocking on: ${url}`);
94
    return this.httpClient.get<Repository>(url, headerOptions);
95
  }
96

    
97
  getRepositoryInterface(id: string): Observable<RepositoryInterface[]> {
98
    const url = `${this.apiUrl}getRepositoryInterface/${id}`;
99
    console.log(`knocking on: ${url}`);
100
    return this.httpClient.get<RepositoryInterface[]>(url, headerOptions);
101
  }
102

    
103

    
104
  getUrlsOfUserRepos(): Observable<string[]> {
105
    const url = `${this.apiUrl}getUrlsOfUserRepos/0/100/`;
106
    console.log(`knocking on: ${url}`);
107
    return this.httpClient.get<string[]>(url, headerOptions);
108
  }
109

    
110
  getRepositoryAggregations(id: string): Observable<AggregationDetails[]> {
111
    const url = `${this.apiUrl}getRepositoryAggregations/${id}`;
112
    console.log(`knocking on: ${url}`);
113
    return this.httpClient.get<AggregationDetails[]>(url, headerOptions);
114
  }
115

    
116
  getRepositoryAggregationsByYear(id: string): Observable<Map<string, AggregationDetails[]>> {
117
    const url = `${this.apiUrl}getRepositoryAggregationsByYear/${id}`;
118
    console.log(`knocking on: ${url}`);
119
    return this.httpClient.get<Map<string, AggregationDetails[]>>(url, headerOptions);
120
  }
121

    
122
  getTimezones(): Observable<Timezone[]> {
123
/*    const url = `${this.apiUrl}getTimezones`;
124
    console.log(`knocking on: ${url}`);
125
    return this.httpClient.get<Timezone[]>(url, headerOptions);*/
126
    return of(<Timezone[]>timezones);
127
  }
128

    
129
  getTypologies(): Observable<Typology[]> {
130
/*    const url = `${this.apiUrl}getTypologies`;
131
    console.log(`knocking on: ${url}`);
132
    return this.httpClient.get<Typology[]>(url, headerOptions);*/
133
    return of(<Typology[]>typologies);
134
  }
135

    
136
  getCountries(): Observable<Country[]> {
137
    const url = `${this.apiUrl}getCountries`;
138
    console.log(`knocking on: ${url}`);
139
    return this.httpClient.get<Country[]>(url, headerOptions);
140
  }
141

    
142

    
143
  getCompatibilityClasses (mode: string): Observable<Map<string, string>> {
144
    const url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
145
    console.log(`knocking on: ${url}`);
146
    return this.httpClient.get<Map<string, string>>(url, headerOptions);
147
  }
148

    
149
  getDatasourceClasses(mode: string): Observable<Map<string, string>> {
150
    const url = `${this.apiUrl}getDatasourceClasses/${mode}`;
151
    console.log(`knocking on: ${url}`);
152
    return this.httpClient.get<Map<string, string>>(url, headerOptions);
153
  }
154

    
155

    
156
  getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
157
    const url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
158
    console.log(`knocking on: ${url}`);
159
    return this.httpClient.get<MetricsInfo>(url, headerOptions);
160
  }
161

    
162
  getListLatestUpdate(mode: string): Observable<any> {
163
    const url = `${this.apiUrl}getListLatestUpdate/${mode}`;
164
    console.log(`knocking on: ${url}`);
165
    return this.httpClient.get<any>(url, headerOptions);
166
  }
167

    
168
  searchRegisteredRepositories(page, size, urlParams: URLParameter[]) {
169
    const url = `${this.apiUrl}searchRegisteredRepositories/${page}/${size}`;
170
    console.log(`knocking on: ${url}`);
171
    let params = new HttpParams();
172
    for (const urlParameter of urlParams) {
173
      for (const value of urlParameter.value) {
174
        params = params.append(urlParameter.key, value);
175
      }
176
    }
177

    
178
    return this.httpClient.get<RepositorySnippet[]>(url, {params, withCredentials: true});
179
  }
180

    
181
  getRepositoriesSummaryInfo(): Observable<RepositorySummaryInfo[]> {
182
    const url = `${this.dashboardAPIUrl}getRepositoriesSummary/0/100`;
183
    console.log(`knocking on: ${url}`);
184
    return this.httpClient.get<RepositorySummaryInfo[]>(url, headerOptions);
185
  }
186
}
(9-9/13)