Skip to content

Instantly share code, notes, and snippets.

@aurelmegn
Last active September 3, 2017 09:30
Show Gist options
  • Save aurelmegn/d482407dfdc2e5b18ac5d0a5e4c1c972 to your computer and use it in GitHub Desktop.
Save aurelmegn/d482407dfdc2e5b18ac5d0a5e4c1c972 to your computer and use it in GitHub Desktop.
An http service for angular 4 which use the cache to store and retrieve data
import {Injectable} from '@angular/core'
import {Response, Headers, Http, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {SessionStorageService} from 'ngx-webstorage';
import {ApiUrls, server, STORAGE} from '../parameters';
@Injectable()
export class HttpService {
/**
*
*/
public defaultHeader: Headers = new Headers();
/**
*
*/
private defaultOptions: RequestOptions;
public uploadHeader: Headers = new Headers();
public uploadOptions: RequestOptions;
// TODO setup this parameter to be get in the service config
public storageTokenKey: string;
public serverUrl: string;
private handleError(error: Response) {
return Observable.throw(error);
}
public constructor(private http: Http, private StorageService: SessionStorageService) {
this.defaultHeader.append('content-type', 'application/json');
this.defaultHeader.append('accept', 'application/json');
this.uploadHeader.append('content-type', 'multipart/form-data');
this.uploadHeader.append('accept', 'application/json');
const token = this.StorageService.retrieve(this.storageTokenKey);
if (token !== null) {
this.uploadHeader.append('authorization', 'Bearer' + token);
this.defaultHeader.append('authorization', 'Bearer' + token);
}
// TODO remove this line after setup the key parameter get
this.defaultHeader = this.StorageService.retrieve(STORAGE.HTTP.headerWithToken);
this.uploadOptions = new RequestOptions({headers: this.uploadHeader});
this.defaultOptions = new RequestOptions({ headers: this.defaultHeader });
}
public post(url: string, data: object, final?: () => void, isUpload: boolean = false): Observable<Response> {
let Options: RequestOptions = this.defaultOptions;
if (isUpload) {
Options = this.uploadOptions;
}
url = ApiUrls.url + url;
return this.http.post(url, data, Options)
.map(res => res.json())
.catch(this.handleError)
.finally(final);
}
public put(url: string, data: object, final?: () => void, isUpload: boolean = false): Observable<Response> {
let Options: RequestOptions = this.defaultOptions;
if (isUpload) {
Options = this.uploadOptions;
}
url = ApiUrls.url + url;
return this.http.put(url, data, Options)
.map(res => res.json())
.catch(this.handleError)
.finally(final);
}
public get(url: string, savedId: string, final?: () => void): Observable<Response> {
// put the server url in url
url = server + url;
const element = this.StorageService.retrieve(savedId);
// const element = null;
if (element === null) {
return this.http.get(url, this.defaultOptions)
.map(res => {
const data = res.json();
this.StorageService.store(savedId, data);
return data;
})
.catch(this.handleError)
.finally(final);
} else {
return Observable.of(element)
.finally(final);
}
}
public delete(url: string, savedId: string, final?: () => void): Observable<Response> {
// put the server url in url
url = server + url;
return this.http.delete(url, this.defaultOptions)
.map(res => res.json())
.catch(this.handleError)
.finally(final);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment