blob: a300ee34ecba18818914268f4411507bad2be2c4 [file] [log] [blame]
Matteo Scandolo0f77c502016-12-06 16:46:00 -08001import {Http, RequestOptionsArgs, Response, URLSearchParams} from '@angular/http';
2import {Injectable} from '@angular/core';
3import {Observable} from 'rxjs';
4import {AuthService} from './auth.service';
5/**
6 * Created by teone on 12/6/16.
7 */
8@Injectable()
9export class XosHttp {
10 constructor(
11 private http: Http,
12 private authService: AuthService
13 ) {
14 }
15
Matteo Scandolo40f8fa92016-12-07 09:21:35 -080016 // TODO intercept non authenticated calls and send to login (remove cookies)
17 // TODO add POST, PUT, DELETE declaration
18 get(url: string, options?: RequestOptionsArgs): Observable<Response> {
19
20 options = this.checkOptions(options);
21 options = this.getHeaders(options);
22 options = this.getParams(options);
23
24 return this.http.get(url, options);
25 }
26
Matteo Scandolo0f77c502016-12-06 16:46:00 -080027 private checkOptions(options?: RequestOptionsArgs): RequestOptionsArgs {
28 // if options are not there, create them
Matteo Scandolo40f8fa92016-12-07 09:21:35 -080029 if (!options) {
Matteo Scandolo0f77c502016-12-06 16:46:00 -080030 options = {};
31 }
32 return options;
33 }
34
35 private getHeaders(options: RequestOptionsArgs): RequestOptionsArgs {
36 // add auth headers
37 options.headers = this.authService.getUserHeaders();
38 return options;
39 }
40
41 private getParams(options: RequestOptionsArgs): RequestOptionsArgs {
42 // add the no_hyperlinks param
43 if (!options.search) {
44 options.search = new URLSearchParams();
45 }
46
47 if (options.search instanceof URLSearchParams) {
48 options.search.set('no_hyperlinks', '1');
49 }
50 else if (typeof options.search === 'string') {
51 options.search += '&no_hyperlinks=1';
52 }
53 return options;
54 }
Matteo Scandolo0f77c502016-12-06 16:46:00 -080055}