File

src/app/services/api.service.ts

Description

Servizio per la gestione delle chiamate alla API

Example

Index

Properties
Methods

Constructor

constructor(httpClient: HttpClient)

Creates an instance of ApiService.

Parameters :
Name Type Optional
httpClient HttpClient no

Methods

Public elaborateLookup
elaborateLookup(item: any)

Elaborazione delle lookup, estrae i figli

Parameters :
Name Type Optional
item any no
Returns : void
Public getLookUp
getLookUp(url: string)

Verifica la presenza di un singolo valore in session storage, se non esiste esegue la chiamata

Parameters :
Name Type Optional
url string no
Returns : any
Public getLookUpList
getLookUpList(itemArray: any[], itemTarget: any[])

Verifica la presenza del dato nel session storage, se non presente esegue la chiamata alle api.

Parameters :
Name Type Optional
itemArray any[] no
itemTarget any[] no
Returns : void
Public getUrl
getUrl(url: string)

Efettua una chiamata

Parameters :
Name Type Optional Default value
url string no null
Returns : Promise<any>
Public getUrlDetail
getUrlDetail(url: string)

Estrare un url per dettaglio utente

Parameters :
Name Type Optional
url string no
Returns : Promise<any>
Public getUrlPages
getUrlPages(page: number, obj: string)

Get Object paginated list

Parameters :
Name Type Optional Default value
page number no 1
obj string no
Returns : Promise<Page<any>>

Properties

Private BASE_PATH
BASE_PATH: string
Type : string
Default value : 'https://swapi.co/api/'

Base PATH API

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { People } from '../models/people.model';
import { Page } from '../models/page.model';
/**
 * Servizio per la gestione delle chiamate alla API
 *
 * @export
 * @class ApiService
 */
@Injectable({
  providedIn: 'root'
})
export class ApiService {
  /**
   * Base PATH API
   *
   * @private
   * @memberof ApiService
   */
  private BASE_PATH = 'https://swapi.co/api/';

  /**
   * Creates an instance of ApiService.
   * @param {HttpClient} httpClient
   * @memberof ApiService
   */
  constructor(private httpClient: HttpClient) {}
  /**
   * Efettua una chiamata
   *
   * @param {string} [url=null]
   * @returns {Promise<any>}
   * @memberof ApiService
   */
  public getUrl(url: string = null): Promise<any> {
    return this.httpClient
      .get<any>(this.BASE_PATH + (url ? url : ''))
      .toPromise();
  }

  /**
   * Get Object paginated list
   *
   * @param {number} [page=1]
   * @param {string} obj
   * @returns {Promise<Page<Any>>}
   * @memberof ApiService
   */
  public getUrlPages(page: number = 1, obj: string): Promise<Page<any>> {
    return this.httpClient
      .get<Page<People>>(this.BASE_PATH + obj + '?page=' + page)
      .toPromise();
  }
  /**
   * Estrare un url per dettaglio utente
   *
   * @param {string} url
   * @returns {Promise<any>}
   * @memberof ApiService
   */
  public getUrlDetail(url: string): Promise<any> {
    return this.httpClient.get<any>(url).toPromise();
  }

  /**
   *
   * Verifica la presenza di un singolo valore in session storage,
   * se non esiste esegue la chiamata
   *
   * @param {string} url
   * @returns {*}
   * @memberof ApiService
   */
  public getLookUp(url: string): any {
    if (sessionStorage.getItem(url)) {
      return JSON.parse(sessionStorage.getItem(url));
    } else {
      this.getUrlDetail(url).then(result => {
        sessionStorage.setItem(url, JSON.stringify(result));
        return result;
      });
    }
  }

  /**
   * Verifica la presenza del dato nel session storage, se non presente esegue la chiamata alle api.
   *
   * @private
   * @param {any[]} itemArray
   * @param {any[]} itemTarget
   */
  public getLookUpList(itemArray: any[], itemTarget: any[]) {
    itemArray.forEach(url => {
      if (sessionStorage.getItem(url)) {
        itemTarget.push(JSON.parse(sessionStorage.getItem(url)));
      } else {
        this.getUrlDetail(url).then(result => {
          itemTarget.push(result);
          sessionStorage.setItem(url, JSON.stringify(result));
        });
      }
    });
  }

  /**
   * Elaborazione delle lookup, estrae i figli
   *
   * @param {*} item
   * @returns
   * @memberof ApiService
   */
  public elaborateLookup(item: any) {
    if (!item) {
      return;
    }

    if (item.starships) {
      this.getLookUpList(item.starships, (item.starshipsObj = []));
    }

    if (item.films) {
      this.getLookUpList(item.films, (item.filmsObj = []));
    }
    if (item.species) {
      this.getLookUpList(item.species, (item.speciesObj = []));
    }
    if (item.vehicles) {
      this.getLookUpList(item.vehicles, (item.vehiclesObj = []));
    }
    if (item.residents) {
      this.getLookUpList(item.residents, (item.residentsObj = []));
    }
    if (item.pilots) {
      this.getLookUpList(item.pilots, (item.pilotsObj = []));
    }
    if (item.planets) {
      this.getLookUpList(item.planets, (item.planetsObj = []));
    }

    if (item.characters) {
      this.getLookUpList(item.characters, (item.charactersObj = []));
    }

    if (item.people) {
      this.getLookUpList(item.people, (item.peoplesObj = []));
    }

    if (item.homeworld) {
      item.homeworldObj = this.getLookUp(item.homeworld);
    }
  }
}

results matching ""

    No results matching ""