Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts"/> |
| 2 | |
| 3 | // Imports |
| 4 | import {AppConfig} from '../../config/app.config'; |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame^] | 5 | import {Injectable} from '@angular/core'; |
| 6 | import {Response} from '@angular/http'; |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 7 | import {Observable} from 'rxjs/Rx'; |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame^] | 8 | import {XosHttp} from './xoshttp.service'; |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 9 | |
| 10 | // Import RxJs required methods |
| 11 | import 'rxjs/add/operator/map'; |
| 12 | import 'rxjs/add/operator/catch'; |
| 13 | |
| 14 | @Injectable() |
| 15 | export class CoreService { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 16 | // private instance variable to hold base url |
| 17 | private baseUrl = AppConfig.apiEndpoint; |
| 18 | |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame^] | 19 | // Resolve HTTP using the constructor |
| 20 | constructor (private http: XosHttp) {} |
| 21 | |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 22 | // Fetch all existing comments |
| 23 | getCoreEndpoints() : Observable<any[]> { |
| 24 | |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame^] | 25 | const search = 'some=param'; |
| 26 | |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 27 | // ...using get request |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame^] | 28 | return this.http.get(`${this.baseUrl}/core/`, {search}) |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 29 | // ...and calling .json() on the response to return data |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame^] | 30 | .map((res: Response) => res.json()) |
| 31 | // ...errors if any |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 32 | .catch((error:any) => Observable.throw(error.json().error || 'Server error')); |
| 33 | |
| 34 | } |
| 35 | } |