### File

`src/app/core/services/http.ts`

### Constructor

`constructor(backend: ConnectionBackend, defaultOptions: RequestOptions)`
Defined in src/app/core/services/http.ts:22

### Methods

request
`request(url: string | Request, options: RequestOptionsArgs)`
Defined in src/app/core/services/http.ts:37
Performs any type of http request. Parameters : url options Returns: `Observable<Response>`

get
`get(url: string, options: RequestOptionsArgs)`
Defined in src/app/core/services/http.ts:47
Performs a request with `get` http method. Parameters : url options Returns: `Observable<any>`

getLocal
`getLocal(url: string, options: RequestOptionsArgs)`
Defined in src/app/core/services/http.ts:61
Returns: `Observable<any>`

post
`post(url: string, body: any, options: RequestOptionsArgs)`
Defined in src/app/core/services/http.ts:72
Performs a request with `post` http method. Parameters : url body options Returns: `Observable<any>`

put
`put(url: string, body: any, options: RequestOptionsArgs)`
Defined in src/app/core/services/http.ts:93
Performs a request with `put` http method. Parameters : url body options Returns: `Observable<any>`

delete
`delete(url: string, options: RequestOptionsArgs)`
Defined in src/app/core/services/http.ts:113
Performs a request with `delete` http method. Parameters : url options Returns: `Observable<any>`

Private requestOptions
`requestOptions(options: RequestOptionsArgs)`
Defined in src/app/core/services/http.ts:133
Request options. Parameters : options Returns: `RequestOptionsArgs`

Private getFullUrl
`getFullUrl(url: string)`
Defined in src/app/core/services/http.ts:153
Build API url. Parameters : url Returns: `string`

Private requestInterceptor
`requestInterceptor()`
Defined in src/app/core/services/http.ts:160
Request interceptor. Returns: `void`

Private responseInterceptor
`responseInterceptor()`
Defined in src/app/core/services/http.ts:171
Response interceptor. Returns: `void`

Private onCatch
`onCatch(error: any, caught: Observable)`
Defined in src/app/core/services/http.ts:182
Error handler. Parameters : error caught Returns: `Observable<any>`

Private onSubscribeSuccess
`onSubscribeSuccess(res: Response)`
Defined in src/app/core/services/http.ts:192
onSubscribeSuccess Parameters : res Returns: `void`

Private onSubscribeError
`onSubscribeError(error: any)`
Defined in src/app/core/services/http.ts:202
onSubscribeError Parameters : error Returns: `void`

Private onFinally
`onFinally()`
Defined in src/app/core/services/http.ts:213
onFinally Returns: `void`

### Properties

Public loading
`loading: `Subject<{}>``
Defined in src/app/core/services/http.ts:22

    import { Injectable } from '@angular/core';
    import {
      Http,
      ConnectionBackend,
      RequestOptions,
      RequestOptionsArgs,
      Response,
      Headers,
      Request
    } from '@angular/http';
    import { Observable } from 'rxjs/Observable';
    import { environment } from './../../../environments/environment';
    import { Subject } from 'rxjs/Subject';

    @Injectable()
    export class HttpService extends Http {
      public loading = new Subject();

      constructor(
        backend: ConnectionBackend,
        defaultOptions: RequestOptions,
      ) {
        super(backend, defaultOptions);
      }

      /**
       * Performs any type of http request.
       * @param url
       * @param options
       * @returns {Observable<Response>}
       */
      request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
        return super.request(url, options);
      }

      /**
       * Performs a request with `get` http method.
       * @param url
       * @param options
       * @returns {Observable<>}
       */
      get(url: string, options?: RequestOptionsArgs): Observable<any> {
        this.requestInterceptor();
        return super.get(this.getFullUrl(url), this.requestOptions(options))
          .catch(this.onCatch.bind(this))
          .do((res: Response) => {
            this.onSubscribeSuccess(res);
          }, (error: any) => {
            this.onSubscribeError(error);
          })
          .finally(() => {
            this.onFinally();
          });
      }

      getLocal(url: string, options?: RequestOptionsArgs): Observable<any> {
        return super.get(url, options);
      }

      /**
       * Performs a request with `post` http method.
       * @param url
       * @param body
       * @param options
       * @returns {Observable<>}
       */
      post(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
        this.requestInterceptor();
        return super.post(this.getFullUrl(url), body, this.requestOptions(options))
          .catch(this.onCatch.bind(this))
          .do((res: Response) => {
            this.onSubscribeSuccess(res);
          }, (error: any) => {
            this.onSubscribeError(error);
          })
          .finally(() => {
            this.onFinally();
          });
      }

      /**
       * Performs a request with `put` http method.
       * @param url
       * @param body
       * @param options
       * @returns {Observable<>}
       */
      put(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
        this.requestInterceptor();
        return super.put(this.getFullUrl(url), body, this.requestOptions(options))
          .catch(this.onCatch.bind(this))
          .do((res: Response) => {
            this.onSubscribeSuccess(res);
          }, (error: any) => {
            this.onSubscribeError(error);
          })
          .finally(() => {
            this.onFinally();
          });
      }

      /**
       * Performs a request with `delete` http method.
       * @param url
       * @param options
       * @returns {Observable<>}
       */
      delete(url: string, options?: RequestOptionsArgs): Observable<any> {
        this.requestInterceptor();
        return super.delete(this.getFullUrl(url), this.requestOptions(options))
          .catch(this.onCatch.bind(this))
          .do((res: Response) => {
            this.onSubscribeSuccess(res);
          }, (error: any) => {
            this.onSubscribeError(error);
          })
          .finally(() => {
            this.onFinally();
          });
      }

      /**
       * Request options.
       * @param options
       * @returns {RequestOptionsArgs}
       */
      private requestOptions(options?: RequestOptionsArgs): RequestOptionsArgs {
        if (options == null) {
          options = new RequestOptions();
        }

        if (options.headers == null) {
          const user = JSON.parse(localStorage.getItem('user'));
          options.headers = new Headers({
            'Content-Type': 'application/json',
            'X-Spree-Token': user && user.spree_api_key
          });
        }
        return options;
      }

      /**
       * Build API url.
       * @param url
       * @returns {string}
       */
      private getFullUrl(url: string): string {
        return environment.API_ENDPOINT + url;
      }

      /**
       * Request interceptor.
       */
      private requestInterceptor(): void {
        console.log('Sending Request');
        // this.loaderService.showPreloader();
        this.loading.next({
          loading: true, hasError: false, hasMsg: ''
        });
      }

      /**
       * Response interceptor.
       */
      private responseInterceptor(): void {
        console.log('Request Complete');
        // this.loaderService.hidePreloader();
      }

      /**
       * Error handler.
       * @param error
       * @param caught
       * @returns {ErrorObservable}
       */
      private onCatch(error: any, caught: Observable<any>): Observable<any> {
        console.log('Something went terrible wrong and error is', error);
        // this.loaderService.popError();
        return Observable.of(error);
      }

      /**
       * onSubscribeSuccess
       * @param res
       */
      private onSubscribeSuccess(res: Response): void {
        this.loading.next({
          loading: false, hasError: false, hasMsg: ''
        });
      }

      /**
       * onSubscribeError
       * @param error
       */
      private onSubscribeError(error: any): void {
        console.log('Something Went wrong while subscribing', error);
        // this.loaderService.popError();
        this.loading.next({
          loading: false, hasError: true, hasMsg: 'Something went wrong'
        });
      }

      /**
       * onFinally
       */
      private onFinally(): void {
        this.responseInterceptor();
      }
    }

# results matching ""

# No results matching ""