2
.. i18n: XML-RPC Web services
3
.. i18n: ====================
9
.. 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.
12
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.
14
.. 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.
17
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.
19
.. 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.
22
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.
24
.. 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/).
27
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/).
29
.. 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:
32
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:
48
.. i18n: XML-RPC Architecture
49
.. i18n: """"""""""""""""""""
55
.. i18n: OpenERP 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.
58
OpenERP 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.
67
.. i18n: The diagram below synthesizes the client server architecture of OpenERP. OpenERP server and OpenERP clients communicate using XML-RPC.
70
The diagram below synthesizes the client server architecture of OpenERP. OpenERP server and OpenERP clients communicate using XML-RPC.
72
.. i18n: .. figure:: images/tech_arch.png
74
.. i18n: :align: center
77
.. figure:: images/tech_arch.png
86
.. i18n: The logic of OpenERP 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 OpenERP easier to maintain.
89
The logic of OpenERP 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 OpenERP easier to maintain.
91
.. 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.
94
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.
96
.. 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.
99
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.
101
.. i18n: There are three types of action;
104
There are three types of action;
106
.. i18n: * Open a window (form or tree)
107
.. i18n: * Print a document
108
.. i18n: * Execute a wizard
111
* Open a window (form or tree)
122
.. i18n: Access tiny-server using xml-rpc
123
.. i18n: """"""""""""""""""""""""""""""""
126
Access tiny-server using xml-rpc
127
""""""""""""""""""""""""""""""""
136
.. i18n: * **Create a partner and his address**
139
* **Create a partner and his address**
143
.. i18n: import xmlrpclib
145
.. i18n: username = 'admin' #the user
146
.. i18n: pwd = 'admin' #the password of the user
147
.. i18n: dbname = 'terp' #the database
149
.. i18n: # Get the uid
150
.. i18n: sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
151
.. i18n: uid = sock_common.login(dbname, username, pwd)
153
.. i18n: #replace localhost with the address of the server
154
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
157
.. i18n: 'name': 'Fabien Pinckaers',
158
.. i18n: 'lang': 'fr_FR',
161
.. i18n: partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
164
.. i18n: 'partner_id': partner_id,
165
.. i18n: 'type' : 'default',
166
.. i18n: 'street': 'Chaussée de Namur 40',
167
.. i18n: 'zip': '1367',
168
.. i18n: 'city': 'Grand-Rosière',
169
.. i18n: 'phone': '+3281813700',
170
.. i18n: 'fax': '+3281733501',
173
.. i18n: address_id = sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
180
username = 'admin' #the user
181
pwd = 'admin' #the password of the user
182
dbname = 'terp' #the database
185
sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
186
uid = sock_common.login(dbname, username, pwd)
188
#replace localhost with the address of the server
189
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
192
'name': 'Fabien Pinckaers',
196
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
199
'partner_id': partner_id,
201
'street': 'Chaussée de Namur 40',
203
'city': 'Grand-Rosière',
204
'phone': '+3281813700',
205
'fax': '+3281733501',
208
address_id = sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
210
.. i18n: * **Search a partner**
213
.. i18n: args = [('vat', '=', 'ZZZZZZ')] #query clause
214
.. i18n: ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', args)
216
.. i18n: * **Read partner data**
219
.. i18n: fields = ['name', 'active', 'vat', 'ref'] #fields to read
220
.. i18n: data = sock.execute(dbname, uid, pwd, 'res.partner', 'read', ids, fields) #ids is a list of id
222
.. i18n: * **Update partner data**
225
.. i18n: values = {'vat': 'ZZ1ZZZ'} #data to update
226
.. i18n: result = sock.execute(dbname, uid, pwd, 'res.partner', 'write', ids, values)
228
.. i18n: * **Delete partner**
231
.. i18n: # ids : list of id
232
.. i18n: result = sock.execute(dbname, uid, pwd, 'res.partner', 'unlink', ids)
235
* **Search a partner**
238
args = [('vat', '=', 'ZZZZZZ')] #query clause
239
ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', args)
241
* **Read partner data**
244
fields = ['name', 'active', 'vat', 'ref'] #fields to read
245
data = sock.execute(dbname, uid, pwd, 'res.partner', 'read', ids, fields) #ids is a list of id
247
* **Update partner data**
250
values = {'vat': 'ZZ1ZZZ'} #data to update
251
result = sock.execute(dbname, uid, pwd, 'res.partner', 'write', ids, values)
257
result = sock.execute(dbname, uid, pwd, 'res.partner', 'unlink', ids)
266
.. i18n: Access Open-server using xml-rpc
267
.. i18n: """"""""""""""""""""""""""""""""
270
Access Open-server using xml-rpc
271
""""""""""""""""""""""""""""""""
273
.. 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
281
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
283
.. i18n: **Setup the XML-RPC for PHP**
286
**Setup the XML-RPC for PHP**
288
.. 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
291
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
293
.. i18n: **Demo script**
303
.. i18n: .. code-block:: php
305
.. i18n: function connect() {
306
.. i18n: var $user = 'admin';
307
.. i18n: var $password = 'admin';
308
.. i18n: var $dbname = 'db_name';
309
.. i18n: var $server_url = 'http://localhost:8069/xmlrpc/';
311
.. i18n: if(isset($_COOKIE["user_id"]) == true) {
312
.. i18n: if($_COOKIE["user_id"]>0) {
313
.. i18n: return $_COOKIE["user_id"];
317
.. i18n: $sock = new xmlrpc_client($server_url.'common');
318
.. i18n: $msg = new xmlrpcmsg('login');
319
.. i18n: $msg->addParam(new xmlrpcval($dbname, "string"));
320
.. i18n: $msg->addParam(new xmlrpcval($user, "string"));
321
.. i18n: $msg->addParam(new xmlrpcval($password, "string"));
322
.. i18n: $resp = $sock->send($msg);
323
.. i18n: $val = $resp->value();
324
.. i18n: $id = $val->scalarval();
325
.. i18n: setcookie("user_id",$id,time()+3600);
326
.. i18n: if($id > 0) {
338
var $password = 'admin';
339
var $dbname = 'db_name';
340
var $server_url = 'http://localhost:8069/xmlrpc/';
342
if(isset($_COOKIE["user_id"]) == true) {
343
if($_COOKIE["user_id"]>0) {
344
return $_COOKIE["user_id"];
348
$sock = new xmlrpc_client($server_url.'common');
349
$msg = new xmlrpcmsg('login');
350
$msg->addParam(new xmlrpcval($dbname, "string"));
351
$msg->addParam(new xmlrpcval($user, "string"));
352
$msg->addParam(new xmlrpcval($password, "string"));
353
$resp = $sock->send($msg);
354
$val = $resp->value();
355
$id = $val->scalarval();
356
setcookie("user_id",$id,time()+3600);
364
.. i18n: * **Search**
369
.. i18n: .. code-block:: php
372
.. i18n: * $client = xml-rpc handler
373
.. i18n: * $relation = name of the relation ex: res.partner
374
.. i18n: * $attribute = name of the attribute ex:code
375
.. i18n: * $operator = search term operator ex: ilike, =, !=
376
.. i18n: * $key=search for
379
.. i18n: function search($client,$relation,$attribute,$operator,$keys) {
380
.. i18n: var $user = 'admin';
381
.. i18n: var $password = 'admin';
382
.. i18n: var $userId = -1;
383
.. i18n: var $dbname = 'db_name';
384
.. i18n: var $server_url = 'http://localhost:8069/xmlrpc/';
386
.. i18n: $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
387
.. i18n: new xmlrpcval($operator,"string"),
388
.. i18n: new xmlrpcval($keys,"string")),"array"),
391
.. i18n: if($userId<=0) {
395
.. i18n: $msg = new xmlrpcmsg('execute');
396
.. i18n: $msg->addParam(new xmlrpcval($dbname, "string"));
397
.. i18n: $msg->addParam(new xmlrpcval($userId, "int"));
398
.. i18n: $msg->addParam(new xmlrpcval($password, "string"));
399
.. i18n: $msg->addParam(new xmlrpcval($relation, "string"));
400
.. i18n: $msg->addParam(new xmlrpcval("search", "string"));
401
.. i18n: $msg->addParam(new xmlrpcval($key, "array"));
403
.. i18n: $resp = $client->send($msg);
404
.. i18n: $val = $resp->value();
405
.. i18n: $ids = $val->scalarval();
407
.. i18n: return $ids;
414
* $client = xml-rpc handler
415
* $relation = name of the relation ex: res.partner
416
* $attribute = name of the attribute ex:code
417
* $operator = search term operator ex: ilike, =, !=
421
function search($client,$relation,$attribute,$operator,$keys) {
423
var $password = 'admin';
425
var $dbname = 'db_name';
426
var $server_url = 'http://localhost:8069/xmlrpc/';
428
$key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
429
new xmlrpcval($operator,"string"),
430
new xmlrpcval($keys,"string")),"array"),
437
$msg = new xmlrpcmsg('execute');
438
$msg->addParam(new xmlrpcval($dbname, "string"));
439
$msg->addParam(new xmlrpcval($userId, "int"));
440
$msg->addParam(new xmlrpcval($password, "string"));
441
$msg->addParam(new xmlrpcval($relation, "string"));
442
$msg->addParam(new xmlrpcval("search", "string"));
443
$msg->addParam(new xmlrpcval($key, "array"));
445
$resp = $client->send($msg);
446
$val = $resp->value();
447
$ids = $val->scalarval();
452
.. i18n: * **Create**
457
.. i18n: .. code-block:: php
461
.. i18n: include('xmlrpc.inc');
463
.. i18n: $arrayVal = array(
464
.. i18n: 'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
465
.. i18n: 'vat'=>new xmlrpcval('BE477472701' , "string")
468
.. i18n: $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
470
.. i18n: $msg = new xmlrpcmsg('execute');
471
.. i18n: $msg->addParam(new xmlrpcval("dbname", "string"));
472
.. i18n: $msg->addParam(new xmlrpcval("3", "int"));
473
.. i18n: $msg->addParam(new xmlrpcval("demo", "string"));
474
.. i18n: $msg->addParam(new xmlrpcval("res.partner", "string"));
475
.. i18n: $msg->addParam(new xmlrpcval("create", "string"));
476
.. i18n: $msg->addParam(new xmlrpcval($arrayVal, "struct"));
478
.. i18n: $resp = $client->send($msg);
480
.. i18n: if ($resp->faultCode())
482
.. i18n: echo 'Error: '.$resp->faultString();
486
.. i18n: echo 'Partner '.$resp->value()->scalarval().' created !';
495
include('xmlrpc.inc');
498
'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
499
'vat'=>new xmlrpcval('BE477472701' , "string")
502
$client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
504
$msg = new xmlrpcmsg('execute');
505
$msg->addParam(new xmlrpcval("dbname", "string"));
506
$msg->addParam(new xmlrpcval("3", "int"));
507
$msg->addParam(new xmlrpcval("demo", "string"));
508
$msg->addParam(new xmlrpcval("res.partner", "string"));
509
$msg->addParam(new xmlrpcval("create", "string"));
510
$msg->addParam(new xmlrpcval($arrayVal, "struct"));
512
$resp = $client->send($msg);
514
if ($resp->faultCode())
516
echo 'Error: '.$resp->faultString();
520
echo 'Partner '.$resp->value()->scalarval().' created !';
529
.. i18n: .. code-block:: php
532
.. i18n: * $client = xml-rpc handler
533
.. i18n: * $relation = name of the relation ex: res.partner
534
.. i18n: * $attribute = name of the attribute ex:code
535
.. i18n: * $operator = search term operator ex: ilike, =, !=
536
.. i18n: * $id = id of the record to be updated
537
.. i18n: * $data = data to be updated
540
.. i18n: function write($client,$relation,$attribute,$operator,$data,$id) {
541
.. i18n: var $user = 'admin';
542
.. i18n: var $password = 'admin';
543
.. i18n: var $userId = -1;
544
.. i18n: var $dbname = 'db_name';
545
.. i18n: var $server_url = 'http://localhost:8069/xmlrpc/';
547
.. i18n: $id_val = array();
548
.. i18n: $id_val[0] = new xmlrpcval($id, "int");
550
.. i18n: if($userId<=0) {
554
.. i18n: $msg = new xmlrpcmsg('execute');
555
.. i18n: $msg->addParam(new xmlrpcval($dbname, "string"));
556
.. i18n: $msg->addParam(new xmlrpcval($userId, "int"));
557
.. i18n: $msg->addParam(new xmlrpcval($password, "string"));
558
.. i18n: $msg->addParam(new xmlrpcval($relation, "string"));
559
.. i18n: $msg->addParam(new xmlrpcval("write", "string"));
560
.. i18n: $msg->addParam(new xmlrpcval($id, "array"));
561
.. i18n: $msg->addParam(new xmlrpcval($data, "struct"));
563
.. i18n: $resp = $client->send($msg);
564
.. i18n: $val = $resp->value();
565
.. i18n: $record = $val->scalarval();
567
.. i18n: return $record;
575
* $client = xml-rpc handler
576
* $relation = name of the relation ex: res.partner
577
* $attribute = name of the attribute ex:code
578
* $operator = search term operator ex: ilike, =, !=
579
* $id = id of the record to be updated
580
* $data = data to be updated
583
function write($client,$relation,$attribute,$operator,$data,$id) {
585
var $password = 'admin';
587
var $dbname = 'db_name';
588
var $server_url = 'http://localhost:8069/xmlrpc/';
591
$id_val[0] = new xmlrpcval($id, "int");
597
$msg = new xmlrpcmsg('execute');
598
$msg->addParam(new xmlrpcval($dbname, "string"));
599
$msg->addParam(new xmlrpcval($userId, "int"));
600
$msg->addParam(new xmlrpcval($password, "string"));
601
$msg->addParam(new xmlrpcval($relation, "string"));
602
$msg->addParam(new xmlrpcval("write", "string"));
603
$msg->addParam(new xmlrpcval($id, "array"));
604
$msg->addParam(new xmlrpcval($data, "struct"));
606
$resp = $client->send($msg);
607
$val = $resp->value();
608
$record = $val->scalarval();
621
.. i18n: Access Open-server using xml-rpc
622
.. i18n: """"""""""""""""""""""""""""""""
625
Access Open-server using xml-rpc
626
""""""""""""""""""""""""""""""""
628
.. i18n: **Download the apache XML-RPC framework for JAVA**
631
**Download the apache XML-RPC framework for JAVA**
633
.. 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.
634
.. i18n: All OpenERP errors throw exceptions because the framework allows only an int as the error code where OpenERP returns a string.
637
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.
638
All OpenERP errors throw exceptions because the framework allows only an int as the error code where OpenERP returns a string.
640
.. i18n: **Demo script**
645
.. i18n: * **Find Databases**
650
.. i18n: .. code-block:: java
652
.. i18n: import java.net.URL;
653
.. i18n: import java.util.Vector;
655
.. i18n: import org.apache.commons.lang.StringUtils;
656
.. i18n: import org.apache.xmlrpc.XmlRpcException;
657
.. i18n: import org.apache.xmlrpc.client.XmlRpcClient;
658
.. i18n: import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
660
.. i18n: public Vector<String> getDatabaseList(String host, int port)
662
.. i18n: XmlRpcClient xmlrpcDb = new XmlRpcClient();
664
.. i18n: XmlRpcClientConfigImpl xmlrpcConfigDb = new XmlRpcClientConfigImpl();
665
.. i18n: xmlrpcConfigDb.setEnabledForExtensions(true);
666
.. i18n: xmlrpcConfigDb.setServerURL(new URL("http",host,port,"/xmlrpc/db"));
668
.. i18n: xmlrpcDb.setConfig(xmlrpcConfigDb);
671
.. i18n: //Retrieve databases
672
.. i18n: Vector<Object> params = new Vector<Object>();
673
.. i18n: Object result = xmlrpcDb.execute("list", params);
674
.. i18n: Object[] a = (Object[]) result;
676
.. i18n: Vector<String> res = new Vector<String>();
677
.. i18n: for (int i = 0; i < a.length; i++) {
678
.. i18n: if (a[i] instanceof String)
680
.. i18n: res.addElement((String)a[i]);
683
.. i18n: catch (XmlRpcException e) {
684
.. i18n: logger.warn("XmlException Error while retrieving OpenERP Databases: ",e);
687
.. i18n: catch (Exception e)
689
.. i18n: logger.warn("Error while retrieving OpenERP Databases: ",e);
698
import java.util.Vector;
700
import org.apache.commons.lang.StringUtils;
701
import org.apache.xmlrpc.XmlRpcException;
702
import org.apache.xmlrpc.client.XmlRpcClient;
703
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
705
public Vector<String> getDatabaseList(String host, int port)
707
XmlRpcClient xmlrpcDb = new XmlRpcClient();
709
XmlRpcClientConfigImpl xmlrpcConfigDb = new XmlRpcClientConfigImpl();
710
xmlrpcConfigDb.setEnabledForExtensions(true);
711
xmlrpcConfigDb.setServerURL(new URL("http",host,port,"/xmlrpc/db"));
713
xmlrpcDb.setConfig(xmlrpcConfigDb);
717
Vector<Object> params = new Vector<Object>();
718
Object result = xmlrpcDb.execute("list", params);
719
Object[] a = (Object[]) result;
721
Vector<String> res = new Vector<String>();
722
for (int i = 0; i < a.length; i++) {
723
if (a[i] instanceof String)
725
res.addElement((String)a[i]);
728
catch (XmlRpcException e) {
729
logger.warn("XmlException Error while retrieving OpenERP Databases: ",e);
734
logger.warn("Error while retrieving OpenERP Databases: ",e);
744
.. i18n: .. code-block:: java
746
.. i18n: import java.net.URL;
748
.. i18n: import org.apache.commons.lang.StringUtils;
749
.. i18n: import org.apache.xmlrpc.XmlRpcException;
750
.. i18n: import org.apache.xmlrpc.client.XmlRpcClient;
751
.. i18n: import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
753
.. i18n: public int Connect(String host, int port, String tinydb, String login, String password)
755
.. i18n: XmlRpcClient xmlrpcLogin = new XmlRpcClient();
757
.. i18n: XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
758
.. i18n: xmlrpcConfigLogin.setEnabledForExtensions(true);
759
.. i18n: xmlrpcConfigLogin.setServerURL(new URL("http",host,port,"/xmlrpc/common"));
761
.. i18n: xmlrpcLogin.setConfig(xmlrpcConfigLogin);
765
.. i18n: params = new Object[] {tinydb,login,password};
766
.. i18n: Object id = xmlrpcLogin.execute("login", params);
767
.. i18n: if (id instanceof Integer)
768
.. i18n: return (Integer)id;
771
.. i18n: catch (XmlRpcException e) {
772
.. i18n: logger.warn("XmlException Error while logging to OpenERP: ",e);
775
.. i18n: catch (Exception e)
777
.. i18n: logger.warn("Error while logging to OpenERP: ",e);
787
import org.apache.commons.lang.StringUtils;
788
import org.apache.xmlrpc.XmlRpcException;
789
import org.apache.xmlrpc.client.XmlRpcClient;
790
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
792
public int Connect(String host, int port, String tinydb, String login, String password)
794
XmlRpcClient xmlrpcLogin = new XmlRpcClient();
796
XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
797
xmlrpcConfigLogin.setEnabledForExtensions(true);
798
xmlrpcConfigLogin.setServerURL(new URL("http",host,port,"/xmlrpc/common"));
800
xmlrpcLogin.setConfig(xmlrpcConfigLogin);
804
params = new Object[] {tinydb,login,password};
805
Object id = xmlrpcLogin.execute("login", params);
806
if (id instanceof Integer)
810
catch (XmlRpcException e) {
811
logger.warn("XmlException Error while logging to OpenERP: ",e);
816
logger.warn("Error while logging to OpenERP: ",e);
821
.. i18n: * **Search**
826
.. i18n: * **Create**
852
.. i18n: Python Example
853
.. i18n: --------------
859
.. i18n: Example of creation of a partner and his address.
862
Example of creation of a partner and his address.
864
.. i18n: .. code-block:: python
866
.. i18n: import xmlrpclib
868
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
870
.. i18n: pwd = 'demo'
873
.. i18n: 'title': 'Monsieur',
874
.. i18n: 'name': 'Fabien Pinckaers',
875
.. i18n: 'lang': 'fr',
876
.. i18n: 'active': True,
879
.. i18n: partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
882
.. i18n: 'partner_id': partner_id,
883
.. i18n: 'type': 'default',
884
.. i18n: 'street': 'Rue du vieux chateau, 21',
885
.. i18n: 'zip': '1457',
886
.. i18n: 'city': 'Walhain',
887
.. i18n: 'phone': '(+32)10.68.94.39',
888
.. i18n: 'fax': '(+32)10.68.94.39',
891
.. i18n: sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
894
.. code-block:: python
898
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
904
'name': 'Fabien Pinckaers',
909
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
912
'partner_id': partner_id,
914
'street': 'Rue du vieux chateau, 21',
917
'phone': '(+32)10.68.94.39',
918
'fax': '(+32)10.68.94.39',
921
sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
923
.. i18n: To get the UID of a user, you can use the following script:
926
To get the UID of a user, you can use the following script:
928
.. i18n: .. code-block:: python
930
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
931
.. i18n: UID = sock.login('terp3', 'admin', 'admin')
934
.. code-block:: python
936
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
937
UID = sock.login('terp3', 'admin', 'admin')
939
.. i18n: CRUD example:
944
.. i18n: .. code-block:: python
947
.. i18n: :The login function is under
948
.. i18n: :: http://localhost:8069/xmlrpc/common
949
.. i18n: :For object retrieval use:
950
.. i18n: :: http://localhost:8069/xmlrpc/object
952
.. i18n: import xmlrpclib
954
.. i18n: user = 'admin'
955
.. i18n: pwd = 'admin'
956
.. i18n: dbname = 'terp3'
957
.. i18n: model = 'res.partner'
959
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
960
.. i18n: uid = sock.login(dbname ,user ,pwd)
962
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
964
.. i18n: # CREATE A PARTNER
965
.. i18n: partner_data = {'name'.. code-block:: php:'Tiny', 'active':True, 'vat':'ZZZZZ'}
966
.. i18n: partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
968
.. i18n: # The relation between res.partner and res.partner.category is of type many2many
969
.. i18n: # To add categories to a partner use the following format:
970
.. i18n: partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
971
.. i18n: # Where [3, 2, 1] are id fields of lines in res.partner.category
973
.. i18n: # SEARCH PARTNERS
974
.. i18n: args = [('vat', '=', 'ZZZZZ'),]
975
.. i18n: ids = sock.execute(dbname, uid, pwd, model, 'search', args)
977
.. i18n: # READ PARTNER DATA
978
.. i18n: fields = ['name', 'active', 'vat', 'ref']
979
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
980
.. i18n: print results
982
.. i18n: # EDIT PARTNER DATA
983
.. i18n: values = {'vat':'ZZ1ZZ'}
984
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
986
.. i18n: # DELETE PARTNER DATA
987
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
990
.. code-block:: python
993
:The login function is under
994
:: http://localhost:8069/xmlrpc/common
995
:For object retrieval use:
996
:: http://localhost:8069/xmlrpc/object
1003
model = 'res.partner'
1005
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
1006
uid = sock.login(dbname ,user ,pwd)
1008
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
1011
partner_data = {'name'.. code-block:: php:'Tiny', 'active':True, 'vat':'ZZZZZ'}
1012
partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
1014
# The relation between res.partner and res.partner.category is of type many2many
1015
# To add categories to a partner use the following format:
1016
partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
1017
# Where [3, 2, 1] are id fields of lines in res.partner.category
1020
args = [('vat', '=', 'ZZZZZ'),]
1021
ids = sock.execute(dbname, uid, pwd, model, 'search', args)
1024
fields = ['name', 'active', 'vat', 'ref']
1025
results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
1029
values = {'vat':'ZZ1ZZ'}
1030
results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
1032
# DELETE PARTNER DATA
1033
results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
1035
.. i18n: PRINT example:
1040
.. i18n: 1. PRINT INVOICE
1041
.. i18n: 2. IDS is the invoice ID, as returned by:
1042
.. i18n: 3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
1046
2. IDS is the invoice ID, as returned by:
1047
3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
1049
.. i18n: .. code-block:: python
1051
.. i18n: import time
1052
.. i18n: import base64
1053
.. i18n: printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
1054
.. i18n: model = 'account.invoice'
1055
.. i18n: id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
1056
.. i18n: time.sleep(5)
1057
.. i18n: state = False
1058
.. i18n: attempt = 0
1059
.. i18n: while not state:
1060
.. i18n: report = printsock.report_get(dbname, uid, pwd, id_report)
1061
.. i18n: state = report['state']
1062
.. i18n: if not state:
1063
.. i18n: time.sleep(1)
1064
.. i18n: attempt += 1
1065
.. i18n: if attempt>200:
1066
.. i18n: print 'Printing aborted, too long delay !'
1068
.. i18n: string_pdf = base64.decodestring(report['result'])
1069
.. i18n: file_pdf = open('/tmp/file.pdf','w')
1070
.. i18n: file_pdf.write(string_pdf)
1071
.. i18n: file_pdf.close()
1074
.. code-block:: python
1078
printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
1079
model = 'account.invoice'
1080
id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
1085
report = printsock.report_get(dbname, uid, pwd, id_report)
1086
state = report['state']
1091
print 'Printing aborted, too long delay !'
1093
string_pdf = base64.decodestring(report['result'])
1094
file_pdf = open('/tmp/file.pdf','w')
1095
file_pdf.write(string_pdf)
1098
.. i18n: PHP Example
1099
.. i18n: -----------
1105
.. i18n: Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
1108
Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
1110
.. i18n: .. code-block:: php
1114
.. i18n: include('xmlrpc.inc');
1116
.. i18n: $arrayVal = array(
1117
.. i18n: 'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
1118
.. i18n: 'vat'=>new xmlrpcval('BE477472701' , "string")
1121
.. i18n: $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
1123
.. i18n: $msg = new xmlrpcmsg('execute');
1124
.. i18n: $msg->addParam(new xmlrpcval("dbname", "string"));
1125
.. i18n: $msg->addParam(new xmlrpcval("3", "int"));
1126
.. i18n: $msg->addParam(new xmlrpcval("demo", "string"));
1127
.. i18n: $msg->addParam(new xmlrpcval("res.partner", "string"));
1128
.. i18n: $msg->addParam(new xmlrpcval("create", "string"));
1129
.. i18n: $msg->addParam(new xmlrpcval($arrayVal, "struct"));
1131
.. i18n: $resp = $client->send($msg);
1133
.. i18n: if ($resp->faultCode())
1135
.. i18n: echo 'Error: '.$resp->faultString();
1139
.. i18n: echo 'Partner '.$resp->value()->scalarval().' created !';
1148
include('xmlrpc.inc');
1151
'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
1152
'vat'=>new xmlrpcval('BE477472701' , "string")
1155
$client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
1157
$msg = new xmlrpcmsg('execute');
1158
$msg->addParam(new xmlrpcval("dbname", "string"));
1159
$msg->addParam(new xmlrpcval("3", "int"));
1160
$msg->addParam(new xmlrpcval("demo", "string"));
1161
$msg->addParam(new xmlrpcval("res.partner", "string"));
1162
$msg->addParam(new xmlrpcval("create", "string"));
1163
$msg->addParam(new xmlrpcval($arrayVal, "struct"));
1165
$resp = $client->send($msg);
1167
if ($resp->faultCode())
1169
echo 'Error: '.$resp->faultString();
1173
echo 'Partner '.$resp->value()->scalarval().' created !';
1177
.. i18n: Perl Example
1178
.. i18n: ------------
1179
.. i18n: Here is an example in Perl for creating,searching and deleting a partner.
1184
Here is an example in Perl for creating,searching and deleting a partner.
1186
.. i18n: .. code-block:: perl
1188
.. i18n: #!c:/perl/bin/perl
1189
.. i18n: # 17-02-2010
1190
.. i18n: # OpenERP XML RPC communication example
1191
.. i18n: # Todor Todorov <todorov@hp.com> <tttodorov@yahoo.com>
1193
.. i18n: use strict;
1194
.. i18n: use Frontier::Client;
1195
.. i18n: use Data::Dumper;
1197
.. i18n: my($user) = 'admin';
1198
.. i18n: my($pw) = 'admin';
1199
.. i18n: my($db) = 'put_your_dbname_here';
1200
.. i18n: my($model) = 'res.partner';
1203
.. i18n: my $server_url = 'http://localhost:8069/xmlrpc/common';
1204
.. i18n: my $server = Frontier::Client->new('url' => $server_url);
1205
.. i18n: my $uid = $server->call('login',$db,$user,$pw);
1207
.. i18n: print Dumper($uid);
1209
.. i18n: my $server_url = 'http://localhost:8069/xmlrpc/object';
1210
.. i18n: my $server = Frontier::Client->new('url' => $server_url);
1212
.. i18n: print Dumper($server);
1215
.. i18n: # CREATE A PARTNER
1217
.. i18n: my $partner_data = {'name'=>'MyNewPartnerName',
1218
.. i18n: 'active'=> 'True',
1219
.. i18n: 'vat'=>'ZZZZZ'};
1220
.. i18n: my $partner_id = $server->call('execute',$db, $uid, $pw, $model, 'create', $partner_data);
1222
.. i18n: print Dumper($partner_id);
1225
.. i18n: # SEARCH PARTNERS
1227
.. i18n: my $query = [['vat', '=', 'ZZZZZ']];
1229
.. i18n: print Dumper($query);
1231
.. i18n: my $ids = $server->call('execute',$db, $uid, $pw, $model, 'search', $query);
1233
.. i18n: print Dumper($ids);
1235
.. i18n: #Here waiting for user input
1236
.. i18n: #OpenERP interface my be checked if partner is shown there
1238
.. i18n: print $/."Check OpenERP if partner is inserted. Press ENTER".$/;
1242
.. i18n: # DELETE PARTNER DATA
1244
.. i18n: my $results = $server->call('execute',$db, $uid, $pw, $model, 'unlink', $ids);
1246
.. i18n: print Dumper($results);
1249
.. code-block:: perl
1253
# OpenERP XML RPC communication example
1254
# Todor Todorov <todorov@hp.com> <tttodorov@yahoo.com>
1257
use Frontier::Client;
1260
my($user) = 'admin';
1262
my($db) = 'put_your_dbname_here';
1263
my($model) = 'res.partner';
1266
my $server_url = 'http://localhost:8069/xmlrpc/common';
1267
my $server = Frontier::Client->new('url' => $server_url);
1268
my $uid = $server->call('login',$db,$user,$pw);
1272
my $server_url = 'http://localhost:8069/xmlrpc/object';
1273
my $server = Frontier::Client->new('url' => $server_url);
1275
print Dumper($server);
1280
my $partner_data = {'name'=>'MyNewPartnerName',
1283
my $partner_id = $server->call('execute',$db, $uid, $pw, $model, 'create', $partner_data);
1285
print Dumper($partner_id);
1290
my $query = [['vat', '=', 'ZZZZZ']];
1292
print Dumper($query);
1294
my $ids = $server->call('execute',$db, $uid, $pw, $model, 'search', $query);
1298
#Here waiting for user input
1299
#OpenERP interface my be checked if partner is shown there
1301
print $/."Check OpenERP if partner is inserted. Press ENTER".$/;
1305
# DELETE PARTNER DATA
1307
my $results = $server->call('execute',$db, $uid, $pw, $model, 'unlink', $ids);
1309
print Dumper($results);
1311
.. i18n: Everything done in the GTK or web client in OpenERP is through XML/RPC webservices. Start openERP GTK client
1312
.. i18n: using ./openerp-client.py -l debug_rpc (or debug_rpc_answer) then do what you want in the GTK client and watch
1313
.. i18n: your client logs, you will find out the webservice signatures. By creating indents in the logs will help you to
1314
.. i18n: spot which webservice you want.
1317
Everything done in the GTK or web client in OpenERP is through XML/RPC webservices. Start openERP GTK client
1318
using ./openerp-client.py -l debug_rpc (or debug_rpc_answer) then do what you want in the GTK client and watch
1319
your client logs, you will find out the webservice signatures. By creating indents in the logs will help you to
1320
spot which webservice you want.