-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpc.js
94 lines (91 loc) · 2.7 KB
/
rpc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
async function odoo_authenticate(url, database, login, password) {
headers = new Headers({
'Content-Type': 'application/json'
});
parameters = {
method: 'POST',
headers: headers,
mode: 'cors', // https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
// referrerPolicy: "strict-origin-when-cross-origin", // no-referrer
cache: 'default',
body: JSON.stringify({
"jsonrpc": "2.0",
"id": null,
"method": "call",
"params": {
"db": database,
"login": login,
"password": password
}
})
};
var res = (await fetch(url + "/web/session/authenticate", parameters)).json();
return res;
}
async function build_odoo_rpc(url, database, login, password) {
var auth_info = await odoo_authenticate(url, database, login, password);
var uid = auth_info.result.uid;
function raw_rpc(model, method, args=[], kwargs={}) {
headers = new Headers({
'Content-Type': 'application/json'
});
parameters = {
method: 'POST',
headers: headers,
mode: 'cors', // https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
// referrerPolicy: "strict-origin-when-cross-origin", // no-referrer
cache: 'default',
body: JSON.stringify({
"jsonrpc": "2.0",
"id": null,
"method": "call",
"params": {
"service": "object",
"method": "execute_kw",
"args": [
database,
uid,
password,
model,
method,
args,
kwargs
],
}
})
};
return fetch(url + "/jsonrpc", parameters);
}
async function rpc(model, method, args=[], kwargs={}) {
return await raw_rpc(model, method, args, kwargs).then(async (response) => {
let data = await response.json();
if(data.error) {
throw data.error;
}
return data.result;
})
}
return rpc;
}
let url = "http://localhost:8069";
let db = "";
let login = "admin";
let password = "admin";
const rpc = await build_odoo_rpc(url, db, login, password);
rpc(
"res.partner",
"search_read",
[
[
["id", "<", 30]
],
["id", "name"]
],
{
limit: 5
}
).then(
(e) => {console.log(e); console.log("OK")}
).catch(
(e) => {console.log(e.data.debug);}
);