OCA Days 2023 is an annual event organized by the Odoo Community Association, where developers and contributors come together to discuss and work on various aspects of the Odoo platform. This year, one of the workshops at OCA Days 2023 will be conducted by Zina Rasoamanana, who will be guiding participants on how to create and deploy FastAPI services within Odoo.
FastAPI is a modern web framework for building APIs with Python, known for its high performance and ease of use. By combining FastAPI with Odoo, developers can create powerful and efficient web services that integrate seamlessly with their Odoo projects. In this tutorial, we will walk through the process of setting up a FastAPI service within Odoo, deploying it, and testing its functionality.
Prerequisites:
- Basic knowledge of Python programming.
- Familiarity with Odoo development and customization.
- A working instance of Odoo installed on your local machine or a server.
Step 1: Setting up FastAPI
- Start by creating a new Python virtual environment for your FastAPI project. You can do this using the following command:
python3 -m venv myenv
- Activate the virtual environment by running the activate script in the
Scripts
folder (on Windows) orbin
folder (on Unix-based systems):
source myenv/bin/activate
- Install FastAPI and other required packages by running the following commands:
pip install fastapi uvicorn
Step 2: Creating a FastAPI service
-
Create a new Python file for your FastAPI service, for example
app.py
. - Import the necessary modules:
from fastapi import FastAPI
app = FastAPI()
- Define a simple endpoint to test your FastAPI service:
@app.get("/")
async def read_root():
return {"message": "Hello, world!"}
- Run your FastAPI service using Uvicorn:
uvicorn app:app --reload
Visit http://localhost:8000
in your web browser to see the message "Hello, world!" displayed.
Step 3: Integrating FastAPI with Odoo
-
Create a new Odoo module for your FastAPI service, with the necessary files and configurations.
- In the Odoo module’s controller file, define a new route that will call your FastAPI service. For example:
from odoo import http
from fastapi import FastAPI
app = FastAPI()
class FastAPIController(http.Controller):
@http.route('/fastapi', type='http', auth='public', website=True)
def call_fastapi(self, **kw):
response = requests.get('http://localhost:8000/')
return response.json()
-
Configure the Odoo module’s manifest file to include your FastAPI service.
- Install the Odoo module and restart the Odoo server to apply the changes.
Step 4: Deploying your FastAPI service
-
To deploy your FastAPI service to a production environment, you can use a WSGI server like Gunicorn or ASGI server like Uvicorn with a reverse proxy server like Nginx.
-
Install and configure Gunicorn or Uvicorn on your server, and set up a systemd service to manage the FastAPI service.
-
Create a Nginx configuration file to proxy requests to your FastAPI service.
- Start the Gunicorn or Uvicorn service and Nginx server to deploy your FastAPI service.
Step 5: Testing your FastAPI service
-
Use tools like Postman or curl to send requests to your FastAPI service and test its functionality.
- Confirm that your FastAPI service is working as expected and providing the desired responses.
By following this tutorial, you will be able to create and deploy FastAPI services within Odoo, enhancing the functionality of your Odoo projects with high-performance web services. Attend Zina Rasoamanana’s workshop at OCA Days 2023 to learn more about this exciting integration and to further explore the possibilities of using FastAPI with Odoo.