Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Odoo 15 Development Essentials - Fifth Edition

You're reading from  Odoo 15 Development Essentials - Fifth Edition

Product type Book
Published in Feb 2022
Publisher Packt
ISBN-13 9781800200067
Pages 548 pages
Edition 5th Edition
Languages
Author (1):
Daniel Reis Daniel Reis
Profile icon Daniel Reis

Table of Contents (22) Chapters

Preface 1. Section 1: Introduction to Odoo Development
2. Chapter 1: Quick Start Using the Developer Mode 3. Chapter 2: Preparing the Development Environment 4. Chapter 3: Your First Odoo Application 5. Chapter 4: Extending Modules 6. Section 2: Models
7. Chapter 5: Importing, Exporting, and Module Data 8. Chapter 6: Models – Structuring the Application Data 9. Section 3: Business Logic
10. Chapter 7: Recordsets – Working with Model Data 11. Chapter 8: Business Logic – Supporting Business Processes 12. Chapter 9: External API – Integrating with Other Systems 13. Section 4: Views
14. Chapter 10: Backend Views – Designing the User Interface 15. Chapter 11: Kanban Views and Client-Side QWeb 16. Chapter 12: Creating Printable PDF Reports with Server-Side QWeb 17. Chapter 13: Creating Web and Portal Frontend Features 18. Section 5: Deployment and Maintenance
19. Chapter 14: Understanding Odoo Built-In Models 20. Chapter 15: Deploying and Maintaining Production Instances 21. Other Books You May Enjoy

Chapter 9: External API – Integrating with Other Systems

The Odoo server provides an external API that's used by its web client and is also available for other client applications. In this chapter, we'll learn how to use the Odoo external API to implement external applications that interact with an Odoo server by using it as a backend.

This can be used to write scripts to load or modify Odoo data, or to integrate with an Odoo existing business application, which is complementary and can't be replaced by an Odoo app.

We'll describe how to use OdooRPC calls, and then use that knowledge to build a simple command-line application for the Library Odoo app using Python.

The following topics will be covered in this chapter:

  • Introducing the learning project – a client app to catalog books
  • Setting up Python on the client machine
  • Exploring the Odoo external API
  • Implementing the client app's XML-RPC interface
  • Implementing...

Technical requirements

The code in this chapter requires the library_app Odoo module that we created in Chapter 3, Your First Odoo Application. The corresponding code can be found in this book's GitHub repository at https://github.com/PacktPublishing/Odoo-15-Development-Essentials.

The path to the Git clone repository should be in the Odoo add-ons path and the library_app module should be installed. The code examples will assume that the Odoo database you're working with is library, to be consistent with the installation instructions provided in Chapter 2, Preparing the Development Environment.

The code in this chapter can be found in the same repository, in the ch09/client_app/ directory.

Introducing the learning project – a client app to catalog books

In this chapter, we will work on a simple client application to manage the library book catalog. It is a command-line interface (CLI) application that uses Odoo as its backend. The features that we will implement will be basic to keep the focus on the technology that's used to interact with the Odoo server.

This simple CLI application should be able to do the following:

  • Search for and list books by title.
  • Add new books to the catalog.
  • Edit a book title.

The goal is to focus on how to use the Odoo external API, so we want to avoid introducing additional programming languages that you might not be familiar with. By introducing this constraint, the most sensible choice is to use Python to implement the client app. Still, once we understand the XML-RPC library for a particular language, the techniques to handle the RPC calls will also apply.

The application will be a Python script...

Setting up Python on the client machine

The Odoo API can be accessed externally using two different protocols: XML-RPC and JSON-RPC. Any external program capable of implementing a client for one of these protocols will be able to interact with an Odoo server. To avoid introducing additional programming languages, we will use Python to explore the external API.

Until now, Python code was only being used on the server side. For the client app, Python code will run on the client, so the workstation may require additional setup.

To follow the examples in this chapter, the system you're using needs to be able to run Python 3 code. If you've followed the same development environment that's been used for the other chapters in this book, this might already be the case. However, if it isn't, we should make sure that Python is installed.

To make sure that Python 3 is installed in the development workstation, run the python3 --version command in a terminal window...

Exploring the Odoo external API

Some familiarity with the Odoo external API should be gained before we implement the client app. The following sections explore the XML-RPC API using a Python interpreter.

Using XML-RPC to connect to the Odoo external API

The simplest way to access the Odoo server is by using XML-RPC. The xmlrpc library, from Python standard library, can be used for this.

Remember that the application being developed is a client that connects to a server. So, a running Odoo server instance is needed for the client to connect to. The code examples will assume that an Odoo server instance is running on the same machine, http://localhost:8069, but any reachable URL can be used if the server you wish to use is running on a different machine.

The Odoo xmlrpc/2/common endpoint exposes public methods, and these can be accessed without a login. These can be used to inspect the server version and check login credentials. Let's use the xmlrpc library to explore...

Implementing the client app XML-RPC interface

Let's start by implementing the Library book catalog client application.

This can be split into two files: one for the Odoo backend interface containing the server backend, library_xmlrpc.py, and another for the user interface, library.py. This will allow us to use alternative implementations for the backend interface.

Starting with the Odoo backend component, a LibraryAPI class will be used to set up the connection with the Odoo server that supports methods that are needed to interact with Odoo. The methods to implement are as follows:

  • search_read(<title>) to search for book data by title
  • create(<title>) to create a book with a specific title
  • write(<id>, <title>) to update a book title using the book ID
  • unlink(<id>) to delete a book using its ID

Choose a directory to host the application files in and create the library_xmlrpc.py file. Start by adding the class constructor...

Implementing the client app user interface

Our goal here was to learn how to write the interface between an external application and the Odoo server, and we did this in the previous section. But let's go the extra mile and build the user interface for this minimalistic client application.

To keep this as simple as possible, we will use a simple command-line user interface and additional dependencies will be avoided. This leaves us with Python's built-in features to implement command-line applications and the ArgumentParser library.

Now, alongside the library_xmlrpc.py file, create a new library.py file. This will import Python's command-line argument parser and then the LibraryAPI class, as shown in the following code:

from argparse import ArgumentParser
from library_xmlrpc import LibraryAPI

Next, we must describe the commands that the argument parser will expect. There are four commands:

  • list to search for and list books
  • add to add a book
  • ...

Using the OdooRPC library

Another relevant client library to be considered is OdooRPC. It is a complete client library that uses the JSON-RPC protocol instead of XML-RPC. The Odoo official web client uses JSON-RPC as well, although XML-RPC is still also supported.

The OdooRPC library is now maintained under the Odoo Community Association umbrella. The source code repository can be found at https://github.com/OCA/odoorpc.

The OdooRPC library can be installed from PyPI using the following command:

$ pip3 install odoorpc

The OdooRPC library sets up a server connection when a new odoorpc.ODOO object is created. At this point, we should use the ODOO.login() method to create a user session. Just like on the server side, the session has an env attribute containing the session's environment, including the user ID, uid, and context.

The OdooRPC library can be used to provide an alternate implementation for the library_xmlrpc.py interface with the server. It should provide...

Summary

The goal of this chapter was to learn how the external API works and what it is capable of. We started by exploring it with simple scripts using the Python XML-RPC client, though the external API can be used from any programming language. The official documentation provides code examples for Java, PHP, and Ruby.

Then, we learned how to use XML-RPC calls to search for and read data, and then how to call any other method. We can, for example, create, update, and delete records.

Next, we introduced the OdooRPC library. It provides a layer on top of the RPC base library (XML-RPC or JSON-RPC) to provide a local API that's similar to the API that can be found on the server side. This lowers the learning curve, reduces programming mistakes, and makes it easier to copy code between server and client code.

With this, we have finished the chapters dedicated to the programming API and business logic. Now, it's time to look at views and the user interface. In the next...

Further reading

The following additional reference material may complement the topics described in this chapter:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Odoo 15 Development Essentials - Fifth Edition
Published in: Feb 2022 Publisher: Packt ISBN-13: 9781800200067
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}