|
@@ -1,16 +1,44 @@
|
|
|
import { Injectable } from '@angular/core';
|
|
|
import { Http, Headers, Response, RequestOptions } from '@angular/http';
|
|
|
+import { Observable } from 'rxjs/Observable';
|
|
|
+import { PreferencesProvider } from './preferences-provider';
|
|
|
+
|
|
|
+import 'rxjs/add/operator/map';
|
|
|
+import 'rxjs/add/operator/catch';
|
|
|
+import 'rxjs/add/observable/throw'
|
|
|
|
|
|
@Injectable()
|
|
|
export class OdooProvider {
|
|
|
|
|
|
- constructor() { }
|
|
|
+ url: string;
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ public http: Http,
|
|
|
+ public preferencesProvider: PreferencesProvider
|
|
|
+ ) {
|
|
|
+ this.url = preferencesProvider.getHost();
|
|
|
+ this.normalizeUrl();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private normalizeUrl(): void {
|
|
|
+ this.url = (this.url.startsWith('http://') ? this.url : 'http://' + this.url) + '/api/';
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- get(resource: string) {
|
|
|
+ get(resource: string): Observable<Response> {
|
|
|
+ this.url = this.url + resource;
|
|
|
|
|
|
+ let headers = new Headers({
|
|
|
+ 'Authorization': 'JWT ' + this.preferencesProvider.getToken()
|
|
|
+ });
|
|
|
+ let options = new RequestOptions({ headers: headers });
|
|
|
+
|
|
|
+ return this.http.get(this.url, options).map(this.extractData).catch(this.handleError);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -26,4 +54,20 @@ export class OdooProvider {
|
|
|
delete(resource: string, data: Array<any>) {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private extractData(response: Response): any {
|
|
|
+ return response.json();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private handleError(error: any) {
|
|
|
+ let msg = error._body;
|
|
|
+
|
|
|
+ return Observable.throw(msg);
|
|
|
+ }
|
|
|
}
|