Skip to content

Instantly share code, notes, and snippets.

@misha130
Created December 12, 2016 14:45
Show Gist options
  • Save misha130/2e52e573703802af0e7fbbef07830156 to your computer and use it in GitHub Desktop.
Save misha130/2e52e573703802af0e7fbbef07830156 to your computer and use it in GitHub Desktop.
import { Http, RequestOptionsArgs, Headers, RequestOptions, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Injectable } from '@angular/core';
@Injectable()
export class HttpHandler {
constructor(public http: Http) {
}
public Get(url: string): Promise<Response> {
return this.http.get(this.composeUrl(url), this.getRequestOptionArgs()).toPromise();
}
private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
if (options === undefined || options === null) {
options = new RequestOptions();
}
if (options.headers === undefined || options.headers === null) {
options.headers = new Headers();
}
options.headers.append('Content-Type', 'application/json; charset=utf-8');
// options.headers.append('token', Global.getCookie('misite-token'));
return options;
}
private composeUrl(url: string): string {
let urlC: string = (simulated === true ? 'assets/mocks/' : this.global.apiUrl) + url;
if (simulated === true) {
if (urlC.indexOf('?') !== -1) {
urlC = urlC.replace('?', '.json?decache=' + (Math.random() * 100).toString() + '&');
}
else
urlC = urlC + '.json?decache=' + (Math.random() * 100).toString();
}
return urlC;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment