blob: 5572683160aa235b81e81d645d30807ae4bc1b34 [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
16 private checkOptions(options?: RequestOptionsArgs): RequestOptionsArgs {
17 // if options are not there, create them
18 if(!options){
19 options = {};
20 }
21 return options;
22 }
23
24 private getHeaders(options: RequestOptionsArgs): RequestOptionsArgs {
25 // add auth headers
26 options.headers = this.authService.getUserHeaders();
27 return options;
28 }
29
30 private getParams(options: RequestOptionsArgs): RequestOptionsArgs {
31 // add the no_hyperlinks param
32 if (!options.search) {
33 options.search = new URLSearchParams();
34 }
35
36 if (options.search instanceof URLSearchParams) {
37 options.search.set('no_hyperlinks', '1');
38 }
39 else if (typeof options.search === 'string') {
40 options.search += '&no_hyperlinks=1';
41 }
42 return options;
43 }
44
45 get(url: string, options?: RequestOptionsArgs): Observable<Response> {
46
47 options = this.checkOptions(options);
48 options = this.getHeaders(options);
49 options = this.getParams(options);
50
51 return this.http.get(url, options)
52 }
53
54 // TODO add POST, PUT, DELETE declaration
55}