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_LOGIN |
string |
None |
Username for authentication |
ODOO_PASSWORD |
string |
None |
Password for authentication |
Example .env
file:
ODOO_HOST=localhost
ODOO_PORT=8069
ODOO_DB=my_database
ODOO_LOGIN=admin
ODOO_PASSWORD=admin_password
# Optional: ODOO_PROTOCOL=jsonrpc (default: jsonrpc)
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()
API Reference¶
- class OdooRPClient¶
Initialize the Odoo RPC client. This is a singleton class that reads configuration from environment variables. Multiple instantiations will return the same instance.
Note
The client is configured using environment variables as described in the Configuration section above. The connection is automatically established on first use and will be reused across instances. Automatically called on first RPC operation if not already connected.
- raises ConnectionError:
If connection cannot be established
- raises ValueError:
If required credentials are missing
- raises Exception:
For other connection-related errors
- ensure_connection() None ¶
Ensure connection to the Odoo server is established. Automatically called on first RPC operation if not already connected.
- Raises:
ConnectionError – If connection cannot be established
ValueError – If required credentials are missing
Exception – For other connection-related errors
- 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).
- async execute_method(model: str, method: str, ids: List[int], *args, **kwargs) -> Any
Execute a method on the Odoo model asynchronously.
- Example:
# Call a custom method on a record result = await client.execute_method( 'sale.order', 'action_confirm', [order_id] # List of record IDs )
- Parameters:
model – Name of the Odoo model (e.g., ‘sale.order’)
method – Method name to call
ids – List of record IDs to operate on
args – Positional arguments for the method
kwargs – Keyword arguments for the method
- Returns:
Result of the method call
- Raises:
Exception – If the method execution fails
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 (optional, connection will be closed on program exit)
client.close_connection()
Dependencies¶
This client requires the odoorpc package. Install it using:
pip install odoorpc
For development, you may also want to install the test dependencies:
pip install -e ".[test]"
Testing¶
To run the test suite, ensure you have the test dependencies installed and run:
pytest tests/ -v
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