Last active
January 18, 2018 14:22
-
-
Save Rafael-rgsousa/1750b706467c42e42699bed9db8ddb8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Adicionando o provider | |
*/ | |
//app.module.ts | |
import { Http, HttpModule, XHRBackend, RequestOptions } from '@angular/http'; | |
{provide: Http, useFactory: httpFactory, deps: [XHRBackend, RequestOptions, LoadingController, ConexaoServicoProvider]}, | |
/* | |
Factory do Http | |
*/ | |
//http.factory.ts | |
import { XHRBackend, Http, RequestOptions } from "@angular/http"; | |
import { LoadingController } from 'ionic-angular'; | |
import { ConexaoServicoProvider } from '../providers/conexao.servico'; | |
import { HttpInterceptor } from "./http.interceptor"; | |
export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, | |
loadingController: LoadingController, conexaoServicoProvider: ConexaoServicoProvider): Http { | |
return new HttpInterceptor(xhrBackend, requestOptions, loadingController, conexaoServicoProvider); | |
} | |
/* | |
Interceptor | |
*/ | |
//http.interceptor.ts | |
import { Injectable } from "@angular/core"; | |
import { ConnectionBackend, RequestOptions, Request, RequestOptionsArgs, Response, Http, Headers} from "@angular/http"; | |
import { Observable } from "rxjs/Rx"; | |
import { LoadingController } from 'ionic-angular'; | |
import { ConexaoServicoProvider } from '../providers/conexao.servico'; | |
import 'rxjs/Rx'; | |
import 'rxjs/add/operator/finally'; | |
import 'rxjs/add/operator/catch'; | |
@Injectable() | |
export class HttpInterceptor extends Http { | |
private loading; | |
private requestsPendentes:number = 0; | |
//private time1; | |
//private time2; | |
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, | |
private loadingCtrl: LoadingController, private conexao: ConexaoServicoProvider) { | |
super(backend, defaultOptions); | |
} | |
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { | |
return this.interceptor(super.request(url, options)); | |
} | |
get(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this.interceptor(super.get(url, this.getRequestOptionArgs(options))); | |
} | |
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this.interceptor(super.post(url, body, this.getRequestOptionArgs(options))); | |
} | |
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this.interceptor(super.put(url, body, this.getRequestOptionArgs(options))); | |
} | |
delete(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this.interceptor(super.delete(url, this.getRequestOptionArgs(options))); | |
} | |
private getRequestOptionArgs(options?: RequestOptionsArgs) : RequestOptionsArgs { | |
if (options == null) { | |
options = new RequestOptions(); | |
} | |
if (options.headers == null) { | |
options.headers = new Headers(); | |
} | |
if(options.headers.get('Content-Type') == null){ | |
options.headers.append('Content-Type', 'application/json'); | |
} | |
//Token de autorizacao | |
if(options.headers.get('Authorization') == null){ | |
options.headers.append('Authorization', 'Bearer ' + token); | |
} | |
return options; | |
} | |
private interceptor(response:Observable<Response>):Observable<Response>{ | |
if(!this.conexao.isConectado()){ | |
let erro:any = new Object(); | |
erro.status=600; | |
return Observable.throw(erro); | |
} | |
this.carregando(); | |
return response | |
//caso queira adicionar um limite de tempo para a requisicação | |
/*.timeout(60000) | |
.catch(err => { | |
console.log(err.json().error); | |
if (err.name == "TimeoutError") { | |
alert('Sem resposta'); | |
this.removerCarregando(); | |
return Observable.never(); | |
} | |
return Observable.throw(err); | |
})*/ | |
.finally( | |
() => { | |
this.removerCarregando(); | |
} | |
); | |
} | |
private carregando(){ | |
this.requestsPendentes++; | |
if(this.loading != null){ | |
return null; | |
} | |
this.loading = this.loadingCtrl.create(); | |
this.loading.present(); | |
} | |
private removerCarregando(){ | |
this.requestsPendentes--; | |
if(this.loading != null && this.requestsPendentes == 0){ | |
this.loading.dismiss(); | |
this.loading = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment