There's more...

In our example, we are handling the request of only one book's data, and capturing the single credit is simple; but things get complicated with multiple credits. A complex pricing structure can introduce a few corner cases. Let's look at this issue through the following example. Suppose that we want to handle multiple book requests. In this case, a customer has requested the data of 10 books, but we only have the data of five books. Here, if we complete the with block without encountering any errors, the charge() will capture 10 credits, which is incorrect, because we only have the data for a certain amount of books. Furthermore, if we raise the exception, then it will release all 10 credits and show the customer that the book info is not found. To fix this issue, Odoo provides the object of the transaction in the with block. In some cases, the services can not fully served. Example for, the user asked for data of 10 books but you only have data for 5 books. In such cases, you can change the actual credit amount on the go and capture partial credits. See the following example for a further explanation:

...
isbn_list = [<assume list of 10 isbn number>]
credits_to_reserve = len(isbn_list)
data_found = []
with iap.charge(request.env, service_key, account_token, credits_to_reserve) as transection:
for isbn in isbn_list:
data = request.env['books.info']._books_data_by_isbn(isbn)
if data['status'] == 'found':
data_found.appned(data)
transection.credit = len(data_found)
return data_found

In the preceding code block, we have updated the value of the credit to capture on the fly, according to transection.credit; this is how we can only charge credit for the book data that is found.

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

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