|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { HttpClient, HttpErrorResponse } from '@angular/common/http'; |
| 3 | +import { Observable } from 'rxjs/Observable'; |
| 4 | +import { catchError, map } from 'rxjs/operators'; |
| 5 | + |
| 6 | +import { Store } from '@ngrx/store'; |
| 7 | +import { AppState } from '@app/store/app.reducers'; |
| 8 | +import { HTTP } from '@ionic-native/http/ngx'; |
| 9 | +import { ApiBaseService } from './api-base.service'; |
| 10 | +import { from } from 'rxjs/internal/observable/from'; |
| 11 | + |
| 12 | +export interface APIError { |
| 13 | + error: any; |
| 14 | + errorText: string; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Implementation of angular-http |
| 19 | + * https://angular.io/guide/http |
| 20 | + * |
| 21 | + * TODO: Improve retry with: https://www.learnrxjs.io/operators/error_handling/retrywhen.html |
| 22 | + */ |
| 23 | +@Injectable() |
| 24 | +export class ApiBaseNativeService extends ApiBaseService { |
| 25 | + constructor( |
| 26 | + protected http: HttpClient, |
| 27 | + protected store$: Store<AppState>, |
| 28 | + protected httpNative: HTTP |
| 29 | + ) { |
| 30 | + super(http, store$); |
| 31 | + } |
| 32 | + |
| 33 | + public doGet(url, params = null, data = null): Observable<any> { |
| 34 | + // const options = this.getOptions(url, params, data); |
| 35 | + url = this.parseURLParams(url, params); |
| 36 | + return from( |
| 37 | + this.httpNative.get(this.serverURL + url, {}, this.httpHeadersPlain) |
| 38 | + ).pipe( |
| 39 | + map((response) => JSON.parse(response.data)), |
| 40 | + catchError((error) => this.handleError(error)) |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public doPost(url, params, data): Observable<any> { |
| 45 | + // const options = this.getOptions(url, params, data); |
| 46 | + this.setHeaders(); |
| 47 | + url = this.parseURLParams(url, params); |
| 48 | + return from(this.httpNative.post(this.serverURL + url, data, {})).pipe( |
| 49 | + catchError((error) => this.handleError(error)) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public doPut(url, params, data): Observable<any> { |
| 54 | + // const options = this.getOptions(url, params, data); |
| 55 | + url = this.parseURLParams(url, params); |
| 56 | + return from( |
| 57 | + this.httpNative.put(this.serverURL + url, data, this.httpHeadersPlain) |
| 58 | + ).pipe(catchError((error) => this.handleError(error))); |
| 59 | + } |
| 60 | + |
| 61 | + public doDel(url, params, data = null): Observable<any> { |
| 62 | + // const options = this.getOptions(url, params, data); |
| 63 | + url = this.parseURLParams(url, params); |
| 64 | + return from( |
| 65 | + this.httpNative.delete(this.serverURL + url, {}, this.httpHeadersPlain) |
| 66 | + ).pipe(catchError((error) => this.handleError(error))); |
| 67 | + } |
| 68 | + |
| 69 | + public generateFormData(values: any): FormData { |
| 70 | + const form = new FormData(); |
| 71 | + for (const key in values) { |
| 72 | + if (values[key]) { |
| 73 | + form.append(key, values[key]); |
| 74 | + } |
| 75 | + } |
| 76 | + return form; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Generate the httpHeaders property |
| 81 | + */ |
| 82 | + protected setHeaders() { |
| 83 | + for (const key in this.httpHeadersPlain) { |
| 84 | + if (!this.httpHeadersPlain[key]) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + this.httpNative.setHeader( |
| 88 | + '*', |
| 89 | + String(key), |
| 90 | + String(this.httpHeadersPlain[key]) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected handleError(error: HttpErrorResponse): Observable<APIError> { |
| 96 | + console.error('ERROR CATCHED', error); |
| 97 | + return super.handleError(error); |
| 98 | + } |
| 99 | + |
| 100 | + protected getOptions(url, params = null, data = null) { |
| 101 | + return { |
| 102 | + headers: this.httpHeaders, |
| 103 | + // params: this.parseURLParams(url, params), |
| 104 | + }; |
| 105 | + } |
| 106 | +} |
0 commit comments