Guest User

Untitled

a guest
Oct 27th, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by initr on 26.10.2016.
  3.  */
  4. import {Injectable, EventEmitter} from '@angular/core';
  5. import {Http, Headers, RequestOptions, RequestOptionsArgs, Response, RequestMethod, Request} from '@angular/http';
  6. import {Cookie} from 'ng2-cookies/ng2-cookies';
  7. import * as Rx from 'rxjs';
  8.  
  9. export enum Action { QueryStart, QueryStop }
  10.  
  11. @Injectable()
  12. export class HttpService {
  13.   process: EventEmitter<any> = new EventEmitter<any>();
  14.   authFailed: EventEmitter<any> = new EventEmitter<any>();
  15.   private baseUrl = 'http://192.168.90.39';
  16.  
  17.   constructor(private _http: Http) { }
  18.  
  19.   private _buildAuthHeader(): string {
  20.     return Cookie.get("access_token");
  21.   }
  22.  
  23.   public auth(url: string, username: string, password: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  24.     let body = 'username=' + username + '&password=' + password + '&grant_type=password';
  25.  
  26.     return this._request(RequestMethod.Post, url, body, options);
  27.   }
  28.  
  29.   public get(url: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  30.     return this._request(RequestMethod.Get, url, null, options);
  31.   }
  32.  
  33.   public post(url: string, body: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  34.     return this._request(RequestMethod.Post, url, body, options);
  35.   }
  36.  
  37.   public put(url: string, body: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  38.     return this._request(RequestMethod.Put, url, body, options);
  39.   }
  40.  
  41.   public remove(url: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  42.     return this._request(RequestMethod.Delete, url, null, options);
  43.   }
  44.  
  45.   public patch(url: string, body: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  46.     return this._request(RequestMethod.Patch, url, body, options);
  47.   }
  48.  
  49.   public head(url: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  50.     return this._request(RequestMethod.Head, url, null, options);
  51.   }
  52.  
  53.   private _request(method: RequestMethod, url: string, body?: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
  54.     let token = this._buildAuthHeader();
  55.     let requestOptions = new RequestOptions(Object.assign({
  56.       method: method,
  57.       url: this.baseUrl + url,
  58.       body: body
  59.     }, options));
  60.  
  61.     if (!requestOptions.headers) {
  62.       requestOptions.headers = new Headers();
  63.     }
  64.  
  65.     if(token){
  66.       requestOptions.headers.set('Content-Type', 'application/json');
  67.       requestOptions.headers.set("Authorization", 'Bearer ' + token);
  68.     } else {
  69.       requestOptions.headers.set('Content-Type', 'application/x-www-form-urlencoded');
  70.     }
  71.  
  72.     return Rx.Observable.create((observer) => {
  73.       this.process.next(Action.QueryStart);
  74.       this._http.request(new Request(requestOptions))
  75.         // .map(res=> res.json())
  76.         .finally(() => {
  77.           this.process.next(Action.QueryStop);
  78.         })
  79.         .subscribe(
  80.           (res) => {
  81.             observer.next(res);
  82.             observer.complete();
  83.           },
  84.           (err) => {
  85.             switch (err.status) {
  86.               case 401:
  87.                 //intercept 401
  88.                 this.authFailed.next(err);
  89.                 observer.error(err);
  90.                 break;
  91.               default:
  92.                 observer.error(err);
  93.                 break;
  94.             }
  95.           })
  96.     })
  97.   }
  98. }
Add Comment
Please, Sign In to add comment