Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Odoo

Node.js client library for Odoo
React Native library for Odoo

## Installation

```bash
$ npm install Odoo
$ npm install react-native-odoo
```

## Usage

```js
var Odoo = require('Odoo');
import Odoo from 'odoo'

var odoo = new Odoo({
const odoo = new Odoo({
host: 'localhost',
port: 8069,
database: 'demo',
Expand Down
99 changes: 43 additions & 56 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
'use strict';

var assert = require('assert');

var http = require('http'),
jayson = require('jayson'),
_ = require('lodash');

var Odoo = function (config) {
config = config || {};

Expand All @@ -17,61 +11,52 @@ var Odoo = function (config) {
};

// Connect
Odoo.prototype.connect = function (cb) {

Odoo.prototype.connect = function(cb){
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 options = {
host: this.host,
port: this.port,
path: '/web/session/authenticate',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Content-Length': json.length
}
},
body: json
};

var self = this;

var req = http.request(options, function (res) {
var response = '';

res.setEncoding('utf8');

res.on('data', function (chunk) {
response += chunk;
});

res.on('end', function () {
response = JSON.parse(response);

if (response.error) {
return cb(response.error, null);
fetch(url, options)
.then(res=>{
this.sid = res.headers.map['set-cookie'][0].split(';')[0];
console.log('sid:', this.sid);
return res.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);
}

self.uid = response.result.uid;
self.sid = res.headers['set-cookie'][0].split(';')[0];
self.session_id = response.result.session_id;
self.context = response.result.user_context;

return cb(null, response.result);
}, err=>{
cb(err, null);
});
});

req.write(json);
};

// Search records
Odoo.prototype.search = function (model, params, callback) {
// assert(params.ids, "Must provide a list of IDs.");
assert(params.domain, "Must provide a search domain.");
//assert(params.domain, "Must provide a search domain.");

this._request('/web/dataset/call_kw', {
kwargs: {
Expand All @@ -90,8 +75,8 @@ Odoo.prototype.search = function (model, params, callback) {
// 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) {
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.");
//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', {
model: model,
Expand All @@ -112,7 +97,7 @@ Odoo.prototype.search_read = function (model, params, callback) {
// 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) {
assert(params.ids, "Must provide a list of IDs.");
//assert(params.ids, "Must provide a list of IDs.");

this._request('/web/dataset/call_kw', {
model: model,
Expand Down Expand Up @@ -178,7 +163,7 @@ Odoo.prototype.delete = function (model, id, callback) {
// Generic RPC wrapper
// DOES NOT AUTO-INCLUDE context
Odoo.prototype.rpc_call = function (endpoint, params, callback) {
assert(params.model);
//assert(params.model);
// assert(params.method);
// assert(params.args);
// assert(params.kwargs);
Expand All @@ -189,29 +174,31 @@ Odoo.prototype.rpc_call = function (endpoint, params, callback) {


// Private functions
Odoo.prototype._request = function (path, params, callback) {
Odoo.prototype._request = function (path, params, cb) {
params = params || {};

var url = 'http://' + this.host + ':' + this.port + (path || '/') + '';
var options = {
host: this.host,
port: this.port,
path: path || '/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Cookie': this.sid + ';'
}
},
body: JSON.stringify({jsonrpc: '2.0', id: new Date().getUTCMilliseconds(), method: 'call', params: params})
};

var client = jayson.client.http(options);

client.request('call', params, function (e, err, res) {
if (e || err) {
return callback(e || err, null);
}

return callback(null, res);
});
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);
});
};

module.exports = Odoo;
31 changes: 20 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "odoo",
"version": "0.4.0",
"description": "Node.js client library for Odoo using JSON-RPC",
"name": "react-native-odoo",
"version": "0.1.0",
"description": "React Native library for Odoo using JSON-RPC",
"main": "./lib/index.js",
"directories": {
"test": "test"
Expand All @@ -14,25 +14,34 @@
},
"repository": {
"type": "git",
"url": "https://github.com/saidimu/odoo"
"url": "git+https://github.com/kevin3274/react-native-odoo.git"
},
"keywords": [
"odoo",
"openepr",
"openerp",
"jsonrpc"
"react-native"
],
"author": "Saidimu Apale <[email protected]>",
"author": {
"name": "Kevin Wang",
"email": "[email protected]"
},
"bugs": {
"url": "https://github.com/saidimu/odoo/issues"
"url": "https://github.com/kevin327/react-native-odoo/issues"
},
"homepage": "https://github.com/saidimu/odoo",
"homepage": "https://github.com/kevin3274/react-native-odoo",
"dependencies": {
"jayson": "^1.2.1",
"lodash": "^3.10.1"
},
"devDependencies": {
"gulp": "^3.8.11",
"gulp-mocha": "^2.0.0",
"sinon": "^1.12.2"
}
},
"maintainers": [
{
"name": "kevin3274",
"email": "[email protected]"
}
],
"readme": "ERROR: No README data found!"
}