Skip to content
Closed
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.

.. 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

Choose a reason for hiding this comment

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

Please add a usage section on how to use this module

===========

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 <dave@laslabs.com>

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',
'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
Copy link

Choose a reason for hiding this comment

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

AttributeError: 'JsonRequest' object has no attribute 'httprequest'

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)
55 changes: 55 additions & 0 deletions base_web_hook/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.. 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 Web Hook
=============

This module provides an abstract core for receiving and processing web hooks.

.. 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

Known Issues
============

* Security is too lax; public can read too much. Maybe should also add a group for hooks..
* Buffer length should be checked before ``httprequest.get_data`` calls

Bug Tracker

Choose a reason for hiding this comment

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

Please add a usage section on how to use this module

===========

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 <dave@laslabs.com>

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.
5 changes: 5 additions & 0 deletions base_web_hook/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

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

{
'name': 'Base Web Hook',
'summary': 'Provides an abstract system for defining and receiving web '
'hooks.',
'version': '10.0.1.0.0',
'category': 'Tools',
'website': 'https://laslabs.com/',
'author': 'LasLabs, Odoo Community Association (OCA)',
'license': 'LGPL-3',
'installable': True,
'external_dependencies': {
'python': ['slugify'],
},
'depends': [
'base_json_request',
],
'data': [
'security/ir.model.access.csv',
'views/web_hook_view.xml',
],
}
4 changes: 4 additions & 0 deletions base_web_hook/controllers/__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 . import main
46 changes: 46 additions & 0 deletions base_web_hook/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import json

from odoo import http


class WebHookController(http.Controller):

@http.route(
['/base_web_hook/json/<string:slug>.json'],
type='json',
auth='public',
)
def json_receive(self, *args, **kwargs):
return self._receive(*args, **kwargs)

@http.route(
['/base_web_hook/<string:slug>'],
type='http',
auth='public',
csrf=False,
)
def http_receive(self, *args, **kwargs):
return json.dumps(
self._receive(*args, **kwargs),
)

@http.route(
['/base_web_hook/authenticated/<string:slug>'],
type='http',
auth='user',
csrf=False,
)
def http_receive_authenticated(self, *args, **kwargs):
return self.http_receive(*args, **kwargs)

def _receive(self, slug, **kwargs):
hook = http.request.env['web.hook'].search_by_slug(slug)
return hook.receive(
data=kwargs,
data_string=http.request.httprequest.get_data(),
headers=http.request.httprequest.headers,
)
19 changes: 19 additions & 0 deletions base_web_hook/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

# Concrete models
from . import web_hook
from . import web_hook_token

# Adapters
from . import web_hook_adapter
from . import web_hook_token_adapter

# Token Interfaces
from . import web_hook_token_none
from . import web_hook_token_plain
from . import web_hook_token_user

# Request Bin Hook
from . import web_hook_request_bin
from . import web_hook_request_bin_request
Loading