Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by josediaz on 22/02/2017.
  3.  */
  4. import { Injectable } from '@angular/core';
  5. import { Headers, Http, RequestOptionsArgs, Response } from '@angular/http';
  6. import { Observable } from 'rxjs';
  7. import {IUrlOptions, RequestTypes} from "./backend.model";
  8. import {AuthHttp, AuthHttpError} from "angular2-jwt";
  9. import {Message} from "primeng/primeng";
  10. import {Router} from "@angular/router";
  11.  
  12. @Injectable()
  13. export class BackendService {
  14.  
  15.     msgs: Message[] = [];
  16.  
  17.     constructor(
  18.         private host: string,
  19.         private http: Http,
  20.         private authHttp: AuthHttp,
  21.         private router: Router
  22.     ) { }
  23.     private constructUrl(urlOptions: IUrlOptions): string {
  24.         return this.host + urlOptions.restOfUrl;
  25.     }
  26.     //T specifies a generic output of function
  27.     public Request<T>(requestType: RequestTypes, urlOptions: IUrlOptions, body?: any, options?: RequestOptionsArgs) : Observable<T> {
  28.  
  29.         let response: Observable<Response>;
  30.         //True in case of post, put and patch
  31.         if (body && options) {
  32.             response = this.http[RequestTypes[requestType]](
  33.                 this.constructUrl(urlOptions),
  34.                 body,
  35.                 options);
  36.         }
  37.         //True in case of post, put and patch if options is empty
  38.         else if (body) {
  39.             let header = new Headers({'Content-Type': 'application/json'});
  40.             response = this.http[RequestTypes[requestType]](
  41.                 this.constructUrl(urlOptions),
  42.                 body, {headers: header});
  43.         }
  44.         //True in case of get, delete, head and options
  45.         else if (options) {
  46.             response = this.http[RequestTypes[requestType]](
  47.                 this.constructUrl(urlOptions),
  48.                 options);
  49.         }
  50.         //True in case of get, delete, head and options, if options is empty
  51.         else {
  52.             response = this.http[RequestTypes[requestType]](
  53.                 this.constructUrl(urlOptions),
  54.                 options);
  55.         }
  56.         return response.map((res) => <T>res.json());
  57.     }
  58.     //T specifies a generic output of function
  59.     public AuthRequest<T>(requestType: RequestTypes, urlOptions: IUrlOptions, body?: any, options?: RequestOptionsArgs) : Observable<T> {
  60.  
  61.         let response: Observable<Response>;
  62.         //True in case of post, put and patch
  63.  
  64.         if (body && options) {
  65.             response = this.authHttp[RequestTypes[requestType]](
  66.                 this.constructUrl(urlOptions),
  67.                 body,
  68.                 options);
  69.         }
  70.         //True in case of post, put and patch if options is empty
  71.         else if (body) {
  72.             let header = new Headers({'Content-Type': 'application/json'});
  73.             response = this.authHttp[RequestTypes[requestType]](
  74.                 this.constructUrl(urlOptions),
  75.                 body, {headers: header});
  76.         }
  77.         //True in case of get, delete, head and options
  78.         else if (options) {
  79.             response = this.authHttp[RequestTypes[requestType]](
  80.                 this.constructUrl(urlOptions),
  81.                 options);
  82.         }
  83.         //True in case of get, delete, head and options, if options is empty
  84.         else {
  85.             response = this.authHttp[RequestTypes[requestType]](
  86.                 this.constructUrl(urlOptions),
  87.                 options);
  88.         }
  89.         return response.map((res) => <T>res.json());
  90.     }
  91.  
  92.  
  93.     public notification(msgs:Message[],  error: any){
  94.         this.msgs = msgs;
  95.         this.msgs.push({severity: error.severity, summary: error.summary, detail:error.detail});
  96.     }
  97.  
  98.  
  99.     public handleError(error:Response){
  100.         if(error instanceof AuthHttpError){
  101.             this.router.navigate(['/noautorizado']);
  102.             return   Observable.throw(error);
  103.         }
  104.  
  105.         console.error('An error occurred', error);
  106.         return Observable.throw(error.json() || 'Server error');
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement