How to do it...

We will create a new iap_isbn_client module. This module will inherit the my_library module and add a button in the book's form view. Clicking on a button will send a request to our IAP service running on the 8090 port. The IAP service will capture the credit and return the information of the requested book. We will write this information in the book's record. Follow these steps to complete the IAP client module:

  1. Create a new iap_isbn_client module and add __init__.py:
from . import models
  1. Add __manifest__.py, with the given content:
{
'name': "Books ISBN",
'summary': "Get Books Data based on ISBN",
'website': "http://www.example.com",
'category': 'Uncategorized',
'version': '12.0.1',
'depends': ['iap', 'my_library'],
'data': [
'views/library_books_views.xml',
]
}
  1. Add models/library_book.py and add a few fields by inheriting the library.book model:
from odoo import models, fields, api
from odoo.exceptions import UserError
from odoo.addons.iap import jsonrpc


class LibraryBook(models.Model):
_inherit = 'library.book'

cover_image = fields.Binary('Books Cover')
isbn = fields.Char('ISBN')
  1. Add the fetch_book_data() method in the same model. This will be called upon a button click:
def fetch_book_data(self):
self.ensure_one()
if not self.isbn:
raise UserError("Please add ISBN number")

user_token = self.env['iap.account'].get('book_isbn')
params = {
'account_token': user_token.account_token,
'isbn_number': self.isbn
}
service_endpoint = 'http://localhost:8070'
result = jsonrpc(service_endpoint + '/get_book_data', params=params)
if result.get('status') == 'found':
self.write(self.process_result(result['data']))
return True
  1. Add the process_result() method process IAP service response:
@api.model
def process_result(self, result):
authors = []
existing_author_ids = []
for author_name in result['authors']:
author = self.env['res.partner'].search([('name','=',author_name)], limit=1)
if author:
existing_author_ids.append(author.id)
else:
authors.append((0, 0, {'name': author_name}))
if existing_author_ids:
authors.append((6, 0, existing_author_ids))
return {
'author_ids': authors,
'name': result.get('name'),
'isbn': result.get('isbn'),
'cover_image': result.get('cover_image'),
'date_release': result.get('date_release'),
}

  1. Add views/library_books_views.xml, and add a button and fields by inheriting the book's form view:
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="library_book_view_form_inh" model="ir.ui.view">
<field name="name">Library Book Form</field>
<field name="model">library.book</field>
<field name="inherit_id" ref="my_library.library_book_view_form"/>
<field name="arch" type="xml">
<xpath expr="//group" position="before">
<header>
<button name="fetch_book_data" string="Fetch Book Data" type="object"/>
</header>
</xpath>
<field name="date_release" position="after">
<field name="isbn"/>
<field name="cover_image" widget="image" class="oe_avatar"/>
</field>
</field>
</record>

</odoo>

Install the iap_isbn_client module. This will add a Fetch Book Data button to the book form. After doing this, add a valid ISBN number (for example, 1788392019) and click on the button. This will make a request and fetch the data from the service. If you are making the IAP service call for the first time, then your Odoo instance won't have information about the linked account, so Odoo will raise a popup to buy the credits, as follows:

Upon clicking on the Buy credits at Odoo button, you will be redirected to the IAP service page, where you will see the information about the available packs to purchase. For our recipe, you will see the packs that we defined while registering our service in the Registering an IAP service in Odoo recipe of this chapter. Take a look at the following screenshot; it is a list of packs to purchase:

As we are using the sandbox endpoint, you can buy any pack without any payment. After that, you can request the book information from the book's form view.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset