My favorites | Sign in
Logo
                
Search
for
Updated Nov 20, 2009 by cedkr...@gmail.com
Labels: Phase-Support
RemoteCalls  
Various samples of code for remote calls to the tryton server

Remote Calls

Each example shows two remote calls, the first one is the 'search' to get the first 10 party ids. The second one calls name_get on these ids, then outputs the result, which looks like: [(1, u'Supplier A'), (2, u'Supplier B'), (3, u'Customer A'), (4, u'Customer B')]

Note:

- By default trytond only listens on port 8069 for NET-RPC calls. To allow XML-RPC, you must provide a config file (with the -c switch) containing at least (see etc/trytond.conf on your local copy):

# Activate the xml-rpc protocol
xmlrpc = True
# Configure the port for the xml-rpc protocol to listen
xmlport = 8069

- NET-RPC is only available with Python since it uses pickle to serialize data. The data sent over the network are more compact than with XML-RPC, hence speeding up the requests.

Version 1.2, 1.4

XML-RPC in Python

import xmlrpclib

PASSWORD = 'admin'
USER = "admin"

# Get user_id and session
s = xmlrpclib.ServerProxy ('http://localhost:8069/try')
user_id, session = s.common.db.login(USER, PASSWORD)

# Get the user context
context = s.model.res.user.get_preferences(user_id, session, True, {})

# Print all methods (introspection)
print s.system.listMethods()

# Search parties and print rec_name
party_ids = s.model.party.party.search(user_id, session,
                                   [], # search clause
                                   0,  # offset
                                   10, # limit
                                   False, # order
                                   context)  # context

print s.model.party.party.read(user_id, session,
                             party_ids, # party ids
                             ['rec_name'], # list of fields
                             context) # context

# Execute report
type, data, _ = s.report.party.label.execute(user_id, session,
                             party_ids, # party ids
                             {}, # data
                             context) # context

NET-RPC in Python

from trytond import pysocket

PASSWORD = 'admin'
DB = "try"
USER = "admin"

sock = pysocket.PySocket()
sock.connect('127.0.0.1', '8070')

# Get user_id and session
sock.send((DB, USER, PASSWORD, 'common', 'db', 'login'))
user_id, session = sock.receive()

# Get the user context
sock.send((DB, user_id, session, 'model', 'res.user', 'get_preferences',
               True, {}))
context = sock.receive()

# Search parties and print rec_name
sock.send((DB, user_id, session, 'model', 'party.party', 'search',
               [],  # search clause
               0,   # offset
               10,  # limit
               False, # order
               {}))  # context

party_ids = sock.receive()

sock.send((DB, user_id, session, 'model', 'party.party', 'read',
               party_ids, # party ids
               ['rec_name'], # list of fields
               context)) #context
print sock.receive()

# Execute report
sock.send((DB, user_id, session, 'report', 'party.label', 'execute',
               party_ids, # party ids
               {}, # data
               context)) # context
type, data, _ = sock.receive()

Version 1.0

XML-RPC in Python

import xmlrpclib

PASSWORD = 'admin'
DB = "try"
USER = "admin"

# Get user_id and session
common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
user_id, session = common.login(DB, USER, PASSWORD)

object = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')

party_ids = object.execute(DB, user_id, session, 'party.party', 'search',
                           [], # search clause
                           0,  # offset
                           10, # limit
                           {})  # context

print object.execute(DB, user_id, session, 'party.party', 'name_get',
                     party_ids)

XML-RPC in Ruby

require 'xmlrpc/client'
require 'pp'

PASSWORD = 'admin'
DB = "try"
USER = "admin"

# Get user_id and session
common = XMLRPC::Client.new2("http://localhost:8069/xmlrpc/common")
user_id, session = common.call('login', DB, USER, PASSWORD)

object = XMLRPC::Client.new2("http://localhost:8069/xmlrpc/object")

party_ids = object.call('execute', DB, user_id, session, 'party.party', 'search',
                        [],  # search clause
                        0,   # offset
                        10,  # limit
                        {})  # context
pp object.call('execute', DB, user_id, session,  'party.party', 'name_get',
               party_ids)

NET-RPC in Python

from trytond import pysocket

PASSWORD = 'admin'
DB = "try"
USER = "admin"

sock = pysocket.PySocket()
sock.connect('127.0.0.1', '8070')

# Get user_id and session
sock.send(('common', 'login', DB, USER, PASSWORD))
user_id, session = sock.receive()

sock.send(('object', 'execute', DB, user_id, session, 'party.party', 'search',
           [],  # search clause
           0,   # offset
           10,  # limit
           {}))  # context

party_ids = sock.receive()

sock.send(('object', 'execute', DB, user_id, session, 'party.party', 'name_get',
           party_ids))
print sock.receive()

Sign in to add a comment
Hosted by Google Code