Project

General

Profile

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

    
5
import { Injectable } from '@angular/core';
6
import { Observable } from 'rxjs/Observable';
7
import 'rxjs/add/operator/map';
8
import { Http, Response, Headers, RequestOptions } from '@angular/http';
9

    
10
import {
11
  AdvQueryObject, BrowseEntry, EventsPage, OpenaireSubscription, Repository, SimpleSubscriptionDesc, Subscription,
12
  Term
13
} from '../domain/typeScriptClasses';
14

    
15

    
16
let headers = new Headers({ 'Content-Type': 'application/json' });
17
let httpOptions = new RequestOptions({ headers: headers });
18

    
19
@Injectable ()
20
export class BrokerService {
21
  /*private apiUrl = apiUrl + '/broker/';*/
22
  private apiUrl = process.env.API_ENDPOINT + '/broker/';
23

    
24
  constructor(private http: Http) { }
25

    
26
  advancedShowEvents(page: number,size: number,searchParams: AdvQueryObject): Observable<EventsPage>{
27
    let url = `${this.apiUrl}advancedShowEvents/${page}/${size}`;
28
    console.log(`knocking on: ${url}`);
29
    let body = searchParams;
30
    console.log(`sending ${JSON.stringify(body)}`);
31
    httpOptions.withCredentials = true;
32
    return this.http.post(url,body,httpOptions)
33
      .map( res => <EventsPage>res.json())
34
      .catch(this.handleError).share();
35
  }
36

    
37
  getDatasourcesOfUser(userEmail: string) {
38
    let url = `${this.apiUrl}getDatasourcesOfUser?user=${userEmail}&includeShared=false&includeByOthers=false`;
39
    console.log(`knocking on: ${url}`);
40

    
41
    httpOptions.withCredentials = true;
42
    return this.http.get(url,httpOptions)
43
      .map( res => res.json())
44
      .catch(this.handleError).share();
45
  }
46

    
47
  getDnetTopics(): Observable<Map<string,Term>> {
48
    let url = `${this.apiUrl}getDnetTopics`;
49
    console.log(`knocking on: ${url}`);
50
    httpOptions.withCredentials = true;
51
    return this.http.get(url, httpOptions)
52
      .map( res => <Map<string,Term>>res.json())
53
      .catch(this.handleError);
54
  }
55

    
56
  getNotificationsBySubscriptionId(subId: string, page: number, size: number): Observable<EventsPage> {
57
    let url = `${this.apiUrl}getNotificationsBySubscriptionId/${subId}/${page}/${size}`;
58
    console.log(`knocking on: ${url}`);
59
    httpOptions.withCredentials = true;
60
    return this.http.get(url, httpOptions)
61
      .map( res => <EventsPage>res.json())
62
      .catch(this.handleError);
63
  }
64

    
65
  getSimpleSubscriptionsOfUser(userEmail: string): Observable<Map<string,SimpleSubscriptionDesc[]>> {
66
    let url = `${this.apiUrl}getSimpleSubscriptionsOfUser/${userEmail}/`;
67
    console.log(`knocking on: ${url}`);
68
    httpOptions.withCredentials = true;
69
    return this.http.get(url, httpOptions)
70
      .map( res => <Map<string,SimpleSubscriptionDesc[]>>res.json())
71
      .catch(this.handleError);
72
  }
73

    
74
  getSubscription(subId: string): Observable<Subscription> {
75
    let url = `${this.apiUrl}getSubscription/${subId}`;
76
    console.log(`knocking on: ${url}`);
77
    httpOptions.withCredentials = true;
78
    return this.http.get(url,httpOptions)
79
      .map( res => <Subscription>res.json())
80
      .catch(this.handleError);
81
  }
82

    
83
  getSubscriptionsOfUser(userEmail: string): Observable<Map<string, Subscription>> {
84
    let url = `${this.apiUrl}getSubscriptionsOfUser/${userEmail}/`;
85
    console.log(`knocking on: ${url}`);
86
    httpOptions.withCredentials = true;
87
    return this.http.get(url, httpOptions)
88
      .map( res => <Map<string,Subscription>>res.json())
89
      .catch(this.handleError);
90
  }
91

    
92
  getTopicsForDataSource(name: string): Observable<BrowseEntry[]> {
93
    let url = `${this.apiUrl}getTopicsForDatasource/${name}`;
94
    console.log(`knocking on: ${url}`);
95
    httpOptions.withCredentials = true;
96
    return this.http.get(url, httpOptions)
97
      .map( res => <BrowseEntry[]>res.json())
98
      .catch(this.handleError);
99
  }
100

    
101
/* NOT WORKING AND PROBABLY NOT NEEDED
102
  showEvents(repoName: string, topic: string, page: number): Observable<EventsPage> {
103
    let url = `${this.apiUrl}showEvents/{datasourceName}/{topic}/{page}?datasourceName=${repoName}&topic=${topic}&page=${page}`;
104
    console.log(`knocking on: ${url}`);
105
    return this.http.get(url)
106
      .map( res => <EventsPage>res.json())
107
      .catch(this.handleError);
108
  }
109
*/
110

    
111
  /*CHECK IF sub is sent as body*/
112
  subscribeToEvent(sub: OpenaireSubscription): Observable<string>{
113
    let url = `${this.apiUrl}subscribe`;
114
    console.log(`knocking on: ${url}`);
115
    httpOptions.withCredentials = true;
116
    return this.http.post(url,sub,httpOptions)
117
      .map( res => res.status.toString())
118
      .catch(this.handleError);
119
  }
120

    
121
  unsubscribe(subscriptionId: string): Observable<string> {
122
    let url = `${this.apiUrl}unsubscribe/${subscriptionId}`;
123
    console.log(`knocking on: ${url}`);
124
    httpOptions.withCredentials = true;
125
    return this.http.post(url,httpOptions)
126
      .map( res => res.status.toString())
127
      .catch(this.handleError);
128
}
129

    
130

    
131
private handleError(error: Response | any) {
132
    // In a real world app, we might use a remote logging infrastructure
133
    // We'd also dig deeper into the error to get a better message
134
    let errMsg = "";
135
    console.log('E R R O R !!!');
136
    console.log(error);
137
    if (error instanceof Response) {
138
      const body = error.text() || '';
139
      //const err = body.error || JSON.stringify(body);
140
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
141
      console.log(errMsg);
142
    } else {
143
      errMsg = (error.message) ? error.message :
144
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
145
      console.error(errMsg); // log to console instead
146
    }
147
    return Observable.throw(errMsg);
148
  }
149

    
150
}
(3-3/8)