Main Application Module (main.py)
Relevant source files
The following files were used as context for generating this wiki page:
The app/main.py file serves as the entry point for the OdooConnectorAPI, a RESTful interface built with the FastAPI framework. Its primary purpose is to establish a bridge between external HTTP clients and an Odoo ERP instance by utilizing the odoorpc library for Remote Procedure Calls.
Sources: app/main.py:1-7, README.md:3-6
The module handles the initialization of the FastAPI application, defines the core API endpoints, and manages the connection lifecycle—including login and logout—to the target Odoo environment. It is designed to be highly configurable through environment variables, facilitating deployment across local, Dockerized, and Kubernetes environments.
Sources: app/main.py:9-14, README.md:43-58, charts/odooconnectorapi/templates/deployment.yaml:15-22
API Architecture and Endpoints
The application follows a standard FastAPI structure where routes are defined using decorators. The current implementation focuses on read operations from Odoo's res.partner model.
Connection Workflow
When an endpoint requiring Odoo data is accessed, the module initiates a connection using the odoorpc.ODOO client. It authenticates using credentials provided at runtime and performs the requested operation before terminating the session.
The flow illustrates the synchronous connection lifecycle within the get_partners endpoint.
Sources: app/main.py:23-40
Defined Endpoints
| Endpoint | Method | Description | Return Type |
|---|---|---|---|
/ | GET | Health check or welcome message. | {"message": "Hello World"} |
/get/partners | GET | Fetches a list of partners (name and email) from Odoo. | {"partners": [...]} |
Sources: app/main.py:17-21, app/main.py:24-41
Configuration and Environment
The module relies on four critical environment variables to establish a connection with the Odoo backend. These variables are retrieved using os.getenv with sensible defaults for local development.
| Variable | Description | Default (app/main.py) |
|---|---|---|
ODOO_URL | The hostname or IP of the Odoo server. | localhost |
ODOO_DB | The name of the Odoo database to connect to. | mydb |
ODOO_USERNAME | The Odoo user account username. | admin |
ODOO_PASSWORD | The Odoo user account password. | admin |
Sources: app/main.py:10-14, charts/odooconnectorapi/values.yaml:16-20, docker-compose.yml:10-14
Infrastructure Integration
The application is structured to receive these configurations from various orchestration layers:
- Docker Compose: Maps environment variables from a
.envfile to the container. - Kubernetes (Helm): Maps values defined in
values.yamlto the container environment via the deployment template.
Sources: docker-compose.yml:9-14, charts/odooconnectorapi/templates/deployment.yaml:14-22
Data Retrieval Logic
The core logic for interacting with Odoo is encapsulated within the get_partners function. It utilizes the odoorpc library to perform a two-step data retrieval process:
- Search:
partner.search([])retrieves the IDs of all records in theres.partnermodel. - Read:
partner.read(ids, ['name', 'email'])fetches specific fields for the discovered IDs.
@app.get("/get/partners")
async def get_partners() -> Dict[str, Any]:
try:
odoo = odoorpc.ODOO(ODOO_URL, port=80)
odoo.login(ODOO_DB, ODOO_USERNAME, ODOO_PASSWORD)
except odoorpc.error.RPCError as err:
return {"error": err}
if odoo.env:
partner = odoo.env['res.partner']
partners = partner.read(partner.search([]), ['name', 'email'])
odoo.logout()
return {"partners": partners}
Sources: app/main.py:24-41, requirements.txt:3
Dependencies
The main.py module depends on several external libraries to function, as defined in the project's dependency manifest:
- fastapi: The web framework used to build the API.
- odoorpc: The communication library for Odoo integration.
- uvicorn: The ASGI server used to run the application (referenced in startup commands).
Sources: requirements.txt:1-7, README.md:41, docker-compose.yml:6
Summary
The app/main.py module acts as the centralized controller for the OdooConnectorAPI. By leveraging FastAPI and odoorpc, it provides a simplified interface for external systems to interact with Odoo data. Its architecture emphasizes portability through environment-based configuration, allowing it to function seamlessly within Docker containers and Kubernetes clusters.