-
Notifications
You must be signed in to change notification settings - Fork 12
/
web-api-observable.service.ts
124 lines (109 loc) · 4.24 KB
/
web-api-observable.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Observable';
// Observable class extensions
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/throw';
// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
@Injectable()
export class WebApiObservableService {
headers: Headers;
options: RequestOptions;
constructor(private http: Http,
private toasterService: ToasterService,
private loaderService: LoaderService) {
this.headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'q=0.8;application/json;q=0.9' });
this.options = new RequestOptions({ headers: this.headers });
}
getService(url: string): Observable<any> {
return this.http
.get(url, this.options)
.map(this.extractData)
.catch(this.handleError);
}
getServiceWithDynamicQueryTerm(url: string, key: string, val: string): Observable<any> {
return this.http
.get(url + "/?" + key + "=" + val, this.options)
.map(this.extractData)
.catch(this.handleError);
}
getServiceWithFixedQueryString(url: string, param: any): Observable<any> {
this.options = new RequestOptions({ headers: this.headers, search: 'query=' + param });
return this.http
.get(url, this.options)
.map(this.extractData)
.catch(this.handleError);
}
getServiceWithComplexObjectAsQueryString(url: string, param: any): Observable<any> {
let params: URLSearchParams = new URLSearchParams();
for (var key in param) {
if (param.hasOwnProperty(key)) {
let val = param[key];
params.set(key, val);
}
}
this.options = new RequestOptions({ headers: this.headers, search: params });
return this.http
.get(url, this.options)
.map(this.extractData)
.catch(this.handleError);
}
createService(url: string, param: any): Observable<any> {
let body = JSON.stringify(param);
return this.http
.post(url, body, this.options)
.map(this.extractData)
.catch(this.handleError);
}
updateService(url: string, param: any): Observable<any> {
let body = JSON.stringify(param);
return this.http
.put(url, body, this.options)
.map(this.extractData)
.catch(this.handleError);
}
patchService(url: string, param: any): Observable<any> {
let body = JSON.stringify(param);
return this.http
.patch(url, body, this.options)
.map(this.extractData)
.catch(this.handleError);
}
deleteService(url: string, param: any): Observable<any> {
let params: URLSearchParams = new URLSearchParams();
for (var key in param) {
if (param.hasOwnProperty(key)) {
let val = param[key];
params.set(key, val);
}
}
this.options = new RequestOptions({ headers: this.headers, search: params });
return this.http
.delete(url, this.options)
.map(this.extractData)
.catch(this.handleError);
}
deleteServiceWithId(url: string, key: string, val: string): Observable<any> {
return this.http
.delete(url + "/?" + key + "=" + val, this.options)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
}