Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6883

Avoid duplicate api calls angular2

I have a api call to categories and everything works fine. Now if i need categories in another page its making a call again whereas i need to store it locally. I just followed the approach i used to practise in angular1 but its throwing error. The commented lines are my trials.

private categories: any;
  constructor(private http: Http) { }

  getCategories(): Promise<any> {
    // if(!this.categories){
    return this.http.get(environment.API_ENDPOINT + 'categories')
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
    // }else{
    //   console.log("call blocked");
    //   return Promise.resolve(this.categories);
    // }

  }


  private extractData(res: Response) {
    let body = res.json();
    //  this.categories = body || [];
    return body;
    //return this.categories;
  }

  private handleError(error: any): Promise<any> {

    return Promise.reject(JSON.parse(error.message || error._body || error));
  }

How to store the api received data so i call the service, it will check for the previous data, and if not available, it will fetch data from server.

Thanks in advance.

Trying with observable and of

import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of'; //updating import library, which threw error
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/publishReplay'
import {Cities} from '../interface/cities';
import { environment } from '../../environments/environment';
@Injectable()
export class CityService {
  private endPoint = environment.API_URL+'cities';
  private data:any;
  constructor(
    private http: Http) { }

   getAll (): Observable<Cities[]> {
     if(this.data){
        return Observable.of(this.data); // 
      }
    return this.http.get(this.endPoint)
                    .map(this.extractData)
                    .catch(this.handleError);
  }
  private extractData(res: Response) {
    let body = res.json();
    this.data = body || body.data || { };
    return this.data;
  }
  private handleError (error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

}

Tried with publish count

import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/publishReplay'
import {Cities} from '../interface/cities';
import { environment } from '../../environments/environment';
@Injectable()
export class CityService {
  private endPoint = environment.API_URL+'cities';
  private data:any;
  constructor(
    private http: Http) { }

   getAll (): Observable<Cities[]> {
    return this.http.get(this.endPoint)
                    .map(this.extractData)
                    .publishReplay(1) // does not stop duplicate calls
                    .refCount() // does not stop duplicate calls
                    .catch(this.handleError);
  }
  private extractData(res: Response) {
    let body = res.json();
    this.data = body || body.data || { };
    return this.data;
  }
  private handleError (error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

}

Upvotes: 0

Views: 3674

Answers (1)

Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6883

Atlast after 2 days of struggle i found only this as a working solution to me.

Though i posted a question with cities, this is with categories

import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import { Cities } from '../interface/cities';
import { environment } from '../../environments/environment';
import { LocalStorageService } from 'angular-2-local-storage';
@Injectable()
export class CityService {
  private endPoint = environment.API_URL + 'cities';
  private data: any;
  constructor(
    private http: Http,
    private localStorageService: LocalStorageService) { }

  getAll(): Observable<Cities[]> {
    if (this.localStorageService.get('cities')) {
      return Observable.of(this.localStorageService.get('cities'));
    } else {
      return this.http.get(this.endPoint)
        .map(this.extractData)
        .do((data) => this.localStorageService.set("cities", data))
        .catch(this.handleError);
    }
  }
  private extractData(res: Response) {
    let body = res.json();
    this.data = body || body.data || {};

    //this.localStorage.setItems$.subscribe("cities", "Hello world");
    return this.data;
  }
  private handleError(error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

}

Upvotes: 1

Related Questions