src/app/services/api.service.ts
Servizio per la gestione delle chiamate alla API
Properties |
|
Methods |
|
constructor(httpClient: HttpClient)
|
||||||
Defined in src/app/services/api.service.ts:21
|
||||||
Creates an instance of ApiService.
Parameters :
|
Public elaborateLookup | ||||||
elaborateLookup(item: any)
|
||||||
Defined in src/app/services/api.service.ts:113
|
||||||
Elaborazione delle lookup, estrae i figli
Parameters :
Returns :
void
|
Public getLookUp | ||||||
getLookUp(url: string)
|
||||||
Defined in src/app/services/api.service.ts:75
|
||||||
Verifica la presenza di un singolo valore in session storage, se non esiste esegue la chiamata
Parameters :
Returns :
any
|
Public getLookUpList | |||||||||
getLookUpList(itemArray: any[], itemTarget: any[])
|
|||||||||
Defined in src/app/services/api.service.ts:93
|
|||||||||
Verifica la presenza del dato nel session storage, se non presente esegue la chiamata alle api.
Parameters :
Returns :
void
|
Public getUrl | ||||||||
getUrl(url: string)
|
||||||||
Defined in src/app/services/api.service.ts:36
|
||||||||
Efettua una chiamata
Parameters :
Returns :
Promise<any>
|
Public getUrlDetail | ||||||
getUrlDetail(url: string)
|
||||||
Defined in src/app/services/api.service.ts:62
|
||||||
Estrare un url per dettaglio utente
Parameters :
Returns :
Promise<any>
|
Public getUrlPages |
getUrlPages(page: number, obj: string)
|
Defined in src/app/services/api.service.ts:50
|
Get Object paginated list
Returns :
Promise<Page<any>>
|
Private BASE_PATH |
BASE_PATH:
|
Type : string
|
Default value : 'https://swapi.co/api/'
|
Defined in src/app/services/api.service.ts:21
|
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);
}
}
}