api books

This commit is contained in:
XuanHuyen
2025-03-08 10:00:27 +07:00
parent f340de4a96
commit a9f51b5647
7 changed files with 73 additions and 37 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-
from . import controllers
from . import library_api
-22
View File
@@ -1,22 +0,0 @@
# -*- coding: utf-8 -*-
# from odoo import http
# class Library(http.Controller):
# @http.route('/library/library', auth='public')
# def index(self, **kw):
# return "Hello, world"
# @http.route('/library/library/objects', auth='public')
# def list(self, **kw):
# return http.request.render('library.listing', {
# 'root': '/library/library',
# 'objects': http.request.env['library.library'].search([]),
# })
# @http.route('/library/library/objects/<model("library.library"):obj>', auth='public')
# def object(self, obj, **kw):
# return http.request.render('library.object', {
# 'object': obj
# })
+31
View File
@@ -0,0 +1,31 @@
from odoo import http
from odoo.http import request
import json
class LibraryAPI(http.Controller):
@http.route('/library/books', type='http', auth='none', methods=['GET'])
def get_books(self, **kwargs):
token = kwargs.get('token')
valid_token = 'token_123'
if not token or token != valid_token:
return {
'error': 'Unauthorized',
"message": "Invalid API token"
}, 401
books = request.env['library.book'].sudo().search([])
book_list = []
for book in books:
book_list.append({
'id': book.id,
'name': book.name,
'author': book.author,
'published_date': str(book.published_date) if book.published_date else None,
'isbn': book.isbn,
'price': book.price,
'state': book.state,
})
return json.dumps({'books': book_list}, ensure_ascii=False)