~xrg/openobject-doc/trunk-xrg

« back to all changes in this revision

Viewing changes to i18n/vi/source/developer/6_22_XML-RPC_web_services/index.rst

  • Committer: TruongSinh Tran
  • Date: 2009-07-17 18:59:45 UTC
  • Revision ID: truongsinh@vipescoserver-20090717185945-ajjp3zso6xh5jddm
[FIX]private issue

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
.. i18n: XML-RPC Web services
 
3
.. i18n: ====================
 
4
 
 
5
XML-RPC Web services
 
6
====================
 
7
 
 
8
.. i18n: Jump to: navigation, search
 
9
 
 
10
Jump to: navigation, search
 
11
 
 
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
 
19
.. i18n:                 o create
 
20
.. i18n:                 o unlink (=delete)
 
21
 
 
22
   1. **XML-RPC**
 
23
          * standard: http://www.xmlrpc.org
 
24
          * RPC Over HTTP
 
25
          * Function Parameters & Result encoded in XML
 
26
   2. **Principle**;
 
27
          * calls to objects methodes;
 
28
                o read, write
 
29
                o create
 
30
                o unlink (=delete)
 
31
 
 
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.
 
33
 
 
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.
 
35
 
 
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.
 
37
 
 
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.
 
39
 
 
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.
 
41
 
 
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.
 
43
 
 
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/).
 
45
 
 
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/).
 
47
 
 
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:
 
49
 
 
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:
 
51
 
 
52
.. i18n: XML-RPC Home Page\\ XML-RPC for C and C++\\ The Apache XML-RPC Project\\ Expat: The XML Parser\\ 
 
53
 
 
54
XML-RPC Home Page\\ XML-RPC for C and C++\\ The Apache XML-RPC Project\\ Expat: The XML Parser\\ 
 
55
 
 
56
.. i18n: Interfaces
 
57
.. i18n: ----------
 
58
 
 
59
Interfaces
 
60
----------
 
61
 
 
62
.. i18n: XML-RPC
 
63
.. i18n: +++++++
 
64
 
 
65
XML-RPC
 
66
+++++++
 
67
 
 
68
.. i18n: XML-RPC Architecture
 
69
.. i18n: """"""""""""""""""""
 
70
 
 
71
XML-RPC Architecture
 
72
""""""""""""""""""""
 
73
 
 
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.
 
75
 
 
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.
 
77
 
 
78
.. i18n: Architecture
 
79
.. i18n: """"""""""""
 
80
 
 
81
Architecture
 
82
""""""""""""
 
83
 
 
84
.. i18n: The diagram below synthesizes the client server architecture of Open ERP. Open ERP server and Open ERP clients communicate using XML-RPC.
 
85
 
 
86
The diagram below synthesizes the client server architecture of Open ERP. Open ERP server and Open ERP clients communicate using XML-RPC.
 
87
 
 
88
.. i18n: .. figure:: images/tech_arch.png
 
89
.. i18n:   :scale: 85
 
90
.. i18n:   :align: center
 
91
 
 
92
.. figure:: images/tech_arch.png
 
93
  :scale: 85
 
94
  :align: center
 
95
 
 
96
.. i18n: **Client**
 
97
 
 
98
**Client**
 
99
 
 
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.
 
101
 
 
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.
 
103
 
 
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.
 
105
 
 
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.
 
107
 
 
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.
 
109
 
 
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.
 
111
 
 
112
.. i18n: There are three types of action;
 
113
 
 
114
There are three types of action;
 
115
 
 
116
.. i18n:     * Open a window (form or tree)
 
117
.. i18n:     * Print a document
 
118
.. i18n:     * Execute a wizard
 
119
 
 
120
    * Open a window (form or tree)
 
121
    * Print a document
 
122
    * Execute a wizard
 
123
 
 
124
.. i18n: Python
 
125
.. i18n: ++++++
 
126
 
 
127
Python
 
128
++++++
 
129
 
 
130
.. i18n: Access tiny-server using xml-rpc
 
131
.. i18n: """"""""""""""""""""""""""""""""
 
132
 
 
133
Access tiny-server using xml-rpc
 
134
""""""""""""""""""""""""""""""""
 
135
 
 
136
.. i18n: Demo script
 
137
.. i18n: ~~~~~~~~~~~
 
138
 
 
139
Demo script
 
140
~~~~~~~~~~~
 
141
 
 
142
.. i18n:     * **Create a partner and his address**
 
143
 
 
144
    * **Create a partner and his address**
 
145
 
 
146
.. i18n: import xmlrpclib
 
147
.. i18n: """"""""""""""""
 
148
.. i18n: ::
 
149
.. i18n: 
 
150
.. i18n:     username = 'admin' #the user
 
151
.. i18n:     pwd = 'admin'      #the password of the user
 
152
.. i18n:     dbname = 'terp'    #the database
 
153
 
 
154
import xmlrpclib
 
155
""""""""""""""""
 
156
::
 
157
 
 
158
    username = 'admin' #the user
 
159
    pwd = 'admin'      #the password of the user
 
160
    dbname = 'terp'    #the database
 
161
 
 
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)
 
165
 
 
166
    # Get the uid
 
167
    sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
 
168
    uid = sock_common.login(dbname, username, pwd)
 
169
 
 
170
.. i18n:     #replace localhost with the address of the server
 
171
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
172
 
 
173
    #replace localhost with the address of the server
 
174
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
175
 
 
176
.. i18n:     partner = {
 
177
.. i18n:        'name': 'Fabien Pinckaers',
 
178
.. i18n:        'lang': 'fr_FR',
 
179
.. i18n:     }
 
180
 
 
181
    partner = {
 
182
       'name': 'Fabien Pinckaers',
 
183
       'lang': 'fr_FR',
 
184
    }
 
185
 
 
186
.. i18n:     partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
 
187
 
 
188
    partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
 
189
 
 
190
.. i18n:     address = {
 
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',
 
198
.. i18n:     }
 
199
 
 
200
    address = {
 
201
       'partner_id': partner_id,
 
202
       'type' : 'default',
 
203
       'street': 'Chaussée de Namur 40',
 
204
       'zip': '1367',
 
205
       'city': 'Grand-Rosière',
 
206
       'phone': '+3281813700',
 
207
       'fax': '+3281733501',
 
208
    }
 
209
 
 
210
.. i18n:     address_id = sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
 
211
 
 
212
    address_id = sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
 
213
 
 
214
.. i18n: * **Search a partner**
 
215
.. i18n:   ::
 
216
.. i18n: 
 
217
.. i18n:     args = [('vat', '=', 'ZZZZZZ')] #query clause
 
218
.. i18n:     ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', args)
 
219
.. i18n: 
 
220
.. i18n: * **Read partner data**
 
221
.. i18n:   ::
 
222
.. i18n: 
 
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
 
225
.. i18n: 
 
226
.. i18n: * **Update partner data**
 
227
.. i18n:   ::
 
228
.. i18n: 
 
229
.. i18n:     values = {'vat': 'ZZ1ZZZ'} #data to update
 
230
.. i18n:     result = sock.execute(dbname, uid, pwd, 'res.partner', 'write', ids, values)
 
231
.. i18n: 
 
232
.. i18n: * **Delete partner**
 
233
.. i18n:   ::
 
234
.. i18n: 
 
235
.. i18n:     # ids : list of id
 
236
.. i18n:     result = sock.execute(dbname, uid, pwd, 'res.partner', 'unlink', ids)
 
237
 
 
238
* **Search a partner**
 
239
  ::
 
240
 
 
241
    args = [('vat', '=', 'ZZZZZZ')] #query clause
 
242
    ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', args)
 
243
 
 
244
* **Read partner data**
 
245
  ::
 
246
 
 
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
 
249
 
 
250
* **Update partner data**
 
251
  ::
 
252
 
 
253
    values = {'vat': 'ZZ1ZZZ'} #data to update
 
254
    result = sock.execute(dbname, uid, pwd, 'res.partner', 'write', ids, values)
 
255
 
 
256
* **Delete partner**
 
257
  ::
 
258
 
 
259
    # ids : list of id
 
260
    result = sock.execute(dbname, uid, pwd, 'res.partner', 'unlink', ids)
 
261
 
 
262
.. i18n: PHP
 
263
.. i18n: +++
 
264
 
 
265
PHP
 
266
+++
 
267
 
 
268
.. i18n: Access Open-server using xml-rpc
 
269
.. i18n: """"""""""""""""""""""""""""""""
 
270
 
 
271
Access Open-server using xml-rpc
 
272
""""""""""""""""""""""""""""""""
 
273
 
 
274
.. i18n: **Download the XML-RPC framework for PHP**
 
275
 
 
276
**Download the XML-RPC framework for PHP**
 
277
 
 
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
 
279
 
 
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
 
281
 
 
282
.. i18n: **Setup the XML-RPC for PHP**
 
283
 
 
284
**Setup the XML-RPC for PHP**
 
285
 
 
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
 
287
 
 
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
 
289
 
 
290
.. i18n: **Demo script**
 
291
 
 
292
**Demo script**
 
293
 
 
294
.. i18n: * **Login**
 
295
 
 
296
* **Login**
 
297
 
 
298
.. i18n: .. code-block:: php
 
299
.. i18n: 
 
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/';
 
305
.. i18n: 
 
306
.. i18n:        if(isset($_COOKIE["user_id"]) == true)  {
 
307
.. i18n:            if($_COOKIE["user_id"]>0) {
 
308
.. i18n:            return $_COOKIE["user_id"];
 
309
.. i18n:            }
 
310
.. i18n:        }
 
311
.. i18n: 
 
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) {
 
322
.. i18n:            return $id;
 
323
.. i18n:        }else{
 
324
.. i18n:            return -1;
 
325
.. i18n:        }
 
326
.. i18n:      }
 
327
 
 
328
.. code-block:: php
 
329
 
 
330
    function connect() {
 
331
       var $user = 'admin';
 
332
       var $password = 'admin';
 
333
       var $dbname = 'db_name';
 
334
       var $server_url = 'http://localhost:8069/xmlrpc/';
 
335
 
 
336
       if(isset($_COOKIE["user_id"]) == true)  {
 
337
           if($_COOKIE["user_id"]>0) {
 
338
           return $_COOKIE["user_id"];
 
339
           }
 
340
       }
 
341
 
 
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);
 
351
       if($id > 0) {
 
352
           return $id;
 
353
       }else{
 
354
           return -1;
 
355
       }
 
356
     }
 
357
 
 
358
.. i18n: * **Search**
 
359
 
 
360
* **Search**
 
361
 
 
362
.. i18n: .. code-block:: php
 
363
.. i18n: 
 
364
.. i18n:     /**
 
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
 
370
.. i18n:      */
 
371
.. i18n: 
 
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/';
 
378
.. i18n: 
 
379
.. i18n:          $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
 
380
.. i18n:                   new xmlrpcval($operator,"string"),
 
381
.. i18n:                   new xmlrpcval($keys,"string")),"array"),
 
382
.. i18n:             );
 
383
.. i18n: 
 
384
.. i18n:          if($userId<=0) {
 
385
.. i18n:          connect();
 
386
.. i18n:          }
 
387
.. i18n: 
 
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"));
 
395
.. i18n: 
 
396
.. i18n:          $resp = $client->send($msg);
 
397
.. i18n:          $val = $resp->value();
 
398
.. i18n:          $ids = $val->scalarval();
 
399
.. i18n: 
 
400
.. i18n:          return $ids;
 
401
.. i18n:     }
 
402
 
 
403
.. code-block:: php
 
404
 
 
405
    /**
 
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, =, !=
 
410
     * $key=search for
 
411
     */
 
412
 
 
413
    function search($client,$relation,$attribute,$operator,$keys) {
 
414
         var $user = 'admin';
 
415
         var $password = 'admin';
 
416
         var $userId = -1;
 
417
         var $dbname = 'db_name';
 
418
         var $server_url = 'http://localhost:8069/xmlrpc/';
 
419
 
 
420
         $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
 
421
                  new xmlrpcval($operator,"string"),
 
422
                  new xmlrpcval($keys,"string")),"array"),
 
423
            );
 
424
 
 
425
         if($userId<=0) {
 
426
         connect();
 
427
         }
 
428
 
 
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"));
 
436
 
 
437
         $resp = $client->send($msg);
 
438
         $val = $resp->value();
 
439
         $ids = $val->scalarval();
 
440
 
 
441
         return $ids;
 
442
    }
 
443
 
 
444
.. i18n: * **Create**
 
445
.. i18n:   ::
 
446
.. i18n: 
 
447
.. i18n:     TODO
 
448
.. i18n: 
 
449
.. i18n: * **Write**
 
450
.. i18n:   ::
 
451
.. i18n: 
 
452
.. i18n:     TODO
 
453
 
 
454
* **Create**
 
455
  ::
 
456
 
 
457
    TODO
 
458
 
 
459
* **Write**
 
460
  ::
 
461
 
 
462
    TODO
 
463
 
 
464
.. i18n: JAVA
 
465
.. i18n: ++++
 
466
 
 
467
JAVA
 
468
++++
 
469
 
 
470
.. i18n: Access Open-server using xml-rpc
 
471
.. i18n: """"""""""""""""""""""""""""""""
 
472
 
 
473
Access Open-server using xml-rpc
 
474
""""""""""""""""""""""""""""""""
 
475
 
 
476
.. i18n: **Download the apache XML-RPC framework for JAVA**
 
477
 
 
478
**Download the apache XML-RPC framework for JAVA**
 
479
 
 
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.
 
481
 
 
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.
 
483
 
 
484
.. i18n: **Demo script**
 
485
 
 
486
**Demo script**
 
487
 
 
488
.. i18n: * **Find Databases**
 
489
 
 
490
* **Find Databases**
 
491
 
 
492
.. i18n: .. code-block:: java
 
493
.. i18n: 
 
494
.. i18n:     import java.net.URL;
 
495
.. i18n:     import java.util.Vector;
 
496
.. i18n: 
 
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;
 
501
.. i18n: 
 
502
.. i18n:     public Vector<String> getDatabaseList(String host, int port)
 
503
.. i18n:     {
 
504
.. i18n:       XmlRpcClient xmlrpcDb = new XmlRpcClient();
 
505
.. i18n: 
 
506
.. i18n:       XmlRpcClientConfigImpl xmlrpcConfigDb = new XmlRpcClientConfigImpl();
 
507
.. i18n:       xmlrpcConfigDb.setEnabledForExtensions(true);
 
508
.. i18n:       xmlrpcConfigDb.setServerURL(new URL("http",host,port,"/xmlrpc/db"));
 
509
.. i18n: 
 
510
.. i18n:       xmlrpcDb.setConfig(xmlrpcConfigDb);
 
511
.. i18n: 
 
512
.. i18n:       try {
 
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;
 
517
.. i18n: 
 
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)
 
521
.. i18n:         {
 
522
.. i18n:           res.addElement((String)a[i]);
 
523
.. i18n:         }
 
524
.. i18n:       }
 
525
.. i18n:       catch (XmlRpcException e) {
 
526
.. i18n:         logger.warn("XmlException Error while retrieving TinyERP Databases: ",e);
 
527
.. i18n:         return -2;
 
528
.. i18n:       }
 
529
.. i18n:       catch (Exception e)
 
530
.. i18n:       {
 
531
.. i18n:         logger.warn("Error while retrieving TinyERP Databases: ",e);
 
532
.. i18n:         return -3;
 
533
.. i18n:       }
 
534
.. i18n:     }
 
535
 
 
536
.. code-block:: java
 
537
 
 
538
    import java.net.URL;
 
539
    import java.util.Vector;
 
540
 
 
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;
 
545
 
 
546
    public Vector<String> getDatabaseList(String host, int port)
 
547
    {
 
548
      XmlRpcClient xmlrpcDb = new XmlRpcClient();
 
549
 
 
550
      XmlRpcClientConfigImpl xmlrpcConfigDb = new XmlRpcClientConfigImpl();
 
551
      xmlrpcConfigDb.setEnabledForExtensions(true);
 
552
      xmlrpcConfigDb.setServerURL(new URL("http",host,port,"/xmlrpc/db"));
 
553
 
 
554
      xmlrpcDb.setConfig(xmlrpcConfigDb);
 
555
 
 
556
      try {
 
557
        //Retrieve databases
 
558
        Vector<Object> params = new Vector<Object>();
 
559
        Object result = xmlrpcDb.execute("list", params);
 
560
        Object[] a = (Object[]) result;
 
561
 
 
562
        Vector<String> res = new Vector<String>();
 
563
        for (int i = 0; i < a.length; i++) {
 
564
        if (a[i] instanceof String)
 
565
        {
 
566
          res.addElement((String)a[i]);
 
567
        }
 
568
      }
 
569
      catch (XmlRpcException e) {
 
570
        logger.warn("XmlException Error while retrieving TinyERP Databases: ",e);
 
571
        return -2;
 
572
      }
 
573
      catch (Exception e)
 
574
      {
 
575
        logger.warn("Error while retrieving TinyERP Databases: ",e);
 
576
        return -3;
 
577
      }
 
578
    }
 
579
 
 
580
.. i18n: * **Login**
 
581
 
 
582
* **Login**
 
583
 
 
584
.. i18n: .. code-block:: java
 
585
.. i18n: 
 
586
.. i18n:     import java.net.URL;
 
587
.. i18n: 
 
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;
 
592
.. i18n: 
 
593
.. i18n:     public int Connect(String host, int port, String tinydb, String login, String password)
 
594
.. i18n:     {
 
595
.. i18n:       XmlRpcClient xmlrpcLogin = new XmlRpcClient();
 
596
.. i18n: 
 
597
.. i18n:       XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
 
598
.. i18n:       xmlrpcConfigLogin.setEnabledForExtensions(true);
 
599
.. i18n:       xmlrpcConfigLogin.setServerURL(new URL("http",host,port,"/xmlrpc/common"));
 
600
.. i18n: 
 
601
.. i18n:       xmlrpcLogin.setConfig(xmlrpcConfigLogin);
 
602
.. i18n: 
 
603
.. i18n:       try {
 
604
.. i18n:         //Connect
 
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;
 
609
.. i18n:         return -1;
 
610
.. i18n:       }
 
611
.. i18n:       catch (XmlRpcException e) {
 
612
.. i18n:         logger.warn("XmlException Error while logging to TinyERP: ",e);
 
613
.. i18n:         return -2;
 
614
.. i18n:       }
 
615
.. i18n:       catch (Exception e)
 
616
.. i18n:       {
 
617
.. i18n:         logger.warn("Error while logging to TinyERP: ",e);
 
618
.. i18n:         return -3;
 
619
.. i18n:       }
 
620
.. i18n:     }
 
621
 
 
622
.. code-block:: java
 
623
 
 
624
    import java.net.URL;
 
625
 
 
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;
 
630
 
 
631
    public int Connect(String host, int port, String tinydb, String login, String password)
 
632
    {
 
633
      XmlRpcClient xmlrpcLogin = new XmlRpcClient();
 
634
 
 
635
      XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();
 
636
      xmlrpcConfigLogin.setEnabledForExtensions(true);
 
637
      xmlrpcConfigLogin.setServerURL(new URL("http",host,port,"/xmlrpc/common"));
 
638
 
 
639
      xmlrpcLogin.setConfig(xmlrpcConfigLogin);
 
640
 
 
641
      try {
 
642
        //Connect
 
643
        params = new Object[] {tinydb,login,password};
 
644
        Object id = xmlrpcLogin.execute("login", params);
 
645
        if (id instanceof Integer)
 
646
          return (Integer)id;
 
647
        return -1;
 
648
      }
 
649
      catch (XmlRpcException e) {
 
650
        logger.warn("XmlException Error while logging to TinyERP: ",e);
 
651
        return -2;
 
652
      }
 
653
      catch (Exception e)
 
654
      {
 
655
        logger.warn("Error while logging to TinyERP: ",e);
 
656
        return -3;
 
657
      }
 
658
    }
 
659
 
 
660
.. i18n: * **Search**
 
661
.. i18n:   ::
 
662
.. i18n: 
 
663
.. i18n:     TODO
 
664
.. i18n: 
 
665
.. i18n: * **Create**
 
666
.. i18n:   ::
 
667
.. i18n: 
 
668
.. i18n:     TODO
 
669
.. i18n: 
 
670
.. i18n: * **Write**
 
671
.. i18n:   ::
 
672
.. i18n: 
 
673
.. i18n:     TODO
 
674
 
 
675
* **Search**
 
676
  ::
 
677
 
 
678
    TODO
 
679
 
 
680
* **Create**
 
681
  ::
 
682
 
 
683
    TODO
 
684
 
 
685
* **Write**
 
686
  ::
 
687
 
 
688
    TODO
 
689
 
 
690
.. i18n: Python Example
 
691
.. i18n: --------------
 
692
 
 
693
Python Example
 
694
--------------
 
695
 
 
696
.. i18n: Example of creation of a partner and his address.
 
697
 
 
698
Example of creation of a partner and his address.
 
699
 
 
700
.. i18n: .. code-block:: python
 
701
.. i18n: 
 
702
.. i18n:     import xmlrpclib
 
703
.. i18n: 
 
704
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
705
.. i18n:     uid = 1
 
706
.. i18n:     pwd = 'demo'
 
707
.. i18n: 
 
708
.. i18n:     partner = {
 
709
.. i18n:         'title': 'Monsieur',
 
710
.. i18n:         'name': 'Fabien Pinckaers',
 
711
.. i18n:         'lang': 'fr',
 
712
.. i18n:         'active': True,
 
713
.. i18n:     }
 
714
.. i18n: 
 
715
.. i18n:     partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
 
716
.. i18n: 
 
717
.. i18n:     address = {
 
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',
 
725
.. i18n:     }
 
726
.. i18n: 
 
727
.. i18n:     sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
 
728
 
 
729
.. code-block:: python
 
730
 
 
731
    import xmlrpclib
 
732
 
 
733
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
734
    uid = 1
 
735
    pwd = 'demo'
 
736
 
 
737
    partner = {
 
738
        'title': 'Monsieur',
 
739
        'name': 'Fabien Pinckaers',
 
740
        'lang': 'fr',
 
741
        'active': True,
 
742
    }
 
743
 
 
744
    partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
 
745
 
 
746
    address = {
 
747
        'partner_id': partner_id,
 
748
        'type': 'default',
 
749
        'street': 'Rue du vieux chateau, 21',
 
750
        'zip': '1457',
 
751
        'city': 'Walhain',
 
752
        'phone': '(+32)10.68.94.39',
 
753
        'fax': '(+32)10.68.94.39',
 
754
    }
 
755
 
 
756
    sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
 
757
 
 
758
.. i18n: To get the UID of a user, you can use the following script:
 
759
 
 
760
To get the UID of a user, you can use the following script:
 
761
 
 
762
.. i18n: .. code-block:: python
 
763
.. i18n: 
 
764
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
765
.. i18n:      UID = sock.login('terp3', 'admin', 'admin')
 
766
 
 
767
.. code-block:: python
 
768
 
 
769
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
770
     UID = sock.login('terp3', 'admin', 'admin')
 
771
 
 
772
.. i18n: CRUD example:
 
773
 
 
774
CRUD example:
 
775
 
 
776
.. i18n: .. code-block:: python
 
777
.. i18n: 
 
778
.. i18n:     """
 
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
 
783
.. i18n:     """
 
784
.. i18n:     import xmlrpclib
 
785
.. i18n: 
 
786
.. i18n:     user = 'admin'
 
787
.. i18n:     pwd = 'admin'
 
788
.. i18n:     dbname = 'terp3'
 
789
.. i18n:     model = 'res.partner'
 
790
.. i18n: 
 
791
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
792
.. i18n:     uid = sock.login(dbname ,user ,pwd)
 
793
.. i18n: 
 
794
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
795
.. i18n: 
 
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)
 
799
.. i18n: 
 
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
 
804
.. i18n: 
 
805
.. i18n:     # SEARCH PARTNERS
 
806
.. i18n:     args = [('vat', '=', 'ZZZZZ'),]
 
807
.. i18n:     ids = sock.execute(dbname, uid, pwd, model, 'search', args)
 
808
.. i18n: 
 
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
 
813
.. i18n: 
 
814
.. i18n:     # EDIT PARTNER DATA
 
815
.. i18n:     values = {'vat':'ZZ1ZZ'}
 
816
.. i18n:     results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
 
817
.. i18n: 
 
818
.. i18n:     # DELETE PARTNER DATA
 
819
.. i18n:     results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
 
820
 
 
821
.. code-block:: python
 
822
 
 
823
    """
 
824
    :The login function is under
 
825
    ::    http://localhost:8069/xmlrpc/common
 
826
    :For object retrieval use:
 
827
    ::    http://localhost:8069/xmlrpc/object
 
828
    """
 
829
    import xmlrpclib
 
830
 
 
831
    user = 'admin'
 
832
    pwd = 'admin'
 
833
    dbname = 'terp3'
 
834
    model = 'res.partner'
 
835
 
 
836
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
837
    uid = sock.login(dbname ,user ,pwd)
 
838
 
 
839
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
840
 
 
841
    # CREATE A PARTNER
 
842
    partner_data = {'name':'Tiny', 'active':True, 'vat':'ZZZZZ'}
 
843
    partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
 
844
 
 
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
 
849
 
 
850
    # SEARCH PARTNERS
 
851
    args = [('vat', '=', 'ZZZZZ'),]
 
852
    ids = sock.execute(dbname, uid, pwd, model, 'search', args)
 
853
 
 
854
    # READ PARTNER DATA
 
855
    fields = ['name', 'active', 'vat', 'ref']
 
856
    results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
 
857
    print results
 
858
 
 
859
    # EDIT PARTNER DATA
 
860
    values = {'vat':'ZZ1ZZ'}
 
861
    results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
 
862
 
 
863
    # DELETE PARTNER DATA
 
864
    results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
 
865
 
 
866
.. i18n: PRINT example:
 
867
 
 
868
PRINT example:
 
869
 
 
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')])
 
873
 
 
874
   1. PRINT 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')])
 
877
 
 
878
.. i18n: .. code-block:: python
 
879
.. i18n: 
 
880
.. i18n:     import time
 
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
 
887
.. i18n:     attempt = 0
 
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 !'
 
896
.. i18n: 
 
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()
 
901
 
 
902
.. code-block:: python
 
903
 
 
904
    import time
 
905
    import base64
 
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'})
 
909
    time.sleep(5)
 
910
    state = False
 
911
    attempt = 0
 
912
    while not state:
 
913
        report = printsock.report_get(dbname, uid, pwd, id_report)
 
914
        state = report['state']
 
915
        if not state:
 
916
        time.sleep(1)
 
917
        attempt += 1
 
918
        if attempt>200:
 
919
        print 'Printing aborted, too long delay !'
 
920
 
 
921
        string_pdf = base64.decodestring(report['result'])
 
922
        file_pdf = open('/tmp/file.pdf','w')
 
923
        file_pdf.write(string_pdf)
 
924
        file_pdf.close()
 
925
 
 
926
.. i18n: PHP Example
 
927
.. i18n: -----------
 
928
 
 
929
PHP Example
 
930
-----------
 
931
 
 
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.
 
933
 
 
934
Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
 
935
 
 
936
.. i18n: .. code-block:: php
 
937
.. i18n: 
 
938
.. i18n:         <?
 
939
.. i18n: 
 
940
.. i18n:         include('xmlrpc.inc');
 
941
.. i18n: 
 
942
.. i18n:         $arrayVal = array(
 
943
.. i18n:         'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
 
944
.. i18n:         'vat'=>new xmlrpcval('BE477472701' , "string")
 
945
.. i18n:         );
 
946
.. i18n: 
 
947
.. i18n:         $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
 
948
.. i18n: 
 
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"));
 
956
.. i18n: 
 
957
.. i18n:         $resp = $client->send($msg);
 
958
.. i18n: 
 
959
.. i18n:         if ($resp->faultCode())
 
960
.. i18n: 
 
961
.. i18n:             echo 'Error: '.$resp->faultString();
 
962
.. i18n: 
 
963
.. i18n:         else
 
964
.. i18n: 
 
965
.. i18n:             echo 'Partner '.$resp->value()->scalarval().' created !';
 
966
.. i18n: 
 
967
.. i18n:         ?>
 
968
 
 
969
.. code-block:: php
 
970
 
 
971
        <?
 
972
 
 
973
        include('xmlrpc.inc');
 
974
 
 
975
        $arrayVal = array(
 
976
        'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
 
977
        'vat'=>new xmlrpcval('BE477472701' , "string")
 
978
        );
 
979
 
 
980
        $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
 
981
 
 
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"));
 
989
 
 
990
        $resp = $client->send($msg);
 
991
 
 
992
        if ($resp->faultCode())
 
993
 
 
994
            echo 'Error: '.$resp->faultString();
 
995
 
 
996
        else
 
997
 
 
998
            echo 'Partner '.$resp->value()->scalarval().' created !';
 
999
 
 
1000
        ?>