~openerp-community/openobject-doc/ksa-openobject-doc-6.0

« back to all changes in this revision

Viewing changes to i18n/ru/source/developer/17_Web_services/index.rst

  • Committer: Don Kirkby
  • Date: 2011-02-21 20:46:11 UTC
  • mfrom: (433.1.53 openobject-doc)
  • Revision ID: donkirkby+launpd@gmail.com-20110221204611-1ykt6dmg4k3gh5dh
[MERGE] revisions 477 to 486 from the 5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
.. i18n: ============
 
3
.. i18n: Introduction
 
4
.. i18n: ============
 
5
..
 
6
 
 
7
============
 
8
Introduction
 
9
============
 
10
 
 
11
.. i18n: How to load data ?
 
12
.. i18n: ==================
 
13
..
 
14
 
 
15
How to load data ?
 
16
==================
 
17
 
 
18
.. i18n:    #. Postgresql
 
19
.. i18n:           * Simple, standard
 
20
.. i18n:           * Does not respect the WORKFLOW !!!
 
21
.. i18n:    #. XML files (with –update=)
 
22
.. i18n:    #. XML-RPC
 
23
.. i18n:           * Script, same as website interface
 
24
..
 
25
 
 
26
   #. Postgresql
 
27
          * Simple, standard
 
28
          * Does not respect the WORKFLOW !!!
 
29
   #. XML files (with –update=)
 
30
   #. XML-RPC
 
31
          * Script, same as website interface
 
32
 
 
33
.. i18n: How to backup/restore a Postgresql database?
 
34
.. i18n: ============================================
 
35
..
 
36
 
 
37
How to backup/restore a Postgresql database?
 
38
============================================
 
39
 
 
40
.. i18n: :backup:
 
41
..
 
42
 
 
43
:backup:
 
44
 
 
45
.. i18n:     pg_dump terp >terp.sql
 
46
..
 
47
 
 
48
    pg_dump terp >terp.sql
 
49
 
 
50
.. i18n: :restore:
 
51
..
 
52
 
 
53
:restore:
 
54
 
 
55
.. i18n:     createdb terp --encoding=unicode
 
56
.. i18n:     psql terp < terp.sql
 
57
.. i18n:     or
 
58
.. i18n:     psql -d terp -f terp.sql
 
59
..
 
60
 
 
61
    createdb terp --encoding=unicode
 
62
    psql terp < terp.sql
 
63
    or
 
64
    psql -d terp -f terp.sql
 
65
 
 
66
.. i18n: The objects methods
 
67
.. i18n: ===================
 
68
..
 
69
 
 
70
The objects methods
 
71
===================
 
72
 
 
73
.. i18n:    #. create({'field':'value'})
 
74
.. i18n:           * return ID created
 
75
.. i18n:    #. search([('arg1','=','value1')...], offset=0, limit=1000)
 
76
.. i18n:           * return [IDS] found
 
77
.. i18n:    #. read([IDS], ['field1','field2',...])
 
78
.. i18n:           * return [{'id':1, 'field1':..., 'field2':..., ...}, ...]
 
79
.. i18n:    #. write([IDS], {'field1':'value1','field2':3})
 
80
.. i18n:           * return True
 
81
.. i18n:    #. unlink([IDS])
 
82
.. i18n:           * return True
 
83
..
 
84
 
 
85
   #. create({'field':'value'})
 
86
          * return ID created
 
87
   #. search([('arg1','=','value1')...], offset=0, limit=1000)
 
88
          * return [IDS] found
 
89
   #. read([IDS], ['field1','field2',...])
 
90
          * return [{'id':1, 'field1':..., 'field2':..., ...}, ...]
 
91
   #. write([IDS], {'field1':'value1','field2':3})
 
92
          * return True
 
93
   #. unlink([IDS])
 
94
          * return True
 
95
 
 
96
.. i18n: =================
 
97
.. i18n: WebservicesXMLRPC
 
98
.. i18n: =================
 
99
..
 
100
 
 
101
=================
 
102
WebservicesXMLRPC
 
103
=================
 
104
 
 
105
.. i18n: #. XML-RPC
 
106
.. i18n:       * standard: http://www.xmlrpc.org
 
107
.. i18n:       * RPC Over HTTP
 
108
.. i18n:       * Function Parameters & Result encoded in XML
 
109
.. i18n: #. Principle;
 
110
.. i18n:       * calls to objects methodes;
 
111
.. i18n:             - read, write
 
112
.. i18n:             - create
 
113
.. i18n:             - unlink (=delete)
 
114
..
 
115
 
 
116
#. XML-RPC
 
117
      * standard: http://www.xmlrpc.org
 
118
      * RPC Over HTTP
 
119
      * Function Parameters & Result encoded in XML
 
120
#. Principle;
 
121
      * calls to objects methodes;
 
122
            - read, write
 
123
            - create
 
124
            - unlink (=delete)
 
125
 
 
126
.. 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.
 
127
..
 
128
 
 
129
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.
 
130
 
 
131
.. 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.
 
132
..
 
133
 
 
134
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.
 
135
 
 
136
.. 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.
 
137
..
 
138
 
 
139
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.
 
140
 
 
141
.. 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/).
 
142
..
 
143
 
 
144
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/).
 
145
 
 
146
.. 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:
 
147
..
 
148
 
 
149
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:
 
150
 
 
151
.. i18n: XML-RPC Home Page\\\\ XML-RPC for C and C++\\\\ The Apache XML-RPC Project\\\\ Expat: The XML Parser\\\\
 
152
..
 
153
 
 
154
XML-RPC Home Page\\\\ XML-RPC for C and C++\\\\ The Apache XML-RPC Project\\\\ Expat: The XML Parser\\\\
 
155
 
 
156
.. i18n: =================
 
157
.. i18n: An example in PHP
 
158
.. i18n: =================
 
159
..
 
160
 
 
161
=================
 
162
An example in PHP
 
163
=================
 
164
 
 
165
.. i18n: Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
 
166
..
 
167
 
 
168
Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
 
169
 
 
170
.. i18n: .. code-block:: php
 
171
.. i18n: 
 
172
.. i18n:     <?
 
173
.. i18n: 
 
174
.. i18n:         include('xmlrpc.inc');
 
175
.. i18n: 
 
176
.. i18n:         $arrayVal = array(
 
177
.. i18n:         'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
 
178
.. i18n:         'vat'=>new xmlrpcval('BE477472701' , "string")
 
179
.. i18n:         );
 
180
.. i18n: 
 
181
.. i18n:         $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
 
182
.. i18n: 
 
183
.. i18n:         $msg = new xmlrpcmsg('execute');
 
184
.. i18n:         $msg->addParam(new xmlrpcval("dbname", "string"));
 
185
.. i18n:         $msg->addParam(new xmlrpcval("3", "int"));
 
186
.. i18n:         $msg->addParam(new xmlrpcval("demo", "string"));
 
187
.. i18n:         $msg->addParam(new xmlrpcval("res.partner", "string"));
 
188
.. i18n:         $msg->addParam(new xmlrpcval("create", "string"));
 
189
.. i18n:         $msg->addParam(new xmlrpcval($arrayVal, "struct"));
 
190
.. i18n: 
 
191
.. i18n:         $resp = $client->send($msg);
 
192
.. i18n: 
 
193
.. i18n:         if ($resp->faultCode())
 
194
.. i18n: 
 
195
.. i18n:             echo 'Error: '.$resp->faultString();
 
196
.. i18n: 
 
197
.. i18n:         else
 
198
.. i18n: 
 
199
.. i18n:             echo 'Partner '.$resp->value()->scalarval().' created !';
 
200
.. i18n: 
 
201
.. i18n:     ?>
 
202
..
 
203
 
 
204
.. code-block:: php
 
205
 
 
206
    <?
 
207
 
 
208
        include('xmlrpc.inc');
 
209
 
 
210
        $arrayVal = array(
 
211
        'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
 
212
        'vat'=>new xmlrpcval('BE477472701' , "string")
 
213
        );
 
214
 
 
215
        $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
 
216
 
 
217
        $msg = new xmlrpcmsg('execute');
 
218
        $msg->addParam(new xmlrpcval("dbname", "string"));
 
219
        $msg->addParam(new xmlrpcval("3", "int"));
 
220
        $msg->addParam(new xmlrpcval("demo", "string"));
 
221
        $msg->addParam(new xmlrpcval("res.partner", "string"));
 
222
        $msg->addParam(new xmlrpcval("create", "string"));
 
223
        $msg->addParam(new xmlrpcval($arrayVal, "struct"));
 
224
 
 
225
        $resp = $client->send($msg);
 
226
 
 
227
        if ($resp->faultCode())
 
228
 
 
229
            echo 'Error: '.$resp->faultString();
 
230
 
 
231
        else
 
232
 
 
233
            echo 'Partner '.$resp->value()->scalarval().' created !';
 
234
 
 
235
    ?>
 
236
 
 
237
.. i18n: ====================
 
238
.. i18n: An example in Python
 
239
.. i18n: ====================
 
240
..
 
241
 
 
242
====================
 
243
An example in Python
 
244
====================
 
245
 
 
246
.. i18n: Example of creation of a partner and his address.
 
247
..
 
248
 
 
249
Example of creation of a partner and his address.
 
250
 
 
251
.. i18n: .. code-block:: python
 
252
.. i18n: 
 
253
.. i18n:     import xmlrpclib
 
254
.. i18n: 
 
255
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
256
.. i18n:     uid = 1
 
257
.. i18n:     pwd = 'demo'
 
258
.. i18n: 
 
259
.. i18n:     partner = {
 
260
.. i18n:         'title': 'Monsieur',
 
261
.. i18n:         'name': 'Fabien Pinckaers',
 
262
.. i18n:         'lang': 'fr',
 
263
.. i18n:         'active': True,
 
264
.. i18n:     }
 
265
.. i18n: 
 
266
.. i18n:     partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
 
267
.. i18n: 
 
268
.. i18n:     address = {
 
269
.. i18n:         'partner_id': partner_id,
 
270
.. i18n:         'type': 'default',
 
271
.. i18n:         'street': 'Rue du vieux chateau, 21',
 
272
.. i18n:         'zip': '1457',
 
273
.. i18n:         'city': 'Walhain',
 
274
.. i18n:         'phone': '(+32)10.68.94.39',
 
275
.. i18n:         'fax': '(+32)10.68.94.39',
 
276
.. i18n:     }
 
277
.. i18n: 
 
278
.. i18n:     sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
 
279
..
 
280
 
 
281
.. code-block:: python
 
282
 
 
283
    import xmlrpclib
 
284
 
 
285
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
286
    uid = 1
 
287
    pwd = 'demo'
 
288
 
 
289
    partner = {
 
290
        'title': 'Monsieur',
 
291
        'name': 'Fabien Pinckaers',
 
292
        'lang': 'fr',
 
293
        'active': True,
 
294
    }
 
295
 
 
296
    partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
 
297
 
 
298
    address = {
 
299
        'partner_id': partner_id,
 
300
        'type': 'default',
 
301
        'street': 'Rue du vieux chateau, 21',
 
302
        'zip': '1457',
 
303
        'city': 'Walhain',
 
304
        'phone': '(+32)10.68.94.39',
 
305
        'fax': '(+32)10.68.94.39',
 
306
    }
 
307
 
 
308
    sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
 
309
 
 
310
.. i18n: To get the UID of a user, you can use the following script:
 
311
..
 
312
 
 
313
To get the UID of a user, you can use the following script:
 
314
 
 
315
.. i18n: .. code-block:: python
 
316
.. i18n: 
 
317
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
318
.. i18n:     UID = sock.login('terp3', 'admin', 'admin')
 
319
..
 
320
 
 
321
.. code-block:: python
 
322
 
 
323
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
324
    UID = sock.login('terp3', 'admin', 'admin')
 
325
 
 
326
.. i18n: CRUD example:
 
327
..
 
328
 
 
329
CRUD example:
 
330
 
 
331
.. i18n: .. code-block:: python
 
332
.. i18n: 
 
333
.. i18n:     """
 
334
.. i18n:     :The login function is under
 
335
.. i18n:     ::    http://localhost:8069/xmlrpc/common
 
336
.. i18n:     :For object retrieval use:
 
337
.. i18n:     ::    http://localhost:8069/xmlrpc/object
 
338
.. i18n:     """
 
339
.. i18n:     import xmlrpclib
 
340
.. i18n: 
 
341
.. i18n:     user = 'admin'
 
342
.. i18n:     pwd = 'admin'
 
343
.. i18n:     dbname = 'terp3'
 
344
.. i18n:     model = 'res.partner'
 
345
.. i18n: 
 
346
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
347
.. i18n:     uid = sock.login(dbname ,user ,pwd)
 
348
.. i18n: 
 
349
.. i18n:     sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
350
.. i18n: 
 
351
.. i18n:     # CREATE A PARTNER
 
352
.. i18n:     partner_data = {'name':'Tiny', 'active':True, 'vat':'ZZZZZ'}
 
353
.. i18n:     partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
 
354
.. i18n: 
 
355
.. i18n:     # The relation between res.partner and res.partner.category is of type many2many
 
356
.. i18n:     # To add  categories to a partner use the following format:
 
357
.. i18n:     partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
 
358
.. i18n:     # Where [3, 2, 1] are id fields of lines in res.partner.category
 
359
.. i18n: 
 
360
.. i18n:     # SEARCH PARTNERS
 
361
.. i18n:     args = [('vat', '=', 'ZZZZZ'),]
 
362
.. i18n:     ids = sock.execute(dbname, uid, pwd, model, 'search', args)
 
363
.. i18n: 
 
364
.. i18n:     # READ PARTNER DATA
 
365
.. i18n:     fields = ['name', 'active', 'vat', 'ref']
 
366
.. i18n:     results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
 
367
.. i18n:     print results
 
368
.. i18n: 
 
369
.. i18n:     # EDIT PARTNER DATA
 
370
.. i18n:     values = {'vat':'ZZ1ZZ'}
 
371
.. i18n:     results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
 
372
.. i18n: 
 
373
.. i18n:     # DELETE PARTNER DATA
 
374
.. i18n:     results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
 
375
..
 
376
 
 
377
.. code-block:: python
 
378
 
 
379
    """
 
380
    :The login function is under
 
381
    ::    http://localhost:8069/xmlrpc/common
 
382
    :For object retrieval use:
 
383
    ::    http://localhost:8069/xmlrpc/object
 
384
    """
 
385
    import xmlrpclib
 
386
 
 
387
    user = 'admin'
 
388
    pwd = 'admin'
 
389
    dbname = 'terp3'
 
390
    model = 'res.partner'
 
391
 
 
392
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
 
393
    uid = sock.login(dbname ,user ,pwd)
 
394
 
 
395
    sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
 
396
 
 
397
    # CREATE A PARTNER
 
398
    partner_data = {'name':'Tiny', 'active':True, 'vat':'ZZZZZ'}
 
399
    partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
 
400
 
 
401
    # The relation between res.partner and res.partner.category is of type many2many
 
402
    # To add  categories to a partner use the following format:
 
403
    partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
 
404
    # Where [3, 2, 1] are id fields of lines in res.partner.category
 
405
 
 
406
    # SEARCH PARTNERS
 
407
    args = [('vat', '=', 'ZZZZZ'),]
 
408
    ids = sock.execute(dbname, uid, pwd, model, 'search', args)
 
409
 
 
410
    # READ PARTNER DATA
 
411
    fields = ['name', 'active', 'vat', 'ref']
 
412
    results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
 
413
    print results
 
414
 
 
415
    # EDIT PARTNER DATA
 
416
    values = {'vat':'ZZ1ZZ'}
 
417
    results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
 
418
 
 
419
    # DELETE PARTNER DATA
 
420
    results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
 
421
 
 
422
.. i18n: :PRINT example:
 
423
..
 
424
 
 
425
:PRINT example:
 
426
 
 
427
.. i18n:    1. PRINT INVOICE
 
428
.. i18n:    2. IDS is the invoice ID, as returned by:
 
429
.. i18n:    3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
 
430
..
 
431
 
 
432
   1. PRINT INVOICE
 
433
   2. IDS is the invoice ID, as returned by:
 
434
   3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
 
435
 
 
436
.. i18n: .. code-block:: python
 
437
.. i18n: 
 
438
.. i18n:     import time
 
439
.. i18n:     import base64
 
440
.. i18n:     printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
 
441
.. i18n:     model = 'account.invoice'
 
442
.. i18n:     id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
 
443
.. i18n:     time.sleep(5)
 
444
.. i18n:     state = False
 
445
.. i18n:     attempt = 0
 
446
.. i18n:     while not state:
 
447
.. i18n:         report = printsock.report_get(dbname, uid, pwd, id_report)
 
448
.. i18n:         state = report['state']
 
449
.. i18n:         if not state:
 
450
.. i18n:             time.sleep(1)
 
451
.. i18n:             attempt += 1
 
452
.. i18n:         if attempt>200:
 
453
.. i18n:             print 'Printing aborted, too long delay !'
 
454
.. i18n: 
 
455
.. i18n:         string_pdf = base64.decodestring(report['result'])
 
456
.. i18n:         file_pdf = open('/tmp/file.pdf','w')
 
457
.. i18n:         file_pdf.write(string_pdf)
 
458
.. i18n:         file_pdf.close()
 
459
..
 
460
 
 
461
.. code-block:: python
 
462
 
 
463
    import time
 
464
    import base64
 
465
    printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
 
466
    model = 'account.invoice'
 
467
    id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
 
468
    time.sleep(5)
 
469
    state = False
 
470
    attempt = 0
 
471
    while not state:
 
472
        report = printsock.report_get(dbname, uid, pwd, id_report)
 
473
        state = report['state']
 
474
        if not state:
 
475
            time.sleep(1)
 
476
            attempt += 1
 
477
        if attempt>200:
 
478
            print 'Printing aborted, too long delay !'
 
479
 
 
480
        string_pdf = base64.decodestring(report['result'])
 
481
        file_pdf = open('/tmp/file.pdf','w')
 
482
        file_pdf.write(string_pdf)
 
483
        file_pdf.close()