2
.. i18n: XML-RPC Web services
3
.. i18n: ====================
8
.. i18n: Jump to: navigation, search
10
Jump to: navigation, search
12
.. i18n: 1. **XML-RPC**
13
.. i18n: * standard: http://www.xmlrpc.org
14
.. i18n: * RPC Over HTTP
15
.. i18n: * Function Parameters & Result encoded in XML
16
.. i18n: 2. **Principle**;
17
.. i18n: * calls to objects methodes;
18
.. i18n: o read, write
20
.. i18n: o unlink (=delete)
23
* standard: http://www.xmlrpc.org
25
* Function Parameters & Result encoded in XML
27
* calls to objects methodes;
32
.. i18n: XML-RPC is known as a web service. Web services are a set of tools that let one build distributed applications on top of existing web infrastructures. These applications use the Web as a kind of "transport layer" but don't offer a direct human interface via the browser.[1] Extensible Markup Language (XML) provides a vocabulary for describing Remote Procedure Calls (RPC), which is then transmitted between computers using the HyperText Transfer Protocol (HTTP). Effectively, RPC gives developers a mechanism for defining interfaces that can be called over a network. These interfaces can be as simple as a single function call or as complex as a large API.
34
XML-RPC is known as a web service. Web services are a set of tools that let one build distributed applications on top of existing web infrastructures. These applications use the Web as a kind of "transport layer" but don't offer a direct human interface via the browser.[1] Extensible Markup Language (XML) provides a vocabulary for describing Remote Procedure Calls (RPC), which is then transmitted between computers using the HyperText Transfer Protocol (HTTP). Effectively, RPC gives developers a mechanism for defining interfaces that can be called over a network. These interfaces can be as simple as a single function call or as complex as a large API.
36
.. i18n: XML-RPC therefore allows two or more computers running different operating systems and programs written in different languages to share processing. For example, a Java application could talk with a Perl program, which in turn talks with Python application that talks with ASP, and so on. System integrators often build custom connections between different systems, creating their own formats and protocols to make communications possible, but one can often end up with a large number of poorly documented single-use protocols. The RPC approach spares programmers the trouble of having to learn about underlying protocols, networking, and various implementation details.
38
XML-RPC therefore allows two or more computers running different operating systems and programs written in different languages to share processing. For example, a Java application could talk with a Perl program, which in turn talks with Python application that talks with ASP, and so on. System integrators often build custom connections between different systems, creating their own formats and protocols to make communications possible, but one can often end up with a large number of poorly documented single-use protocols. The RPC approach spares programmers the trouble of having to learn about underlying protocols, networking, and various implementation details.
40
.. i18n: XML-RPC can be used with Python, Java, Perl, PHP, C, C++, Ruby, Microsoft’s .NET and many other programming languages. Implementations are widely available for platforms such as Unix, Linux, Windows and the Macintosh.
42
XML-RPC can be used with Python, Java, Perl, PHP, C, C++, Ruby, Microsoft’s .NET and many other programming languages. Implementations are widely available for platforms such as Unix, Linux, Windows and the Macintosh.
44
.. i18n: An XML-RPC call is conducted between two parties: the client (the calling process) and the server (the called process). A server is made available at a particular URL (such as http://example.org:8080/rpcserv/).
46
An XML-RPC call is conducted between two parties: the client (the calling process) and the server (the called process). A server is made available at a particular URL (such as http://example.org:8080/rpcserv/).
48
.. i18n: The above text just touches the surface of XML-RPC. I recommend O'Reilly's "Programming Web Service with XML-RPC" for further reading. One may also wish to review the following links:
50
The above text just touches the surface of XML-RPC. I recommend O'Reilly's "Programming Web Service with XML-RPC" for further reading. One may also wish to review the following links:
52
.. i18n: XML-RPC Home Page\\ XML-RPC for C and C++\\ The Apache XML-RPC Project\\ Expat: The XML Parser\\
54
XML-RPC Home Page\\ XML-RPC for C and C++\\ The Apache XML-RPC Project\\ Expat: The XML Parser\\
68
.. i18n: XML-RPC Architecture
69
.. i18n: """"""""""""""""""""
74
.. i18n: Open ERP is a based on a client/server architecture. The server and the client(s) communicate using the XML-RPC protocol. XML-RPC is a very simple protocol which allows the client to do remote procedure calls. The called function, its arguments, and the result of the call are transported using HTTP and encoded using XML. For more information on XML-RPC, please see: http://www.xml-rpc.com.
76
Open ERP is a based on a client/server architecture. The server and the client(s) communicate using the XML-RPC protocol. XML-RPC is a very simple protocol which allows the client to do remote procedure calls. The called function, its arguments, and the result of the call are transported using HTTP and encoded using XML. For more information on XML-RPC, please see: http://www.xml-rpc.com.
84
.. i18n: The diagram below synthesizes the client server architecture of Open ERP. Open ERP server and Open ERP clients communicate using XML-RPC.
86
The diagram below synthesizes the client server architecture of Open ERP. Open ERP server and Open ERP clients communicate using XML-RPC.
88
.. i18n: .. figure:: images/tech_arch.png
90
.. i18n: :align: center
92
.. figure:: images/tech_arch.png
100
.. i18n: The logic of Open ERP is configured on the server side. The client is very simple; it is only used to post data (forms, lists, trees) and to send back the result to the server. The updates and the addition of new functionality don't need the clients to be frequently upgraded. This makes Open ERP easier to maintain.
102
The logic of Open ERP is configured on the server side. The client is very simple; it is only used to post data (forms, lists, trees) and to send back the result to the server. The updates and the addition of new functionality don't need the clients to be frequently upgraded. This makes Open ERP easier to maintain.
104
.. i18n: The client doesn't understand what it posts. Even actions like 'Click on the print icon' are sent to the server to ask how to react.
106
The client doesn't understand what it posts. Even actions like 'Click on the print icon' are sent to the server to ask how to react.
108
.. i18n: The client operation is very simple; when a user makes an action (save a form, open a menu, print, ...) it sends this action to the server. The server then sends the new action to execute to the client.
110
The client operation is very simple; when a user makes an action (save a form, open a menu, print, ...) it sends this action to the server. The server then sends the new action to execute to the client.
112
.. i18n: There are three types of action;
114
There are three types of action;
116
.. i18n: * Open a window (form or tree)
117
.. i18n: * Print a document
118
.. i18n: * Execute a wizard
120
* Open a window (form or tree)
130
.. i18n: Access tiny-server using xml-rpc
131
.. i18n: """"""""""""""""""""""""""""""""
133
Access tiny-server using xml-rpc
134
""""""""""""""""""""""""""""""""
142
.. i18n: * **Create a partner and his address**
144
* **Create a partner and his address**
146
.. i18n: import xmlrpclib
147
.. i18n: """"""""""""""""
150
.. i18n: username = 'admin' #the user
151
.. i18n: pwd = 'admin' #the password of the user
152
.. i18n: dbname = 'terp' #the database
158
username = 'admin' #the user
159
pwd = 'admin' #the password of the user
160
dbname = 'terp' #the database
162
.. i18n: # Get the uid
163
.. i18n: sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
164
.. i18n: uid = sock_common.login(dbname, username, pwd)
167
sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
168
uid = sock_common.login(dbname, username, pwd)
170
.. i18n: #replace localhost with the address of the server
171
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
173
#replace localhost with the address of the server
174
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
177
.. i18n: 'name': 'Fabien Pinckaers',
178
.. i18n: 'lang': 'fr_FR',
182
'name': 'Fabien Pinckaers',
186
.. i18n: partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
188
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
191
.. i18n: 'partner_id': partner_id,
192
.. i18n: 'type' : 'default',
193
.. i18n: 'street': 'Chaussée de Namur 40',
194
.. i18n: 'zip': '1367',
195
.. i18n: 'city': 'Grand-Rosière',
196
.. i18n: 'phone': '+3281813700',
197
.. i18n: 'fax': '+3281733501',
201
'partner_id': partner_id,
203
'street': 'Chaussée de Namur 40',
205
'city': 'Grand-Rosière',
206
'phone': '+3281813700',
207
'fax': '+3281733501',
210
.. i18n: address_id = sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
212
address_id = sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
214
.. i18n: * **Search a partner**
217
.. i18n: args = [('vat', '=', 'ZZZZZZ')] #query clause
218
.. i18n: ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', args)
220
.. i18n: * **Read partner data**
223
.. i18n: fields = ['name', 'active', 'vat', 'ref'] #fields to read
224
.. i18n: data = sock.execute(dbname, uid, pwd, 'res.partner', 'read', ids, fields) #ids is a list of id
226
.. i18n: * **Update partner data**
229
.. i18n: values = {'vat': 'ZZ1ZZZ'} #data to update
230
.. i18n: result = sock.execute(dbname, uid, pwd, 'res.partner', 'write', ids, values)
232
.. i18n: * **Delete partner**
235
.. i18n: # ids : list of id
236
.. i18n: result = sock.execute(dbname, uid, pwd, 'res.partner', 'unlink', ids)
238
* **Search a partner**
241
args = [('vat', '=', 'ZZZZZZ')] #query clause
242
ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', args)
244
* **Read partner data**
247
fields = ['name', 'active', 'vat', 'ref'] #fields to read
248
data = sock.execute(dbname, uid, pwd, 'res.partner', 'read', ids, fields) #ids is a list of id
250
* **Update partner data**
253
values = {'vat': 'ZZ1ZZZ'} #data to update
254
result = sock.execute(dbname, uid, pwd, 'res.partner', 'write', ids, values)
260
result = sock.execute(dbname, uid, pwd, 'res.partner', 'unlink', ids)
268
.. i18n: Access Open-server using xml-rpc
269
.. i18n: """"""""""""""""""""""""""""""""
271
Access Open-server using xml-rpc
272
""""""""""""""""""""""""""""""""
274
.. i18n: **Download the XML-RPC framework for PHP**
276
**Download the XML-RPC framework for PHP**
278
.. i18n: windows / linux: download the xml-rpc framework for php from http://phpxmlrpc.sourceforge.net/ The latest stable release is version 2.2 released on February 25, 2007
280
windows / linux: download the xml-rpc framework for php from http://phpxmlrpc.sourceforge.net/ The latest stable release is version 2.2 released on February 25, 2007
282
.. i18n: **Setup the XML-RPC for PHP**
284
**Setup the XML-RPC for PHP**
286
.. i18n: extract file xmlrpc-2.2.tar.gz and take the file xmlrpc.inc from lib directory place the xmlrpc.inc in the php library folder restart the apcahe/iis server
288
extract file xmlrpc-2.2.tar.gz and take the file xmlrpc.inc from lib directory place the xmlrpc.inc in the php library folder restart the apcahe/iis server
290
.. i18n: **Demo script**
298
.. i18n: .. code-block:: php
300
.. i18n: function connect() {
301
.. i18n: var $user = 'admin';
302
.. i18n: var $password = 'admin';
303
.. i18n: var $dbname = 'db_name';
304
.. i18n: var $server_url = 'http://localhost:8069/xmlrpc/';
306
.. i18n: if(isset($_COOKIE["user_id"]) == true) {
307
.. i18n: if($_COOKIE["user_id"]>0) {
308
.. i18n: return $_COOKIE["user_id"];
312
.. i18n: $sock = new xmlrpc_client($server_url.'common');
313
.. i18n: $msg = new xmlrpcmsg('login');
314
.. i18n: $msg->addParam(new xmlrpcval($dbname, "string"));
315
.. i18n: $msg->addParam(new xmlrpcval($user, "string"));
316
.. i18n: $msg->addParam(new xmlrpcval($password, "string"));
317
.. i18n: $resp = $sock->send($msg);
318
.. i18n: $val = $resp->value();
319
.. i18n: $id = $val->scalarval();
320
.. i18n: setcookie("user_id",$id,time()+3600);
321
.. i18n: if($id > 0) {
332
var $password = 'admin';
333
var $dbname = 'db_name';
334
var $server_url = 'http://localhost:8069/xmlrpc/';
336
if(isset($_COOKIE["user_id"]) == true) {
337
if($_COOKIE["user_id"]>0) {
338
return $_COOKIE["user_id"];
342
$sock = new xmlrpc_client($server_url.'common');
343
$msg = new xmlrpcmsg('login');
344
$msg->addParam(new xmlrpcval($dbname, "string"));
345
$msg->addParam(new xmlrpcval($user, "string"));
346
$msg->addParam(new xmlrpcval($password, "string"));
347
$resp = $sock->send($msg);
348
$val = $resp->value();
349
$id = $val->scalarval();
350
setcookie("user_id",$id,time()+3600);
358
.. i18n: * **Search**
362
.. i18n: .. code-block:: php
365
.. i18n: * $client = xml-rpc handler
366
.. i18n: * $relation = name of the relation ex: res.partner
367
.. i18n: * $attribute = name of the attribute ex:code
368
.. i18n: * $operator = search term operator ex: ilike, =, !=
369
.. i18n: * $key=search for
372
.. i18n: function search($client,$relation,$attribute,$operator,$keys) {
373
.. i18n: var $user = 'admin';
374
.. i18n: var $password = 'admin';
375
.. i18n: var $userId = -1;
376
.. i18n: var $dbname = 'db_name';
377
.. i18n: var $server_url = 'http://localhost:8069/xmlrpc/';
379
.. i18n: $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
380
.. i18n: new xmlrpcval($operator,"string"),
381
.. i18n: new xmlrpcval($keys,"string")),"array"),
384
.. i18n: if($userId<=0) {
388
.. i18n: $msg = new xmlrpcmsg('execute');
389
.. i18n: $msg->addParam(new xmlrpcval($dbname, "string"));
390
.. i18n: $msg->addParam(new xmlrpcval($userId, "int"));
391
.. i18n: $msg->addParam(new xmlrpcval($password, "string"));
392
.. i18n: $msg->addParam(new xmlrpcval($relation, "string"));
393
.. i18n: $msg->addParam(new xmlrpcval("search", "string"));
394
.. i18n: $msg->addParam(new xmlrpcval($key, "array"));
396
.. i18n: $resp = $client->send($msg);
397
.. i18n: $val = $resp->value();
398
.. i18n: $ids = $val->scalarval();
400
.. i18n: return $ids;
406
* $client = xml-rpc handler
407
* $relation = name of the relation ex: res.partner
408
* $attribute = name of the attribute ex:code
409
* $operator = search term operator ex: ilike, =, !=
413
function search($client,$relation,$attribute,$operator,$keys) {
415
var $password = 'admin';
417
var $dbname = 'db_name';
418
var $server_url = 'http://localhost:8069/xmlrpc/';
420
$key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
421
new xmlrpcval($operator,"string"),
422
new xmlrpcval($keys,"string")),"array"),
429
$msg = new xmlrpcmsg('execute');
430
$msg->addParam(new xmlrpcval($dbname, "string"));
431
$msg->addParam(new xmlrpcval($userId, "int"));
432
$msg->addParam(new xmlrpcval($password, "string"));
433
$msg->addParam(new xmlrpcval($relation, "string"));
434
$msg->addParam(new xmlrpcval("search", "string"));
435
$msg->addParam(new xmlrpcval($key, "array"));
437
$resp = $client->send($msg);
438
$val = $resp->value();
439
$ids = $val->scalarval();
444
.. i18n: * **Create**
470
.. i18n: Access Open-server using xml-rpc
471
.. i18n: """"""""""""""""""""""""""""""""
473
Access Open-server using xml-rpc
474
""""""""""""""""""""""""""""""""
476
.. i18n: **Download the apache XML-RPC framework for JAVA**
478
**Download the apache XML-RPC framework for JAVA**
480
.. i18n: Download the xml-rpc framework for java from http://ws.apache.org/xmlrpc/ The latest stable release is version 3.1 released on August 12, 2007 All TinyERP errors throw exception because the framework allows only an int as the error code where Tinyerp return a string.
482
Download the xml-rpc framework for java from http://ws.apache.org/xmlrpc/ The latest stable release is version 3.1 released on August 12, 2007 All TinyERP errors throw exception because the framework allows only an int as the error code where Tinyerp return a string.
484
.. i18n: **Demo script**
488
.. i18n: * **Find Databases**
492
.. i18n: .. code-block:: java
494
.. i18n: import java.net.URL;
495
.. i18n: import java.util.Vector;
497
.. i18n: import org.apache.commons.lang.StringUtils;
498
.. i18n: import org.apache.xmlrpc.XmlRpcException;
499
.. i18n: import org.apache.xmlrpc.client.XmlRpcClient;
500
.. i18n: import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
502
.. i18n: public Vector<String> getDatabaseList(String host, int port)
504
.. i18n: XmlRpcClient xmlrpcDb = new XmlRpcClient();
506
.. i18n: XmlRpcClientConfigImpl xmlrpcConfigDb = new XmlRpcClientConfigImpl();
507
.. i18n: xmlrpcConfigDb.setEnabledForExtensions(true);
508
.. i18n: xmlrpcConfigDb.setServerURL(new URL("http",host,port,"/xmlrpc/db"));
510
.. i18n: xmlrpcDb.setConfig(xmlrpcConfigDb);
513
.. i18n: //Retrieve databases
514
.. i18n: Vector<Object> params = new Vector<Object>();
515
.. i18n: Object result = xmlrpcDb.execute("list", params);
516
.. i18n: Object[] a = (Object[]) result;
518
.. i18n: Vector<String> res = new Vector<String>();
519
.. i18n: for (int i = 0; i < a.length; i++) {
520
.. i18n: if (a[i] instanceof String)
522
.. i18n: res.addElement((String)a[i]);
525
.. i18n: catch (XmlRpcException e) {
526
.. i18n: logger.warn("XmlException Error while retrieving TinyERP Databases: ",e);
529
.. i18n: catch (Exception e)
531
.. i18n: logger.warn("Error while retrieving TinyERP Databases: ",e);
539
import java.util.Vector;
541
import org.apache.commons.lang.StringUtils;
542
import org.apache.xmlrpc.XmlRpcException;
543
import org.apache.xmlrpc.client.XmlRpcClient;
544
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
546
public Vector<String> getDatabaseList(String host, int port)
548
XmlRpcClient xmlrpcDb = new XmlRpcClient();
550
XmlRpcClientConfigImpl xmlrpcConfigDb = new XmlRpcClientConfigImpl();
551
xmlrpcConfigDb.setEnabledForExtensions(true);
552
xmlrpcConfigDb.setServerURL(new URL("http",host,port,"/xmlrpc/db"));
554
xmlrpcDb.setConfig(xmlrpcConfigDb);
558
Vector<Object> params = new Vector<Object>();
559
Object result = xmlrpcDb.execute("list", params);
560
Object[] a = (Object[]) result;
562
Vector<String> res = new Vector<String>();
563
for (int i = 0; i < a.length; i++) {
564
if (a[i] instanceof String)
566
res.addElement((String)a[i]);
569
catch (XmlRpcException e) {
570
logger.warn("XmlException Error while retrieving TinyERP Databases: ",e);
575
logger.warn("Error while retrieving TinyERP Databases: ",e);
584
.. i18n: .. code-block:: java
586
.. i18n: import java.net.URL;
588
.. i18n: import org.apache.commons.lang.StringUtils;
589
.. i18n: import org.apache.xmlrpc.XmlRpcException;
590
.. i18n: import org.apache.xmlrpc.client.XmlRpcClient;
591
.. i18n: import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
593
.. i18n: public int Connect(String host, int port, String tinydb, String login, String password)
595
.. i18n: XmlRpcClient xmlrpcLogin = new XmlRpcClient();
597
.. i18n: XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
598
.. i18n: xmlrpcConfigLogin.setEnabledForExtensions(true);
599
.. i18n: xmlrpcConfigLogin.setServerURL(new URL("http",host,port,"/xmlrpc/common"));
601
.. i18n: xmlrpcLogin.setConfig(xmlrpcConfigLogin);
605
.. i18n: params = new Object[] {tinydb,login,password};
606
.. i18n: Object id = xmlrpcLogin.execute("login", params);
607
.. i18n: if (id instanceof Integer)
608
.. i18n: return (Integer)id;
611
.. i18n: catch (XmlRpcException e) {
612
.. i18n: logger.warn("XmlException Error while logging to TinyERP: ",e);
615
.. i18n: catch (Exception e)
617
.. i18n: logger.warn("Error while logging to TinyERP: ",e);
626
import org.apache.commons.lang.StringUtils;
627
import org.apache.xmlrpc.XmlRpcException;
628
import org.apache.xmlrpc.client.XmlRpcClient;
629
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
631
public int Connect(String host, int port, String tinydb, String login, String password)
633
XmlRpcClient xmlrpcLogin = new XmlRpcClient();
635
XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
636
xmlrpcConfigLogin.setEnabledForExtensions(true);
637
xmlrpcConfigLogin.setServerURL(new URL("http",host,port,"/xmlrpc/common"));
639
xmlrpcLogin.setConfig(xmlrpcConfigLogin);
643
params = new Object[] {tinydb,login,password};
644
Object id = xmlrpcLogin.execute("login", params);
645
if (id instanceof Integer)
649
catch (XmlRpcException e) {
650
logger.warn("XmlException Error while logging to TinyERP: ",e);
655
logger.warn("Error while logging to TinyERP: ",e);
660
.. i18n: * **Search**
665
.. i18n: * **Create**
690
.. i18n: Python Example
691
.. i18n: --------------
696
.. i18n: Example of creation of a partner and his address.
698
Example of creation of a partner and his address.
700
.. i18n: .. code-block:: python
702
.. i18n: import xmlrpclib
704
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
706
.. i18n: pwd = 'demo'
709
.. i18n: 'title': 'Monsieur',
710
.. i18n: 'name': 'Fabien Pinckaers',
711
.. i18n: 'lang': 'fr',
712
.. i18n: 'active': True,
715
.. i18n: partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
718
.. i18n: 'partner_id': partner_id,
719
.. i18n: 'type': 'default',
720
.. i18n: 'street': 'Rue du vieux chateau, 21',
721
.. i18n: 'zip': '1457',
722
.. i18n: 'city': 'Walhain',
723
.. i18n: 'phone': '(+32)10.68.94.39',
724
.. i18n: 'fax': '(+32)10.68.94.39',
727
.. i18n: sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
729
.. code-block:: python
733
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
739
'name': 'Fabien Pinckaers',
744
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
747
'partner_id': partner_id,
749
'street': 'Rue du vieux chateau, 21',
752
'phone': '(+32)10.68.94.39',
753
'fax': '(+32)10.68.94.39',
756
sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
758
.. i18n: To get the UID of a user, you can use the following script:
760
To get the UID of a user, you can use the following script:
762
.. i18n: .. code-block:: python
764
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
765
.. i18n: UID = sock.login('terp3', 'admin', 'admin')
767
.. code-block:: python
769
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
770
UID = sock.login('terp3', 'admin', 'admin')
772
.. i18n: CRUD example:
776
.. i18n: .. code-block:: python
779
.. i18n: :The login function is under
780
.. i18n: :: http://localhost:8069/xmlrpc/common
781
.. i18n: :For object retrieval use:
782
.. i18n: :: http://localhost:8069/xmlrpc/object
784
.. i18n: import xmlrpclib
786
.. i18n: user = 'admin'
787
.. i18n: pwd = 'admin'
788
.. i18n: dbname = 'terp3'
789
.. i18n: model = 'res.partner'
791
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
792
.. i18n: uid = sock.login(dbname ,user ,pwd)
794
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
796
.. i18n: # CREATE A PARTNER
797
.. i18n: partner_data = {'name':'Tiny', 'active':True, 'vat':'ZZZZZ'}
798
.. i18n: partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
800
.. i18n: # The relation between res.partner and res.partner.category is of type many2many
801
.. i18n: # To add categories to a partner use the following format:
802
.. i18n: partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
803
.. i18n: # Where [3, 2, 1] are id fields of lines in res.partner.category
805
.. i18n: # SEARCH PARTNERS
806
.. i18n: args = [('vat', '=', 'ZZZZZ'),]
807
.. i18n: ids = sock.execute(dbname, uid, pwd, model, 'search', args)
809
.. i18n: # READ PARTNER DATA
810
.. i18n: fields = ['name', 'active', 'vat', 'ref']
811
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
812
.. i18n: print results
814
.. i18n: # EDIT PARTNER DATA
815
.. i18n: values = {'vat':'ZZ1ZZ'}
816
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
818
.. i18n: # DELETE PARTNER DATA
819
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
821
.. code-block:: python
824
:The login function is under
825
:: http://localhost:8069/xmlrpc/common
826
:For object retrieval use:
827
:: http://localhost:8069/xmlrpc/object
834
model = 'res.partner'
836
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
837
uid = sock.login(dbname ,user ,pwd)
839
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
842
partner_data = {'name':'Tiny', 'active':True, 'vat':'ZZZZZ'}
843
partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
845
# The relation between res.partner and res.partner.category is of type many2many
846
# To add categories to a partner use the following format:
847
partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
848
# Where [3, 2, 1] are id fields of lines in res.partner.category
851
args = [('vat', '=', 'ZZZZZ'),]
852
ids = sock.execute(dbname, uid, pwd, model, 'search', args)
855
fields = ['name', 'active', 'vat', 'ref']
856
results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
860
values = {'vat':'ZZ1ZZ'}
861
results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
863
# DELETE PARTNER DATA
864
results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
866
.. i18n: PRINT example:
870
.. i18n: 1. PRINT INVOICE
871
.. i18n: 2. IDS is the invoice ID, as returned by:
872
.. i18n: 3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
875
2. IDS is the invoice ID, as returned by:
876
3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
878
.. i18n: .. code-block:: python
881
.. i18n: import base64
882
.. i18n: printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
883
.. i18n: model = 'account.invoice'
884
.. i18n: id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
885
.. i18n: time.sleep(5)
886
.. i18n: state = False
888
.. i18n: while not state:
889
.. i18n: report = printsock.report_get(dbname, uid, pwd, id_report)
890
.. i18n: state = report['state']
891
.. i18n: if not state:
892
.. i18n: time.sleep(1)
893
.. i18n: attempt += 1
894
.. i18n: if attempt>200:
895
.. i18n: print 'Printing aborted, too long delay !'
897
.. i18n: string_pdf = base64.decodestring(report['result'])
898
.. i18n: file_pdf = open('/tmp/file.pdf','w')
899
.. i18n: file_pdf.write(string_pdf)
900
.. i18n: file_pdf.close()
902
.. code-block:: python
906
printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
907
model = 'account.invoice'
908
id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
913
report = printsock.report_get(dbname, uid, pwd, id_report)
914
state = report['state']
919
print 'Printing aborted, too long delay !'
921
string_pdf = base64.decodestring(report['result'])
922
file_pdf = open('/tmp/file.pdf','w')
923
file_pdf.write(string_pdf)
932
.. i18n: Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
934
Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
936
.. i18n: .. code-block:: php
940
.. i18n: include('xmlrpc.inc');
942
.. i18n: $arrayVal = array(
943
.. i18n: 'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
944
.. i18n: 'vat'=>new xmlrpcval('BE477472701' , "string")
947
.. i18n: $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
949
.. i18n: $msg = new xmlrpcmsg('execute');
950
.. i18n: $msg->addParam(new xmlrpcval("dbname", "string"));
951
.. i18n: $msg->addParam(new xmlrpcval("3", "int"));
952
.. i18n: $msg->addParam(new xmlrpcval("demo", "string"));
953
.. i18n: $msg->addParam(new xmlrpcval("res.partner", "string"));
954
.. i18n: $msg->addParam(new xmlrpcval("create", "string"));
955
.. i18n: $msg->addParam(new xmlrpcval($arrayVal, "struct"));
957
.. i18n: $resp = $client->send($msg);
959
.. i18n: if ($resp->faultCode())
961
.. i18n: echo 'Error: '.$resp->faultString();
965
.. i18n: echo 'Partner '.$resp->value()->scalarval().' created !';
973
include('xmlrpc.inc');
976
'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
977
'vat'=>new xmlrpcval('BE477472701' , "string")
980
$client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
982
$msg = new xmlrpcmsg('execute');
983
$msg->addParam(new xmlrpcval("dbname", "string"));
984
$msg->addParam(new xmlrpcval("3", "int"));
985
$msg->addParam(new xmlrpcval("demo", "string"));
986
$msg->addParam(new xmlrpcval("res.partner", "string"));
987
$msg->addParam(new xmlrpcval("create", "string"));
988
$msg->addParam(new xmlrpcval($arrayVal, "struct"));
990
$resp = $client->send($msg);
992
if ($resp->faultCode())
994
echo 'Error: '.$resp->faultString();
998
echo 'Partner '.$resp->value()->scalarval().' created !';