Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3.  
  4. import { Observable, of } from 'rxjs';
  5. import { catchError } from 'rxjs/operators';
  6.  
  7. import {IMovieModel} from '../model/movie.model';
  8.  
  9.  
  10.  
  11. @Injectable({ providedIn: 'root' })
  12. export class HttpService {
  13.  
  14.   private baseUrl = 'http://www.mocky.io/v2/'; //TODO not working for GitHub Pages
  15.   private movieID = '5dceddf43000005500931d09';
  16.   private tagsID = '5dcede30300000fd9f931d0b';
  17.   private moviesLocal = 'assets/movie.json';
  18.   private tagsLocal = 'assets/tags.json';
  19.  
  20.   constructor(
  21.     private http: HttpClient) {
  22.   }
  23.  
  24.   getMovies(): Observable<IMovieModel[]> {
  25.     return this.http.get<IMovieModel[]>(this.moviesLocal)
  26.       .pipe(
  27.         catchError(this.handleError<IMovieModel[]>('getMovies', []))
  28.       );
  29.   }
  30.  
  31.   getTags(): Observable<string[]> {
  32.     return this.http.get<string[]>(this.tagsLocal)
  33.       .pipe(
  34.         catchError(this.handleError<string[]>('getTags', []))
  35.       );
  36.   }
  37.  
  38.   /**
  39.    * Handle Http operation that failed.
  40.    * Let the app continue.
  41.    * @param operation - name of the operation that failed
  42.    * @param result - optional value to return as the observable result
  43.    */
  44.   private handleError<T>(operation = 'operation', result?: T) {
  45.     return (error: any): Observable<T> => {
  46.  
  47.       // TODO: send the error to remote logging infrastructure
  48.       console.error(error); // log to console instead
  49.  
  50.       // Let the app keep running by returning an empty result.
  51.       return of(result as T);
  52.     };
  53.   }
  54.  
  55. }
  56.  
  57.  
  58. /*
  59. Copyright Google LLC. All Rights Reserved.
  60. Use of this source code is governed by an MIT-style license that
  61. can be found in the LICENSE file at http://angular.io/license
  62. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement