From a02ed7221adf84c92db2b5646abe75932bebd4cd Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 16 Jan 2016 17:02:25 +0800 Subject: [PATCH 01/29] increate version to 0.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7e065d6..36f2e98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo", - "version": "0.1.0", + "version": "0.5.0", "description": "React Native library for Odoo using JSON-RPC", "main": "./lib/index.js", "directories": { From 63067ee1e78a134f1737a275f7b2c1b13b794692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Thu, 26 Jul 2018 21:43:47 -0400 Subject: [PATCH 02/29] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e804489 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 César Gutiérrez Olivares + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 02aa02dc518160a2bda2c8b22d3d2bbc48663aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Thu, 26 Jul 2018 22:04:07 -0400 Subject: [PATCH 03/29] Update semver to 1.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 36f2e98..d461f86 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-native-odoo", - "version": "0.5.0", "description": "React Native library for Odoo using JSON-RPC", + "version": "1.0.0", "main": "./lib/index.js", "directories": { "test": "test" From 7c7e35cb3e11ee2ec05ca1b2edfdc77e1bbf476d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Thu, 26 Jul 2018 22:05:22 -0400 Subject: [PATCH 04/29] Update package.json info --- package.json | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index d461f86..9b97cca 100644 --- a/package.json +++ b/package.json @@ -1,36 +1,32 @@ { "name": "react-native-odoo", - "description": "React Native library for Odoo using JSON-RPC", "version": "1.0.0", + "description": "React Native library for Odoo using Fetch API and JSON-RPC", + "keywords": [ + "odoo", + "openerp", + "jsonrpc", + "react-native" + ], + "homepage": "https://github.com/cesar-gutierrez/react-native-odoo", + "bugs": { + "url": "https://github.com/cesar-gutierrez/react-native-odoo/issues" + }, + "license": "MIT", "main": "./lib/index.js", "directories": { "test": "test" }, - "scripts": { - "test": "mocha test" - }, - "engines": { - "node": ">=0.11.16" - }, "repository": { "type": "git", - "url": "git+https://github.com/kevin3274/react-native-odoo.git" + "url": "git+https://github.com/cesar-gutierrez/react-native-odoo" }, - "keywords": [ - "odoo", - "openerp", - "jsonrpc" - "react-native" - ], - "author": { - "name": "Kevin Wang", - "email": "kevin_327@163.com" - }, - "bugs": { - "url": "https://github.com/kevin327/react-native-odoo/issues" + "scripts": { + "test": "mocha test" }, - "homepage": "https://github.com/kevin3274/react-native-odoo", - "dependencies": { + "author": { + "name": "César Gutiérrez Olivares", + "email": "hello@cesar.codes" }, "devDependencies": { "gulp": "^3.8.11", @@ -39,9 +35,8 @@ }, "maintainers": [ { - "name": "kevin3274", - "email": "kevin_327@163.com" + "name": "César Gutiérrez Olivares", + "email": "hello@cesar.codes" } - ], - "readme": "ERROR: No README data found!" + ] } From b97248708a1bd5521977b02e253c706dbe833ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Thu, 26 Jul 2018 22:09:29 -0400 Subject: [PATCH 05/29] Add support for authentication with Odoo session id instead of user/pass --- lib/index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8e7a74c..9051732 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,14 +1,15 @@ 'use strict'; var Odoo = function (config) { - config = config || {}; - - this.host = config.host; - this.port = config.port || 80; - this.database = config.database; - this.username = config.username; - this.password = config.password; -}; + config = config || {} + + this.host = config.host + this.port = config.port || 80 + this.database = config.database + this.username = config.username || null + this.password = config.password || null + this.sid = config.sid || null +} // Connect From 2061e74c430bfff9336a139b688784de6a41e1e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Thu, 26 Jul 2018 22:11:22 -0400 Subject: [PATCH 06/29] Change callback-based implementation for promise-based methods --- lib/index.js | 165 +++++++++++++++++++++++++++------------------------ 1 file changed, 86 insertions(+), 79 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9051732..c1bf206 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,4 @@ -'use strict'; +'use strict' var Odoo = function (config) { config = config || {} @@ -12,16 +12,15 @@ var Odoo = function (config) { } // Connect - -Odoo.prototype.connect = function(cb){ +Odoo.prototype.connect = function () { var params = { db: this.database, login: this.username, password: this.password - }; + } - var json = JSON.stringify({ params: params }); - var url = 'http://' + this.host + ':' + this.port + '/web/session/authenticate'; + var json = JSON.stringify({ params: params }) + var url = 'http://' + this.host + ':' + this.port + '/web/session/authenticate' var options = { method: 'POST', @@ -31,35 +30,36 @@ Odoo.prototype.connect = function(cb){ 'Content-Length': json.length }, body: json - }; - fetch(url, options) - .then(res=>{ - this.sid = res.headers.map['set-cookie'][0].split(';')[0]; - console.log('sid:', this.sid); - return res.json() + } + + var context = this + + return fetch(url, options) + .then(function (response) { + context.sid = response.headers.map['set-cookie'][0].split(';')[0] + return response.json() }) - .then(data=>{ - if (data.error){ - cb(data.error, null); - }else{ - this.uid = data.result.uid; - this.session_id = data.result.session_id; - this.context = data.result.user_context; - this.username = data.result.username; - cb(null, data.result); + .then(function (responseJson) { + if(responseJson.error) return { success: false, error: responseJson.error } + else { + context.uid = responseJson.result.uid + context.session_id = responseJson.result.session_id + context.context = responseJson.result.user_context + context.username = responseJson.result.username + return { success: true, data: responseJson.result } } - }, err=>{ - cb(err, null); - }); - -}; + }) + .catch(function (error) { + return { success: false, error: error } + }) +} // Search records -Odoo.prototype.search = function (model, params, callback) { - // assert(params.ids, "Must provide a list of IDs."); +Odoo.prototype.search = function (model, params) { + //assert(params.ids, "Must provide a list of IDs."); //assert(params.domain, "Must provide a search domain."); - this._request('/web/dataset/call_kw', { + return this._request('/web/dataset/call_kw', { kwargs: { context: this.context }, @@ -68,18 +68,18 @@ Odoo.prototype.search = function (model, params, callback) { args: [ params.domain, ], - }, callback); -}; + }) +} // Search & Read records // https://www.odoo.com/documentation/8.0/api_integration.html#search-and-read // https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.search // https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.read -Odoo.prototype.search_read = function (model, params, callback) { +Odoo.prototype.search_read = function (model, params) { //assert(params.domain, "'domain' parameter required. Must provide a search domain."); //assert(params.limit, "'limit' parameter required. Must specify max. number of results to return."); - this._request('/web/dataset/call_kw', { + return this._request('/web/dataset/call_kw', { model: model, method: 'search_read', args: [], @@ -91,16 +91,16 @@ Odoo.prototype.search_read = function (model, params, callback) { order: params.order, fields: params.fields, }, - }, callback); -}; + }) +} // Get record // https://www.odoo.com/documentation/8.0/api_integration.html#read-records // https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.read -Odoo.prototype.get = function (model, params, callback) { +Odoo.prototype.get = function (model, params) { //assert(params.ids, "Must provide a list of IDs."); - this._request('/web/dataset/call_kw', { + return this._request('/web/dataset/call_kw', { model: model, method: 'read', args: [ @@ -109,76 +109,78 @@ Odoo.prototype.get = function (model, params, callback) { kwargs: { fields: params.fields, }, - }, callback); -}; //get + }) +} // Browse records by ID // Not a direct implementation of Odoo RPC 'browse' but rather a workaround based on 'search_read' // https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.browse Odoo.prototype.browse_by_id = function(model, params, callback) { - params.domain = [['id', '>', '0' ]]; // assumes all records IDs are > 0 - this.search_read(model, params, callback); -}; //browse + params.domain = [['id', '>', '0' ]] // assumes all records IDs are > 0 + this.search_read(model, params) + .then(function (response) { + return response + }) +} // Create record -Odoo.prototype.create = function (model, params, callback) { - this._request('/web/dataset/call_kw', { +Odoo.prototype.create = function (model, params) { + return this._request('/web/dataset/call_kw', { kwargs: { context: this.context }, model: model, method: 'create', args: [params] - }, callback); -}; + }) +} // Update record -Odoo.prototype.update = function (model, id, params, callback) { - if (id) { - this._request('/web/dataset/call_kw', { +Odoo.prototype.update = function (model, ids, params) { + if (ids) { + return this._request('/web/dataset/call_kw', { kwargs: { context: this.context }, model: model, method: 'write', - args: [[id], params] - }, callback); + args: [ids, params] + }) } -}; +} // Delete record -Odoo.prototype.delete = function (model, id, callback) { - this._request('/web/dataset/call_kw', { +Odoo.prototype.delete = function (model, id) { + return this._request('/web/dataset/call_kw', { kwargs: { context: this.context }, model: model, method: 'unlink', args: [[id]] - }, callback); -}; + }) +} // Generic RPC wrapper // DOES NOT AUTO-INCLUDE context -Odoo.prototype.rpc_call = function (endpoint, params, callback) { +Odoo.prototype.rpc_call = function (endpoint, params) { //assert(params.model); // assert(params.method); // assert(params.args); // assert(params.kwargs); // assert(params.kwargs.context); - this._request(endpoint, params, callback); -}; //generic - + return this._request(endpoint, params) +} // Private functions -Odoo.prototype._request = function (path, params, cb) { - params = params || {}; +Odoo.prototype._request = function (path, params) { + params = params || {} - var url = 'http://' + this.host + ':' + this.port + (path || '/') + ''; + var url = 'http://' + this.host + ':' + this.port + (path || '/') + '' var options = { method: 'POST', headers: { @@ -186,20 +188,25 @@ Odoo.prototype._request = function (path, params, cb) { 'Accept': 'application/json', 'Cookie': this.sid + ';' }, - body: JSON.stringify({jsonrpc: '2.0', id: new Date().getUTCMilliseconds(), method: 'call', params: params}) - }; - - fetch(url, options) - .then(res=>res.json()) - .then(data=>{ - if (data.error){ - cb(data.error, null); - }else{ - cb(null, data.result); - } - }, err=>{ - cb(err, null); - }); -}; + body: JSON.stringify({ + jsonrpc: '2.0', + id: new Date().getUTCMilliseconds(), + method: 'call', + params: params + }) + } + + return fetch(url, options) + .then(function (response) { + return response.json() + }) + .then(function (responseJson) { + if(responseJson.error) return { success: false, error: responseJson.error } + else return { success: true, data: responseJson.result } + }) + .catch(function (error) { + return { success: false, error: error } + }) +} -module.exports = Odoo; +module.exports = Odoo From 1b28f037444157ebe21c8eb421ed19fd454ba5f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Tue, 7 Aug 2018 20:16:52 -0400 Subject: [PATCH 07/29] Remove old test and example files and dependencies --- .jshintrc | 19 ------ examples/basic.js | 21 ------ gulpfile.js | 11 --- package.json | 15 ++-- test/test.js | 171 ---------------------------------------------- 5 files changed, 5 insertions(+), 232 deletions(-) delete mode 100644 .jshintrc delete mode 100644 examples/basic.js delete mode 100644 gulpfile.js delete mode 100644 test/test.js diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index b342fca..0000000 --- a/.jshintrc +++ /dev/null @@ -1,19 +0,0 @@ -{ - "curly": false, - "eqeqeq": false, - "indent": false, - "newcap": false, - "plusplus": false, - "quotmark": false, - "unused": false, - "strict": false, - "trailing": false, - "maxstatements": false, - "maxlen": false, - - "eqnull": true, - "smarttabs": true, - - "browser": true, - "node": true -} diff --git a/examples/basic.js b/examples/basic.js deleted file mode 100644 index a2f11bd..0000000 --- a/examples/basic.js +++ /dev/null @@ -1,21 +0,0 @@ -var Odoo = require('../lib/index'); - -var odoo = new Odoo({ - host: 'localhost', - port: 4569, - database: '4yopping', - username: 'admin', - password: '4yopping' -}); - -// Connect to Odoo -odoo.connect(function (err) { - if (err) { return console.log(err); } - - // Get a partner - odoo.get('res.partner', 4, function (err, partner) { - if (err) { return console.log(err); } - - console.log('Partner', partner); - }); -}); diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index 8df8944..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,11 +0,0 @@ -var gulp = require('gulp'), - mocha = require('gulp-mocha'); - -gulp.task('test', function () { - return gulp.src(['./test/**/*.js'], { read: false }) - .pipe(mocha({ reporter: 'spec' })); -}); - -gulp.task('default', function () { - gulp.watch(['./lib/**/*', './test/**/*'], ['test']); -}); diff --git a/package.json b/package.json index 9b97cca..9c64b2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "react-native-odoo", - "version": "1.0.0", + "name": "react-native-odoo-promise-based", + "version": "0.1.0", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", @@ -8,9 +8,9 @@ "jsonrpc", "react-native" ], - "homepage": "https://github.com/cesar-gutierrez/react-native-odoo", + "homepage": "https://github.com/cesar-gutierrez/react-native-odoo-promise-based", "bugs": { - "url": "https://github.com/cesar-gutierrez/react-native-odoo/issues" + "url": "https://github.com/cesar-gutierrez/react-native-odoo-promise-based/issues" }, "license": "MIT", "main": "./lib/index.js", @@ -19,7 +19,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/cesar-gutierrez/react-native-odoo" + "url": "git+https://github.com/cesar-gutierrez/react-native-odoo-promise-based" }, "scripts": { "test": "mocha test" @@ -28,11 +28,6 @@ "name": "César Gutiérrez Olivares", "email": "hello@cesar.codes" }, - "devDependencies": { - "gulp": "^3.8.11", - "gulp-mocha": "^2.0.0", - "sinon": "^1.12.2" - }, "maintainers": [ { "name": "César Gutiérrez Olivares", diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 4c9c415..0000000 --- a/test/test.js +++ /dev/null @@ -1,171 +0,0 @@ -var assert = require('assert'), - sinon = require('sinon'), - Odoo = require('../lib/index'); - -var config = { - host: 'odoo4yopping.vagrantshare.com', - port: 80, - database: '4yopping', - username: 'admin', - password: '4yopping' -}; - -var odoo = new Odoo(config); - -describe('Odoo', function () { - this.timeout(3000); - - it('Odoo should be a function', function () { - assert.equal(typeof Odoo, 'function'); - }); - - it('odoo should be an instance of Odoo', function () { - assert(odoo instanceof Odoo); - }); - - it('odoo should have this properties', function () { - assert.notEqual(odoo.host, undefined); - assert.equal(odoo.host, config.host); - assert.notEqual(odoo.port, undefined); - assert.equal(odoo.port, config.port); - assert.notEqual(odoo.database, undefined); - assert.equal(odoo.database, config.database); - assert.notEqual(odoo.username, undefined); - assert.equal(odoo.username, config.username); - assert.notEqual(odoo.password, undefined); - assert.equal(odoo.password, config.password); - }); - - it('odoo should have this public functions', function () { - assert.equal(typeof odoo.connect, 'function'); - assert.equal(typeof odoo.create, 'function'); - assert.equal(typeof odoo.get, 'function'); - assert.equal(typeof odoo.update, 'function'); - assert.equal(typeof odoo.delete, 'function'); - assert.equal(typeof odoo.search, 'function'); - }); - - it('odoo should have this private functions', function () { - assert.equal(typeof odoo._request, 'function'); - }); - - describe('Creating client', function () { - - it('client should not be able to connect to odoo server', function (done) { - var client = new Odoo({ - host: config.host, - database: 'DatabaseNotFound', - username: config.username, - password: config.password - }), - callback = sinon.spy(); - - client.connect(callback); - - setTimeout(function () { - assert(callback.called); - assert.equal(typeof callback.args[0][0], 'object'); - assert.equal(callback.args[0][1], null); - - done(); - }, 2000); - }); - - it('client should be able to connect to odoo server', function (done) { - var callback = sinon.spy(); - - odoo.connect(callback); - - setTimeout(function () { - assert(callback.calledWith(null)); - assert.equal(typeof callback.args[0][1], 'object'); - assert(odoo.uid); - assert(odoo.sid); - assert(odoo.session_id); - assert(odoo.context); - - done(); - }, 2000); - }); - - }); - - describe('Records', function () { - - var created; - - it('client should create a record', function (done) { - var callback = sinon.spy(); - odoo.create('hr.employee', { - name: 'John Doe', - work_email: 'john@doe.com' - }, callback); - - setTimeout(function () { - assert(callback.calledWith(null)); - assert.equal(typeof callback.args[0][1], 'number'); - - created = callback.args[0][1]; - - done(); - }, 2000); - - }); - - it('client should get a record', function (done) { - var callback = sinon.spy(); - odoo.get('hr.employee', created, callback); - - setTimeout(function () { - assert(callback.calledWith(null)); - assert.equal(typeof callback.args[0][1], 'object'); - assert.equal(callback.args[0][1].display_name, 'John Doe'); - assert.equal(callback.args[0][1].work_email, 'john@doe.com'); - - done(); - }, 2000); - - }); - - it('client should update a record', function (done) { - var callback = sinon.spy(); - odoo.update('hr.employee', created, { - name: 'Jane Doe', - work_email: 'jane@doe.com' - }, callback); - - setTimeout(function () { - assert(callback.calledWith(null)); - assert(callback.args[0][1]); - - done(); - }, 2000); - }); - - it('client should delete a record', function (done) { - var callback = sinon.spy(); - odoo.delete('hr.employee', created, callback); - - setTimeout(function () { - assert(callback.calledWith(null)); - assert(callback.args[0][1]); - - done(); - }, 2000); - }); - - it('client should search records', function (done) { - var callback = sinon.spy(); - odoo.search('hr.employee', [['login', '=', 'admin']], callback); - - setTimeout(function () { - assert(callback.calledWith(null)); - assert.equal(typeof callback.args[0][1], 'array'); - - done(); - }, 2000); - }); - - }); - -}); From 21b03937de6069be609fc592486d7afe14b5224f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Tue, 7 Aug 2018 20:20:00 -0400 Subject: [PATCH 08/29] Code cleanup and formatting --- lib/index.js | 65 +++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/lib/index.js b/lib/index.js index c1bf206..7601308 100644 --- a/lib/index.js +++ b/lib/index.js @@ -40,7 +40,7 @@ Odoo.prototype.connect = function () { return response.json() }) .then(function (responseJson) { - if(responseJson.error) return { success: false, error: responseJson.error } + if (responseJson.error) return { success: false, error: responseJson.error } else { context.uid = responseJson.result.uid context.session_id = responseJson.result.session_id @@ -56,9 +56,6 @@ Odoo.prototype.connect = function () { // Search records Odoo.prototype.search = function (model, params) { - //assert(params.ids, "Must provide a list of IDs."); - //assert(params.domain, "Must provide a search domain."); - return this._request('/web/dataset/call_kw', { kwargs: { context: this.context @@ -72,12 +69,7 @@ Odoo.prototype.search = function (model, params) { } // Search & Read records -// https://www.odoo.com/documentation/8.0/api_integration.html#search-and-read -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.search -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.read Odoo.prototype.search_read = function (model, params) { - //assert(params.domain, "'domain' parameter required. Must provide a search domain."); - //assert(params.limit, "'limit' parameter required. Must specify max. number of results to return."); return this._request('/web/dataset/call_kw', { model: model, @@ -94,12 +86,8 @@ Odoo.prototype.search_read = function (model, params) { }) } -// Get record -// https://www.odoo.com/documentation/8.0/api_integration.html#read-records -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.read +// Read records Odoo.prototype.get = function (model, params) { - //assert(params.ids, "Must provide a list of IDs."); - return this._request('/web/dataset/call_kw', { model: model, method: 'read', @@ -115,17 +103,16 @@ Odoo.prototype.get = function (model, params) { // Browse records by ID // Not a direct implementation of Odoo RPC 'browse' but rather a workaround based on 'search_read' -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.browse -Odoo.prototype.browse_by_id = function(model, params, callback) { - params.domain = [['id', '>', '0' ]] // assumes all records IDs are > 0 +Odoo.prototype.browse_by_id = function (model, params) { + params.domain = [['id', '>', '0']] this.search_read(model, params) - .then(function (response) { - return response - }) + .then(function (response) { + return response + }) } -// Create record +// Create records Odoo.prototype.create = function (model, params) { return this._request('/web/dataset/call_kw', { kwargs: { @@ -137,7 +124,7 @@ Odoo.prototype.create = function (model, params) { }) } -// Update record +// Update records Odoo.prototype.update = function (model, ids, params) { if (ids) { return this._request('/web/dataset/call_kw', { @@ -151,28 +138,22 @@ Odoo.prototype.update = function (model, ids, params) { } } -// Delete record -Odoo.prototype.delete = function (model, id) { - return this._request('/web/dataset/call_kw', { - kwargs: { - context: this.context - }, - model: model, - method: 'unlink', - args: [[id]] - }) +// Delete records +Odoo.prototype.delete = function (model, ids) { + if (ids) { + return this._request('/web/dataset/call_kw', { + kwargs: { + context: this.context + }, + model: model, + method: 'unlink', + args: [ids] + }) + } } - // Generic RPC wrapper -// DOES NOT AUTO-INCLUDE context Odoo.prototype.rpc_call = function (endpoint, params) { - //assert(params.model); - // assert(params.method); - // assert(params.args); - // assert(params.kwargs); - // assert(params.kwargs.context); - return this._request(endpoint, params) } @@ -180,7 +161,7 @@ Odoo.prototype.rpc_call = function (endpoint, params) { Odoo.prototype._request = function (path, params) { params = params || {} - var url = 'http://' + this.host + ':' + this.port + (path || '/') + '' + var url = this.host + ':' + this.port + (path || '/') var options = { method: 'POST', headers: { @@ -201,7 +182,7 @@ Odoo.prototype._request = function (path, params) { return response.json() }) .then(function (responseJson) { - if(responseJson.error) return { success: false, error: responseJson.error } + if (responseJson.error) return { success: false, error: responseJson.error } else return { success: true, data: responseJson.result } }) .catch(function (error) { From 6431c130c46b7191d5477f5b6725c8cd8d0e3b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Tue, 7 Aug 2018 20:20:19 -0400 Subject: [PATCH 09/29] Update README.md --- README.md | 248 +++++++++++++++++++++++------------------------------- 1 file changed, 107 insertions(+), 141 deletions(-) diff --git a/README.md b/README.md index 10b5efe..2289b32 100644 --- a/README.md +++ b/README.md @@ -1,172 +1,138 @@ -# Odoo +# react-native-odoo-promise-based -React Native library for Odoo +React Native library for Odoo using the Fetch API and JSON-RPC. + +This library is a modified version from the original repository and was created to allow authentication using Odoo session ids for in-app session persistence. Also server response handling was reimplemented using Promises instead of callback functions for convenience. + +**This package is still a work in progress** and hasn't been subjected to proper unit testing after the original repository was forked. ## Installation ```bash -$ npm install react-native-odoo +$ npm install react-native-odoo-promise-based +``` +or +```bash +$ yarn add react-native-odoo-promise-based ``` ## Usage +Please refer to the Odoo [API documentation](https://www.odoo.com/documentation/11.0/webservices/odoo.html) if you need help structuring your database queries. I'll try to make a more thorough explanation in the future about how it works. + +**Creating Odoo connection instance** +Before executing any kind of query operations, a connection instance must be established either with a username/password or with a previously retrieved session id. ```js -import Odoo from 'odoo' +import Odoo from 'react-native-odoo-promise-based' +/* Create new Odoo connection instance */ const odoo = new Odoo({ - host: 'localhost', - port: 8069, - database: 'demo', - username: 'admin', - password: 'admin' -}); - -// Connect to Odoo -odoo.connect(function (err) { - if (err) { return console.log(err); } -}); - -// Get a partner -// https://www.odoo.com/documentation/8.0/api_integration.html#read-records -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.read -var params = { + host: 'YOUR_SERVER_ADDRESS', + port: 8069 /* Defaults to 80 if not specified */ + database: 'YOUR_DATABASE_NAME', + username: 'YOUR_USERNAME' /* Optional if using a stored session_id */ + password: 'YOUR_PASSWORD' /* Optional if using a stored session_id */ + sid: 'YOUR_SESSION_ID' /* Optional if using username/password */ +}) + +``` + +**Connect** +Returns a promise which resolves into an object containing the current users' data, including a session id which can be stored for future connections and session persistence. +```js +odoo.connect() +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) +``` + +**Get (Odoo Read)** +Receives an Odoo database `model` string and a `params` object containing the `ids` you want to read and the `fields` you want to retrieve from each result. + +Returns a promise which resolves to an array of results matching the array of ids provided in the parameters. +```js +/* Get partner from server example */ +const params = { ids: [1,2,3,4,5], fields: [ 'name' ], -}; //params -odoo.get('res.partner', params, function (err, partners) { - if (err) { return console.log(err); } - - console.log(partners); -// [ -// { id: 1, name: 'Demo Company' }, -// { id: 3, name: 'Administrator' }, -// { id: 4, name: 'Public user' }, -// { id: 5, name: 'Demo User' } -// ] -}); //get - - -// Search & Get products in one RPC call -// https://www.odoo.com/documentation/8.0/api_integration.html#search-and-read -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.search -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.read -var params = { +} + +odoo.get('res.partner', params) +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) +``` + +**Search & Read items** +Just like the Get method, this one also receives a `model` string and a `params` object. With this method the parameters may include a `domain` array for filtering purposes (with filter statements similar to SQL's `WHERE`), `limit` and `offset` values for pagination and an `order` property which can be set to specific fields. The array of `ids` becomes optional. + +Returns a promised which resolves to an array of results matching the parameters provided. +```js +const params = { ids: [1,2,3,4,5], domain: [ [ 'list_price', '>', '50' ], [ 'list_price', '<', '65' ] ], fields: [ 'name', 'list_price', 'items' ], - order: 'list_price', - limit: 5, - offset: 0, -}; //params -odoo.search_read('product.product', params, function (err, products) { - if (err) { return console.log(err); } - - console.log(products); -// [ -// { list_price: 60, id: 52, name: 'Router R430' }, -// { list_price: 62, id: 39, name: 'Headset standard' } -// ] - -}); //search_read - - -// Browse products by ID -// Not a direct implementation of Odoo RPC 'browse' but rather a workaround based on 'search_read' -// https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.browse -var params = { - fields: [ 'name', 'list_price'], - limit: 5, - offset: 0, -}; //params -odoo.browse_by_id('product.product', params, function (err, products) { - if (err) { return console.log(err); } - - console.log(products); -// [ -// { list_price: 4.49, id: 1180, name: 'Fruit Cup' }, -// { list_price: 0, id: 1139, name: 'Orange Crush' }, -// { list_price: 1.59, id: 1062, name: 'Blueberry muffin' }, -// { list_price: 1.35, id: 1381, name: 'Otis Harvest Bran' } -// ] -}); //browse_by_id - - -// Generic RPC call -// Note that, unlike the other methods, the context is not automatically included -var endpoint = '/web/dataset/call_kw'; -var model = 'sale.order'; -var method = 'action_view_delivery'; - -var args = [ - [sale_order_id], - { - tz: odoo.context.tz, - uid: odoo.context.uid, - }, -];//args - -var params = { - model: model, - method: method, - args: args, - kwargs: {}, -};//params - -// View Delivery Order -odoo.rpc_call(endpoint, params, function(err, result) { - if(err) { - console.log(err); - return; - }//if - - var delivery_order = result; - - console.log(delivery_order); -});//odoo.rpc_call + order: 'list_price DESC', + limit: 5, + offset: 0, +} + +odoo.search_read('product.product', params) +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) +``` +**Create** +Receives a `model` string and a `params` object with properties corresponding to the fields you want to write in the row. +```js +odoo.create('delivery.order.line', { + sale_order_id: 123 + delivered: 'false', +}) +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) ``` -## Methods -* odoo.connect(callback) -* odoo.get(model, id, callback) -* odoo.search(model, params, callback) -* odoo.search_read(model, params, callback) -* odoo.browse_by_id(model, params, callback) -* odoo.create(model, params, callback) -* odoo.update(model, id, params, callback) -* odoo.delete(model, id, callback) -* odoo.rpc_call(endpoint, params, callback); +**Update** +Receives a `model` string, an array of `ids` related to the rows you want to update in the database and a `params` object with properties corresponding to the fields that are going to be updated. -##Node version -Works better with NodeJS v11.16 and further +If you need to update several rows in the database you can take advantage of the Promises API to generate and populate an array of promises by iterating through each operation and then resolving all of them using `Promise.all()` +```js +odoo.update('delivery.order.line', [ids], { + delivered: 'true', + delivery_note: 'Delivered on time!' +}) +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) +``` -## Reference +**Delete** +Receives an Odoo database `model` string and an `ids` array corresponding to the rows you want to delete in the database. -* [Odoo Technical Documentation](https://www.odoo.com/documentation/8.0) -* [Odoo Web Service API](https://www.odoo.com/documentation/8.0/api_integration.html) +```js +odoo.delete('delivery.order.line', [ids]) +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) +``` -## License +**Custom RPC Call** +If you wish to execute a custom RPC call not represented in this library's methods, you can also run a custom call by passing an `endpoint` string and a `params` object. This requires understanding how the Odoo Web API works more thoroughly so you can properly structure the functions parameters. -The MIT License (MIT) +```js +odoo.rpc_call('/web/dataset/call_kw', params) +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) +``` -Copyright (c) 2015 Marco Godínez, 4yopping and all the related trademarks +## References -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +* [Odoo ORM API Reference](https://www.odoo.com/documentation/11.0/reference/orm.html) + +* [Odoo Web Service External API](https://www.odoo.com/documentation/11.0/webservices/odoo.html) + +## License +This project is licensed under the MIT License - see the [LICENSE.md](https://github.com/cesar-gutierrez/react-native-odoo/LICENSE.md) file for details -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +## Acknowledgements -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +This project was built upon previous versions. It is a fork of [kevin3274](https://github.com/kevin3274/react-native-odoo)'s original React Native library which in turn is based of [saidimu](https://github.com/saidimu/odoo)'s Node.js library. \ No newline at end of file From cf3ea8c0df33d9de66338ba10f7de6bc88151e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Tue, 7 Aug 2018 20:21:25 -0400 Subject: [PATCH 10/29] Update semver to 0.6.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9c64b2e..a3a1cab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.1.0", + "version": "0.6.0", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", From 7aadca285fbac45de89916b153561fb7ad5b38c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Sim=C3=B3n=20Perez?= Date: Wed, 15 Aug 2018 10:12:39 -0400 Subject: [PATCH 11/29] fix: Remove http url prefix --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 7601308..8cdaf4e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,7 +20,7 @@ Odoo.prototype.connect = function () { } var json = JSON.stringify({ params: params }) - var url = 'http://' + this.host + ':' + this.port + '/web/session/authenticate' + var url = this.host + ':' + this.port + '/web/session/authenticate' var options = { method: 'POST', From 51a12bc93c11eda360c51b6f727e5de23b9f3d3f Mon Sep 17 00:00:00 2001 From: nichita Date: Thu, 22 Nov 2018 21:22:40 +0200 Subject: [PATCH 12/29] Fix network error. --- lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8cdaf4e..e1b72c6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,7 +20,7 @@ Odoo.prototype.connect = function () { } var json = JSON.stringify({ params: params }) - var url = this.host + ':' + this.port + '/web/session/authenticate' + var url = 'http://' + this.host + ':' + this.port + '/web/session/authenticate' var options = { method: 'POST', @@ -161,7 +161,7 @@ Odoo.prototype.rpc_call = function (endpoint, params) { Odoo.prototype._request = function (path, params) { params = params || {} - var url = this.host + ':' + this.port + (path || '/') + var url = 'http://' + this.host + ':' + this.port + (path || '/') var options = { method: 'POST', headers: { From e25983437a852f01ca1a269c4bc84be092f4d915 Mon Sep 17 00:00:00 2001 From: nichita Date: Thu, 22 Nov 2018 21:24:18 +0200 Subject: [PATCH 13/29] Fix getting session id from connect response. --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index e1b72c6..227a436 100644 --- a/lib/index.js +++ b/lib/index.js @@ -36,7 +36,7 @@ Odoo.prototype.connect = function () { return fetch(url, options) .then(function (response) { - context.sid = response.headers.map['set-cookie'][0].split(';')[0] + context.sid = response.headers.map['set-cookie'].split(';')[0].split('=')[1]; return response.json() }) .then(function (responseJson) { From 6391495e9ec979f26ff40c4f7b6fce08e7de420d Mon Sep 17 00:00:00 2001 From: nichita Date: Thu, 22 Nov 2018 21:25:04 +0200 Subject: [PATCH 14/29] Send set-cookie in requests. --- lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 227a436..28f6482 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,6 +37,7 @@ Odoo.prototype.connect = function () { return fetch(url, options) .then(function (response) { context.sid = response.headers.map['set-cookie'].split(';')[0].split('=')[1]; + context.cookie = response.headers.map['set-cookie']; return response.json() }) .then(function (responseJson) { @@ -167,7 +168,7 @@ Odoo.prototype._request = function (path, params) { headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', - 'Cookie': this.sid + ';' + 'Cookie': this.cookie }, body: JSON.stringify({ jsonrpc: '2.0', From 7d20ca416f0ec19a3c4534edb4381e65777cb5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Fri, 30 Nov 2018 08:45:51 -0400 Subject: [PATCH 15/29] Update version to v0.6.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a3a1cab..17fcc4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.0", + "version": "0.6.1", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", From 1913499dc7a6cfda69d7cb75e7108e7656499849 Mon Sep 17 00:00:00 2001 From: nichita Date: Tue, 4 Dec 2018 17:07:49 +0200 Subject: [PATCH 16/29] Add possibility to send custom context. --- README.md | 10 +++++----- lib/index.js | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2289b32..1cdb20c 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ const params = { fields: [ 'name' ], } -odoo.get('res.partner', params) +odoo.get('res.partner', params, context) .then(response => { /* ... */ }) .catch(e => { /* ... */ }) ``` @@ -75,7 +75,7 @@ const params = { offset: 0, } -odoo.search_read('product.product', params) +odoo.search_read('product.product', params, context) .then(response => { /* ... */ }) .catch(e => { /* ... */ }) ``` @@ -87,7 +87,7 @@ Receives a `model` string and a `params` object with properties corresponding to odoo.create('delivery.order.line', { sale_order_id: 123 delivered: 'false', -}) +}, context) .then(response => { /* ... */ }) .catch(e => { /* ... */ }) ``` @@ -101,7 +101,7 @@ If you need to update several rows in the database you can take advantage of the odoo.update('delivery.order.line', [ids], { delivered: 'true', delivery_note: 'Delivered on time!' -}) +}, context) .then(response => { /* ... */ }) .catch(e => { /* ... */ }) ``` @@ -110,7 +110,7 @@ odoo.update('delivery.order.line', [ids], { Receives an Odoo database `model` string and an `ids` array corresponding to the rows you want to delete in the database. ```js -odoo.delete('delivery.order.line', [ids]) +odoo.delete('delivery.order.line', [ids], context) .then(response => { /* ... */ }) .catch(e => { /* ... */ }) ``` diff --git a/lib/index.js b/lib/index.js index 28f6482..8410f68 100644 --- a/lib/index.js +++ b/lib/index.js @@ -56,10 +56,10 @@ Odoo.prototype.connect = function () { } // Search records -Odoo.prototype.search = function (model, params) { +Odoo.prototype.search = function (model, params, context) { return this._request('/web/dataset/call_kw', { kwargs: { - context: this.context + context: {...this.context, ...context} }, model: model, method: 'search', @@ -70,14 +70,14 @@ Odoo.prototype.search = function (model, params) { } // Search & Read records -Odoo.prototype.search_read = function (model, params) { +Odoo.prototype.search_read = function (model, params, context) { return this._request('/web/dataset/call_kw', { model: model, method: 'search_read', args: [], kwargs: { - context: this.context, + context: {...this.context, ...context}, domain: params.domain, offset: params.offset, limit: params.limit, @@ -88,7 +88,7 @@ Odoo.prototype.search_read = function (model, params) { } // Read records -Odoo.prototype.get = function (model, params) { +Odoo.prototype.get = function (model, params, context) { return this._request('/web/dataset/call_kw', { model: model, method: 'read', @@ -96,6 +96,7 @@ Odoo.prototype.get = function (model, params) { params.ids, ], kwargs: { + context: {...this.context, ...context}, fields: params.fields, }, }) @@ -114,10 +115,10 @@ Odoo.prototype.browse_by_id = function (model, params) { // Create records -Odoo.prototype.create = function (model, params) { +Odoo.prototype.create = function (model, params, context) { return this._request('/web/dataset/call_kw', { kwargs: { - context: this.context + context: {...this.context, ...context} }, model: model, method: 'create', @@ -126,11 +127,11 @@ Odoo.prototype.create = function (model, params) { } // Update records -Odoo.prototype.update = function (model, ids, params) { +Odoo.prototype.update = function (model, ids, params, context) { if (ids) { return this._request('/web/dataset/call_kw', { kwargs: { - context: this.context + context: {...this.context, ...context} }, model: model, method: 'write', @@ -140,11 +141,11 @@ Odoo.prototype.update = function (model, ids, params) { } // Delete records -Odoo.prototype.delete = function (model, ids) { +Odoo.prototype.delete = function (model, ids, context) { if (ids) { return this._request('/web/dataset/call_kw', { kwargs: { - context: this.context + context: {...this.context, ...context} }, model: model, method: 'unlink', From a3d1996e136af9e6a5fd456b5ceee637750a677a Mon Sep 17 00:00:00 2001 From: nichita Date: Tue, 4 Dec 2018 17:16:35 +0200 Subject: [PATCH 17/29] Add possibility configure data transfer protocol. --- lib/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8410f68..292eaa5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,7 @@ var Odoo = function (config) { this.username = config.username || null this.password = config.password || null this.sid = config.sid || null + this.protocol = config.protocol || 'http' } // Connect @@ -20,7 +21,7 @@ Odoo.prototype.connect = function () { } var json = JSON.stringify({ params: params }) - var url = 'http://' + this.host + ':' + this.port + '/web/session/authenticate' + var url = `${this.protocol}://${this.host}:${this.port}/web/session/authenticate` var options = { method: 'POST', @@ -163,7 +164,7 @@ Odoo.prototype.rpc_call = function (endpoint, params) { Odoo.prototype._request = function (path, params) { params = params || {} - var url = 'http://' + this.host + ':' + this.port + (path || '/') + var url = `${this.protocol}://${this.host}:${this.port}${path || '/'}` var options = { method: 'POST', headers: { From be131dc93fd7008bf40751254eca738ab6f62d26 Mon Sep 17 00:00:00 2001 From: nichita Date: Thu, 24 Jan 2019 18:37:35 +0200 Subject: [PATCH 18/29] Update package version and add new maintainer name. --- package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 17fcc4c..3ae3f4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.1", + "version": "0.6.2", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", @@ -32,6 +32,10 @@ { "name": "César Gutiérrez Olivares", "email": "hello@cesar.codes" + }, + { + "name": "Nichita Gruia", + "email": "gruianichita@gmail.com" } ] } From d050ebee8a5aaa565cadedbe5b582af615f4f20d Mon Sep 17 00:00:00 2001 From: nichita Date: Thu, 24 Jan 2019 18:56:48 +0200 Subject: [PATCH 19/29] Update connection instance in readme file. --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1cdb20c..b4d67b7 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,12 @@ import Odoo from 'react-native-odoo-promise-based' /* Create new Odoo connection instance */ const odoo = new Odoo({ host: 'YOUR_SERVER_ADDRESS', - port: 8069 /* Defaults to 80 if not specified */ + port: 8069, /* Defaults to 80 if not specified */ database: 'YOUR_DATABASE_NAME', - username: 'YOUR_USERNAME' /* Optional if using a stored session_id */ - password: 'YOUR_PASSWORD' /* Optional if using a stored session_id */ - sid: 'YOUR_SESSION_ID' /* Optional if using username/password */ + username: 'YOUR_USERNAME', /* Optional if using a stored session_id */ + password: 'YOUR_PASSWORD', /* Optional if using a stored session_id */ + sid: 'YOUR_SESSION_ID', /* Optional if using username/password */ + protocol: 'https' /* Defaults to http if not specified */ }) ``` From ed65077afa2840598781e831315345cba9335837 Mon Sep 17 00:00:00 2001 From: nichita Date: Thu, 24 Jan 2019 19:04:36 +0200 Subject: [PATCH 20/29] Update package version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3ae3f4e..6666f89 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.2", + "version": "0.6.3", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", From a9abdc6ef774fa329ec34d924dee8c22f01b0681 Mon Sep 17 00:00:00 2001 From: aidoskz Date: Thu, 14 Mar 2019 11:56:29 +0600 Subject: [PATCH 21/29] Add Read Group method --- README.md | 25 ++++++++++++++++++++++++- lib/index.js | 16 ++++++++++++++++ package.json | 6 +++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b4d67b7..4f42e18 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,29 @@ odoo.search_read('product.product', params, context) .catch(e => { /* ... */ }) ``` +**Read Group** +Just like the Search & read, but get the list of records in list view grouped by the given ``groupby`` fields. +If ``lazy`` true, the results are only grouped by the first groupby and the remaining groupbys are put in the __context key. +If ``lazy`` false, all the groupbys are done in one call. +Returns a promised which resolves to an array of results matching the parameters provided. +Array containing: all the values of fields grouped by the fields in ``groupby`` argument, +``__domain``: array of list specifying the search criteria, ``__context``: array with argument like ``groupby`` +```js +const params = { + domain: [ ["project_id", "=", 7] ], + fields: [ "color", "stage_id" , "sequence"], + groupby: [ + "stage_id" + ], + order: 'sequence', + lazy: true +} + +odoo.read_group('project.task', params, context) +.then(response => { /* ... */ }) +.catch(e => { /* ... */ }) +``` + **Create** Receives a `model` string and a `params` object with properties corresponding to the fields you want to write in the row. @@ -136,4 +159,4 @@ This project is licensed under the MIT License - see the [LICENSE.md](https://g ## Acknowledgements -This project was built upon previous versions. It is a fork of [kevin3274](https://github.com/kevin3274/react-native-odoo)'s original React Native library which in turn is based of [saidimu](https://github.com/saidimu/odoo)'s Node.js library. \ No newline at end of file +This project was built upon previous versions. It is a fork of [kevin3274](https://github.com/kevin3274/react-native-odoo)'s original React Native library which in turn is based of [saidimu](https://github.com/saidimu/odoo)'s Node.js library. diff --git a/lib/index.js b/lib/index.js index 292eaa5..6de9b33 100644 --- a/lib/index.js +++ b/lib/index.js @@ -103,6 +103,22 @@ Odoo.prototype.get = function (model, params, context) { }) } +// Read Group +Odoo.prototype.read_group = function(model, params, context) { + return this._request("/web/dataset/call_kw", { + model: model, + method: "read_group", + args: [], + kwargs: { + context: { ...this.context, ...context }, + domain: params.domain, + fields: params.fields, + groupby: params.groupby, + lazy: params.lazy, + orderby: params.order + }, + }) +} // Browse records by ID // Not a direct implementation of Odoo RPC 'browse' but rather a workaround based on 'search_read' diff --git a/package.json b/package.json index 6666f89..39b61d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.3", + "version": "0.6.4", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", @@ -36,6 +36,10 @@ { "name": "Nichita Gruia", "email": "gruianichita@gmail.com" + }, + { + "name": "Aidos Kakimzhanov", + "email": "aidos.kakimzhan@gmail.com" } ] } From 64a90db91420cd0254721c9eb1616b2565d72e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=C3=A9rian=20Dorcy?= Date: Mon, 7 Oct 2019 18:37:17 +0200 Subject: [PATCH 22/29] Fix issue with react-native cookie Fix the issue with the cookie based authentication. Add credentials:omit parameter at each fetch call --- lib/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 6de9b33..1f7259e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,7 +30,8 @@ Odoo.prototype.connect = function () { 'Accept': 'application/json', 'Content-Length': json.length }, - body: json + body: json, + credentials: 'omit' } var context = this @@ -188,6 +189,7 @@ Odoo.prototype._request = function (path, params) { 'Accept': 'application/json', 'Cookie': this.cookie }, + credentials: 'omit', body: JSON.stringify({ jsonrpc: '2.0', id: new Date().getUTCMilliseconds(), From 0a0e709b7e4a0734b0ec2d29730467b3aa32d0d2 Mon Sep 17 00:00:00 2001 From: Nichita Gruia Date: Sun, 19 Jan 2020 11:50:58 +0200 Subject: [PATCH 23/29] Fix double session key for IOS devices. --- lib/index.js | 3 +-- package.json | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6de9b33..c41da4f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -185,8 +185,7 @@ Odoo.prototype._request = function (path, params) { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Accept': 'application/json', - 'Cookie': this.cookie + 'Accept': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', diff --git a/package.json b/package.json index 39b61d5..cab819a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.4", + "version": "0.6.5", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", @@ -17,6 +17,7 @@ "directories": { "test": "test" }, + "publishConfig": { "registry": "https://npm.pkg.github.com/" }, "repository": { "type": "git", "url": "git+https://github.com/cesar-gutierrez/react-native-odoo-promise-based" From 2486766d98c5d7a0850688c3839a7184f5215824 Mon Sep 17 00:00:00 2001 From: Nichita Gruia Date: Sun, 19 Jan 2020 12:27:14 +0200 Subject: [PATCH 24/29] Format package json file. --- package.json | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index cab819a..9b1a381 100644 --- a/package.json +++ b/package.json @@ -17,30 +17,20 @@ "directories": { "test": "test" }, - "publishConfig": { "registry": "https://npm.pkg.github.com/" }, + "publishConfig": { + "registry": "https://npm.pkg.github.com/" + }, "repository": { "type": "git", - "url": "git+https://github.com/cesar-gutierrez/react-native-odoo-promise-based" + "url": "git+https://github.com/cesar-gutierrez/react-native-odoo-promise-based.git" }, "scripts": { "test": "mocha test" }, - "author": { - "name": "César Gutiérrez Olivares", - "email": "hello@cesar.codes" - }, + "author": "César Gutiérrez Olivares ", "maintainers": [ - { - "name": "César Gutiérrez Olivares", - "email": "hello@cesar.codes" - }, - { - "name": "Nichita Gruia", - "email": "gruianichita@gmail.com" - }, - { - "name": "Aidos Kakimzhanov", - "email": "aidos.kakimzhan@gmail.com" - } + "César Gutiérrez Olivares ", + "Nichita Gruia ", + "Aidos Kakimzhanov " ] } From bae356f55ac7a8ca7b50c7acaacec2b379a25089 Mon Sep 17 00:00:00 2001 From: Nichita Gruia Date: Sun, 19 Jan 2020 12:36:54 +0200 Subject: [PATCH 25/29] Upgrade package version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9b1a381..9b63e40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.5", + "version": "0.6.6", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", From 3e1ffca015c01e3b953c890a4f8b175592ce76bd Mon Sep 17 00:00:00 2001 From: Nichita Gruia Date: Sun, 19 Jan 2020 13:14:40 +0200 Subject: [PATCH 26/29] Remove publish config. --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 9b63e40..562255e 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,6 @@ "directories": { "test": "test" }, - "publishConfig": { - "registry": "https://npm.pkg.github.com/" - }, "repository": { "type": "git", "url": "git+https://github.com/cesar-gutierrez/react-native-odoo-promise-based.git" From 423b04c96053c0583671b271770d36850e435f60 Mon Sep 17 00:00:00 2001 From: Nichita Gruia Date: Sun, 19 Jan 2020 13:23:10 +0200 Subject: [PATCH 27/29] Upgrade version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 562255e..928bc2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.6", + "version": "0.6.7", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", From ca43dd23c92b641619bbbe24f0021c71d7ff1696 Mon Sep 17 00:00:00 2001 From: Nichita Gruia Date: Sun, 19 Jan 2020 15:27:23 +0200 Subject: [PATCH 28/29] Remove 0.6.5 changes. --- lib/index.js | 3 ++- package.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index d91e9c7..1f7259e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -186,7 +186,8 @@ Odoo.prototype._request = function (path, params) { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Accept': 'application/json' + 'Accept': 'application/json', + 'Cookie': this.cookie }, credentials: 'omit', body: JSON.stringify({ diff --git a/package.json b/package.json index 928bc2f..a90c038 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-odoo-promise-based", - "version": "0.6.7", + "version": "0.6.8", "description": "React Native library for Odoo using Fetch API and JSON-RPC", "keywords": [ "odoo", From 073a85d839a03b1ba741caf1c96aa702f77764a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Guti=C3=A9rrez=20Olivares?= Date: Wed, 2 Sep 2020 16:37:08 +0200 Subject: [PATCH 29/29] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4f42e18..88df26f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # react-native-odoo-promise-based +⚠️ This repository is no longer being maintained. Feel free to use it for reference and forking. + React Native library for Odoo using the Fetch API and JSON-RPC. This library is a modified version from the original repository and was created to allow authentication using Odoo session ids for in-app session persistence. Also server response handling was reimplemented using Promises instead of callback functions for convenience.