From 440f5c91845c32de2313e854480dec5dfaafbd87 Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 16 Jan 2016 17:00:44 +0800 Subject: [PATCH] [REF]change to support react-native only --- README.md | 8 ++--- lib/index.js | 99 +++++++++++++++++++++++----------------------------- package.json | 31 ++++++++++------ 3 files changed, 67 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 724c989..10b5efe 100644 --- a/README.md +++ b/README.md @@ -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', diff --git a/lib/index.js b/lib/index.js index ec4a50a..8e7a74c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 || {}; @@ -17,7 +11,8 @@ var Odoo = function (config) { }; // Connect -Odoo.prototype.connect = function (cb) { + +Odoo.prototype.connect = function(cb){ var params = { db: this.database, login: this.username, @@ -25,53 +20,43 @@ Odoo.prototype.connect = function (cb) { }; 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: { @@ -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, @@ -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, @@ -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); @@ -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; diff --git a/package.json b/package.json index e79f8b3..7e065d6 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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 ", + "author": { + "name": "Kevin Wang", + "email": "kevin_327@163.com" + }, "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": "kevin_327@163.com" + } + ], + "readme": "ERROR: No README data found!" }