|
@@ -6,16 +6,17 @@ import { PreferencesProvider } from './preferences-provider';
|
|
|
import 'rxjs/add/operator/toPromise'
|
|
|
import 'rxjs/add/operator/map';
|
|
|
import 'rxjs/add/operator/catch';
|
|
|
-import 'rxjs/add/observable/throw'
|
|
|
+import 'rxjs/add/observable/throw';
|
|
|
+import 'rxjs/add/observable/forkJoin';
|
|
|
|
|
|
@Injectable()
|
|
|
export class OdooProvider {
|
|
|
|
|
|
- url: string;
|
|
|
+ private url: string;
|
|
|
|
|
|
constructor(
|
|
|
- public http: Http,
|
|
|
- public preferencesProvider: PreferencesProvider
|
|
|
+ private http: Http,
|
|
|
+ private preferencesProvider: PreferencesProvider
|
|
|
) {
|
|
|
this.url = preferencesProvider.getHost();
|
|
|
this.normalizeUrl();
|
|
@@ -31,32 +32,64 @@ export class OdooProvider {
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- post(resource: string, data: Array<any>): Array<Promise<any>> {
|
|
|
+ post(resource: string, data: Array<any>): Promise<any> {
|
|
|
let headers = new Headers({
|
|
|
+ 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
'Authorization': 'JWT ' + this.preferencesProvider.getToken()
|
|
|
});
|
|
|
+
|
|
|
let options = new RequestOptions({ headers: headers });
|
|
|
|
|
|
- let promises: Array<Promise<any>> = [];
|
|
|
+ let toSend: Array<Observable<Response>> = [];
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
- promises.push(this.http.post(this.url + resource, null, options).toPromise());
|
|
|
+ this.encodeData(data[i]);
|
|
|
+ toSend.push(this.http.post(this.url + resource, this.encodeData(data[i]), options));
|
|
|
}
|
|
|
|
|
|
- return promises;
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ Observable.forkJoin(toSend).subscribe(r => {
|
|
|
+ resolve(r);
|
|
|
+ }, e => {
|
|
|
+ reject(e);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private encodeData(data: any) {
|
|
|
+ let body = [];
|
|
|
+ for (let field in data) {
|
|
|
+ if (['doc_state', "remote_id", "rev"].indexOf(field) == -1) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ body.push(field + "=" + data[field]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return body.join("&");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- get(resource: string): Observable<Array<any>> {
|
|
|
+ get(resource: string, id?: number, filter?: Array<[string, string]>): Promise<any> {
|
|
|
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);
|
|
|
+
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ this.http.get(this.url, options).toPromise().then(r => {
|
|
|
+ resolve(r);
|
|
|
+ }).catch(e => {
|
|
|
+ reject(e);
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|