Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import {Http, Request, RequestOptionsArgs, Response, Headers} from '@angular/http';
  3. import { Observable } from 'rxjs';
  4. import { CacheService } from "ionic-cache";
  5. import { Helper } from '../pages/helper';
  6. import { AuthService } from './auth.service';
  7.  
  8. @Injectable()
  9. export class HttpService {
  10.     constructor(private helper: Helper,
  11.                 private auth: AuthService,
  12.                 private http: Http,
  13.                 private cache: CacheService) {}
  14.  
  15.     request(url: string | Request, options?: RequestOptionsArgs): Observable<any> {
  16.         return this.intercept(this.http.request(url, this.getRequestOptionArgs(options)));
  17.     }
  18.  
  19.     get(url: string, options?: RequestOptionsArgs, cache?:object): Observable<any> {
  20.         if (cache) {
  21.             const groupKey = cache['groupKey'] || 'default';
  22.             const ttl = cache['ttl'] || 60 * 60 * 24 * 7;
  23.             const cacheKey = url;
  24.             const request = this.intercept(this.http.get(url,this.getRequestOptionArgs(options)));
  25.             return this.cache.loadFromObservable(cacheKey, request, groupKey, ttl);
  26.         }
  27.         return this.intercept(this.http.get(url, this.getRequestOptionArgs(options)));
  28.     }
  29.  
  30.     post(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
  31.         return this.intercept(this.http.post(url, body, this.getRequestOptionArgs(options)));
  32.     }
  33.  
  34.     put(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
  35.         return this.intercept(this.http.put(url, body, this.getRequestOptionArgs(options)));
  36.     }
  37.  
  38.     delete(url: string, options?: RequestOptionsArgs): Observable<any> {
  39.         return this.intercept(this.http.delete(url, this.getRequestOptionArgs(options)));
  40.     }
  41.  
  42.     getRequestOptionArgs(options?: RequestOptionsArgs) : RequestOptionsArgs {
  43.         if (!options) {
  44.             options = {
  45.                 headers: new Headers()
  46.             }
  47.         }
  48.  
  49.         let token = this.auth.getToken();
  50.  
  51.         if (token) {
  52.             options.headers.set("Authorization", "JWT " + token);
  53.         }
  54.  
  55.         options.headers.set('Content-Type', 'application/json');
  56.  
  57.         return options;
  58.     }
  59.  
  60.     intercept(observable: Observable<Response>): Observable<any> {
  61.         return observable
  62.             .map(response => response.text() ? response.json() : response.text())
  63.             .catch((err, source) => {
  64.                 const errJson = err.json();
  65.  
  66.                 if (errJson && errJson.message) {
  67.                     this.helper.alertWarn(errJson.message);
  68.                 } else if (err.status != 401 && err.status != 403 && err.status != 404) {
  69.                     this.helper.alertWarn("Um erro inesperado ocorreu.");
  70.                 }
  71.  
  72.                 return Observable.throw(err);
  73.             });
  74.     }
  75.  
  76.     serialize(obj) {
  77.         let str = [];
  78.  
  79.         for(let p in obj) {
  80.             if (obj.hasOwnProperty(p) && obj[p]) {
  81.                 str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
  82.             }
  83.         }
  84.        
  85.         return str.join("&");
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement