Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] base_json_request: Module to allow standard JSON requests #3

Open
wants to merge 1 commit into
base: 10.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions base_json_request/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.. image:: https://img.shields.io/badge/licence-LGPL--3-blue.svg
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3

=================
Base JSON Request
=================

This module allows you to receive JSON requests in Odoo that are not
RPC.

Copy link
Author

@lasley lasley Oct 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add note that route needs to be defined with type=json

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/210/10.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/server-tools/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Dave Lasley <[email protected]>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
4 changes: 4 additions & 0 deletions base_json_request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from .hooks import post_load
18 changes: 18 additions & 0 deletions base_json_request/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

{
'name': 'Base JSON Request',
'summary': 'Allows you to receive JSON requests that are not RPC.',
'version': '10.0.1.0.0',
'category': 'Authentication',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not

'website': 'https://laslabs.com/',
'author': 'LasLabs, Odoo Community Association (OCA)',
'license': 'LGPL-3',
'installable': True,
'depends': [
'web',
],
'post_load': 'post_load',
}
13 changes: 13 additions & 0 deletions base_json_request/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import http

from .http import _handle_exception, __init__


def post_load():
"""Monkey patch HTTP methods."""
http.JsonRequest._handle_exception = _handle_exception
http.JsonRequest.__init__ = __init__
60 changes: 60 additions & 0 deletions base_json_request/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import json

from werkzeug.exceptions import BadRequest

from odoo import http


old_handle_exception = http.JsonRequest._handle_exception
old_init = http.JsonRequest.__init__


def __init__(self, *args):
try:
old_init(self, *args)
except BadRequest as e:
try:
args = self.httprequest.args
self.jsonrequest = args
self.params = json.loads(self.jsonrequest.get('params', "{}"))
self.context = self.params.pop('context',
dict(self.session.context))
except ValueError:
raise e


def _handle_exception(self, exception):
""" Override the original method to handle Werkzeug exceptions.

Args:
exception (Exception): Exception object that is being thrown.

Returns:
BaseResponse: JSON Response.
"""

# For some reason a try/except here still raised...
code = getattr(exception, 'code', None)
if code is None:
return old_handle_exception(
self, exception,
)

error = {
'data': http.serialize_exception(exception),
'code': code,
}

try:
error['message'] = exception.description
except AttributeError:
try:
error['message'] = exception.message
except AttributeError:
error['message'] = 'Internal Server Error'

return self._json_response(error=error)