OCA Days 2023 – Learn how to build and deploy FastAPI services in Odoo with Zina Rasoamanana

Posted by


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:

  1. Basic knowledge of Python programming.
  2. Familiarity with Odoo development and customization.
  3. A working instance of Odoo installed on your local machine or a server.

Step 1: Setting up FastAPI

  1. Start by creating a new Python virtual environment for your FastAPI project. You can do this using the following command:
python3 -m venv myenv
  1. Activate the virtual environment by running the activate script in the Scripts folder (on Windows) or bin folder (on Unix-based systems):
source myenv/bin/activate
  1. Install FastAPI and other required packages by running the following commands:
pip install fastapi uvicorn

Step 2: Creating a FastAPI service

  1. Create a new Python file for your FastAPI service, for example app.py.

  2. Import the necessary modules:
from fastapi import FastAPI
app = FastAPI()
  1. Define a simple endpoint to test your FastAPI service:
@app.get("/")
async def read_root():
    return {"message": "Hello, world!"}
  1. 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

  1. Create a new Odoo module for your FastAPI service, with the necessary files and configurations.

  2. 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()
  1. Configure the Odoo module’s manifest file to include your FastAPI service.

  2. Install the Odoo module and restart the Odoo server to apply the changes.

Step 4: Deploying your FastAPI service

  1. 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.

  2. Install and configure Gunicorn or Uvicorn on your server, and set up a systemd service to manage the FastAPI service.

  3. Create a Nginx configuration file to proxy requests to your FastAPI service.

  4. Start the Gunicorn or Uvicorn service and Nginx server to deploy your FastAPI service.

Step 5: Testing your FastAPI service

  1. Use tools like Postman or curl to send requests to your FastAPI service and test its functionality.

  2. 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.

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x