Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by initr on 26.10.2016.
- */
- import {Injectable, EventEmitter} from '@angular/core';
- import {Http, Headers, RequestOptions, RequestOptionsArgs, Response, RequestMethod, Request} from '@angular/http';
- import {Cookie} from 'ng2-cookies/ng2-cookies';
- import * as Rx from 'rxjs';
- export enum Action { QueryStart, QueryStop }
- @Injectable()
- export class HttpService {
- process: EventEmitter<any> = new EventEmitter<any>();
- authFailed: EventEmitter<any> = new EventEmitter<any>();
- private baseUrl = 'http://192.168.90.39';
- constructor(private _http: Http) { }
- private _buildAuthHeader(): string {
- return Cookie.get("access_token");
- }
- public auth(url: string, username: string, password: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- let body = 'username=' + username + '&password=' + password + '&grant_type=password';
- return this._request(RequestMethod.Post, url, body, options);
- }
- public get(url: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- return this._request(RequestMethod.Get, url, null, options);
- }
- public post(url: string, body: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- return this._request(RequestMethod.Post, url, body, options);
- }
- public put(url: string, body: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- return this._request(RequestMethod.Put, url, body, options);
- }
- public remove(url: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- return this._request(RequestMethod.Delete, url, null, options);
- }
- public patch(url: string, body: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- return this._request(RequestMethod.Patch, url, body, options);
- }
- public head(url: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- return this._request(RequestMethod.Head, url, null, options);
- }
- private _request(method: RequestMethod, url: string, body?: string, options?: RequestOptionsArgs): Rx.Observable<Response> {
- let token = this._buildAuthHeader();
- let requestOptions = new RequestOptions(Object.assign({
- method: method,
- url: this.baseUrl + url,
- body: body
- }, options));
- if (!requestOptions.headers) {
- requestOptions.headers = new Headers();
- }
- if(token){
- requestOptions.headers.set('Content-Type', 'application/json');
- requestOptions.headers.set("Authorization", 'Bearer ' + token);
- } else {
- requestOptions.headers.set('Content-Type', 'application/x-www-form-urlencoded');
- }
- return Rx.Observable.create((observer) => {
- this.process.next(Action.QueryStart);
- this._http.request(new Request(requestOptions))
- // .map(res=> res.json())
- .finally(() => {
- this.process.next(Action.QueryStop);
- })
- .subscribe(
- (res) => {
- observer.next(res);
- observer.complete();
- },
- (err) => {
- switch (err.status) {
- case 401:
- //intercept 401
- this.authFailed.next(err);
- observer.error(err);
- break;
- default:
- observer.error(err);
- break;
- }
- })
- })
- }
- }
Add Comment
Please, Sign In to add comment