Odoo RPC Client¶
A high-level client for interacting with Odoo’s XML-RPC/JSON-RPC API.
Configuration¶
The OdooRPClient can be configured using environment variables for convenience. All parameters are optional and can be overridden when initializing the client.
Variable |
Type |
Default |
Description |
---|---|---|---|
ODOO_HOST |
string |
None |
Odoo server hostname or IP address |
ODOO_PORT |
int |
8069 |
Odoo server port number |
ODOO_PROTOCOL |
string |
jsonrpc |
Protocol to use (jsonrpc or xmlrpc) |
ODOO_DB |
string |
None |
Database name to connect to |
ODOO_USER |
string |
None |
Username for authentication |
ODOO_PASSWORD |
string |
None |
Password for authentication |
Example .env
file:
ODOO_HOST=localhost
ODOO_PORT=8069
ODOO_PROTOCOL=jsonrpc
ODOO_DB=my_database
ODOO_USER=admin
ODOO_PASSWORD=admin_password
When using environment variables, you can initialize the client without any parameters:
from prs_commons.odoo.rpc_client import OdooRPClient
# Uses environment variables for configuration
client = OdooRPClient()
# You can still override specific parameters
client = OdooRPClient(
host='custom-host', # Overrides ODOO_HOST
database='other_db' # Overrides ODOO_DB
)
API Reference¶
- class OdooRPClient(host=None, database=None, username=None, password=None, protocol='jsonrpc', port=8069, **kwargs)¶
Initialize the Odoo RPC client.
- Parameters:
host (str) – Odoo server hostname or IP (default: from environment)
database (str) – Database name (default: from environment)
username (str) – Login username (default: from environment)
password (str) – Login password (default: from environment)
protocol (str) – Protocol to use (‘jsonrpc’ or ‘xmlrpc’)
port (int) – Port number (default: 8069)
kwargs – Additional connection parameters
Note
This is a singleton class. Multiple instantiations will return the same instance.
- ensure_connection()¶
Ensure connection to the Odoo server is established.
- Raises:
ConnectionError – If connection cannot be established
ValueError – If required credentials are missing
- search_read(model, domain=None, fields=None, offset=0, limit=None, order=None, **kwargs)¶
Search and read records matching the criteria.
- Parameters:
- Returns:
List of dictionaries containing record data
- create_record(model, values, **kwargs)¶
Create a new record in the specified model.
- write_record(model, ids, values, **kwargs)¶
Update existing record(s).
Example Usage¶
from prs_commons.odoo.rpc_client import OdooRPClient
# Initialize the client (singleton pattern)
client = OdooRPClient()
# Ensure connection
client.ensure_connection()
# Search for partner records
domain = [('is_company', '=', True)]
fields = ['id', 'name', 'email']
partners = client.search_read('res.partner', domain, fields=fields)
# Create a new partner
new_id = client.create_record('res.partner', {
'name': 'Acme Inc.',
'is_company': True,
'email': 'info@acme.com'
})
# Update the partner
client.write_record('res.partner', [new_id], {
'email': 'contact@acme.com'
})
# Execute a method
result = client.execute_method('res.partner', 'search_read', domain, fields=fields)
# Close the connection
client.close_connection()
Error Handling¶
The client raises appropriate exceptions for different error conditions:
ConnectionError
: When unable to connect to Odoo serverValueError
: For invalid input parametersException
: For other Odoo-specific errors