forked from nicolas-van/openerp-client-lib
-
Notifications
You must be signed in to change notification settings - Fork 47
Support the new odoo 18.4 /json/2/ route
#5
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ odoolib/__pycache__ | |
| MANIFEST | ||
| build | ||
| dist | ||
| odoo_client_lib.egg-info | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Copyright (C) Stephane Wirtel | ||
| Copyright (C) 2011 Nicolas Vanhoren | ||
| Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>) | ||
| Copyright (C) 2018 Nicolas Seinlet | ||
| Copyright (C) 2018 Odoo s.a. (<http://odoo.com>) | ||
|
|
||
| Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
|
||
| 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
|
|
||
| 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
|
||
| 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1 @@ | ||
| # -*- coding: utf-8 -*- | ||
| ############################################################################## | ||
| # | ||
| # Copyright (C) Stephane Wirtel | ||
| # Copyright (C) 2011 Nicolas Vanhoren | ||
| # Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>) | ||
| # Copyright (C) 2018 Odoo s.a. (<http://odoo.com>). | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # 1. Redistributions of source code must retain the above copyright notice, this | ||
| # list of conditions and the following disclaimer. | ||
| # 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
| # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| # | ||
| ############################################################################## | ||
|
|
||
| from .main import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import logging | ||
| import httpx | ||
|
|
||
| from http import HTTPStatus | ||
|
|
||
| from .tools import AuthenticationError, RemoteModel, _getChildLogger | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
| DEFAULT_TIMEOUT = 60 | ||
|
|
||
|
|
||
| class JsonModel(RemoteModel): | ||
| def __init__(self, connection, model_name): | ||
| res = super().__init__(connection, model_name) | ||
| self.__logger = _getChildLogger(_getChildLogger(_logger, 'object'), model_name or "") | ||
| self.methods = {} | ||
| self.model_methods = [] | ||
| return res | ||
|
|
||
| def __getattr__(self, method): | ||
| """ | ||
| Provides proxy methods that will forward calls to the model on the remote Odoo server. | ||
|
|
||
| :param method: The method for the linked model (search, read, write, unlink, create, ...) | ||
| """ | ||
| def proxy(*args, **kwargs): | ||
| """ | ||
| :param args: A list of values for the method | ||
| """ | ||
| # self.__logger.debug(args) | ||
| data = kwargs | ||
| if args: | ||
| # Should convert args list into dict of args | ||
| self._introspect() | ||
| offset = 0 | ||
| if method not in self.model_methods and 'ids' not in kwargs.keys(): | ||
| data['ids'] = args[0] | ||
| offset = 1 | ||
| for i in range(offset, len(args)): | ||
| if i-offset < len(self.methods[method]): | ||
| data[self.methods[method][i-offset]] = args[i] | ||
| else: | ||
| _logger.warning(f"Method {method} called with too many arguments: {args}") | ||
|
|
||
| result = httpx.post( | ||
| self._url(method), | ||
| headers=self.connection.bearer_header, | ||
| json=data, | ||
| timeout=DEFAULT_TIMEOUT, | ||
| ) | ||
|
|
||
| if result.status_code == HTTPStatus.UNAUTHORIZED: | ||
| raise AuthenticationError("Authentication failed. Please check your API key.") | ||
| if result.status_code == 422: | ||
| raise ValueError(f"Invalid request: {result.text} for data {data}") | ||
| if result.status_code != 200: | ||
| raise ValueError(f"Unexpected status code {result.status_code}: {result.text}") | ||
| return result.json() | ||
| return proxy | ||
|
|
||
| def _introspect(self): | ||
| if not self.methods: | ||
| url = f"{self.connection.connector.url.removesuffix('/json/2/')}/doc-bearer/{self.model_name}.json" | ||
| response = httpx.get(url, headers=self.connection.bearer_header) | ||
| response.raise_for_status() | ||
| m = response.json().get('methods', {}) | ||
| self.methods = {k: tuple(m[k]['parameters'].keys()) for k in m.keys()} | ||
| self.model_methods = [ k for k in m.keys() if 'model' in m[k].get('api', []) ] | ||
|
|
||
| def read(self, *args, **kwargs): | ||
| res = self.__getattr__('read')(*args, **kwargs) | ||
| if len(res) == 1: | ||
nseinlet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return res[0] | ||
| return res | ||
|
|
||
| def _url(self, method): | ||
| """ | ||
| Returns the URL of the Odoo server. | ||
| """ | ||
| return f"{self.connection.connector.url}{self.model_name}/{method}" | ||
|
|
||
|
|
||
| class Json2Connector(object): | ||
| def __init__(self, hostname, port="8069"): | ||
| """ | ||
| Initialize by specifying the hostname and the port. | ||
| :param hostname: The hostname of the computer holding the instance of Odoo. | ||
| :param port: The port used by the Odoo instance for JsonRPC (default to 8069). | ||
| """ | ||
| if port != 80: | ||
| self.url = f'http://{hostname}:{port}/json/2/' | ||
| else: | ||
| self.url = f'http://{hostname}/json/2/' | ||
|
|
||
|
|
||
| class Json2SConnector(Json2Connector): | ||
| def __init__(self, hostname, port="443"): | ||
| super().__init__(hostname, port) | ||
| if port != 443: | ||
| self.url = f'https://{hostname}:{port}/json/2/' | ||
| else: | ||
| self.url = f'https://{hostname}/json/2/' | ||
|
|
||
|
|
||
| class Json2Connection(object): | ||
| """ | ||
| A class representing a connection to an Odoo server. | ||
| """ | ||
|
|
||
| def __init__(self, connector, database, api_key): | ||
| self.connector = connector | ||
| self.database = database | ||
| self.bearer_header = {"Authorization": f"Bearer {api_key}", 'Content-Type': 'application/json; charset=utf-8', "X-Odoo-Database": database} | ||
| self.user_context = None | ||
|
|
||
| def get_model(self, model_name): | ||
| return JsonModel(self, model_name) | ||
|
|
||
| def get_connector(self): | ||
| return self.connector | ||
|
|
||
| def get_user_context(self): | ||
| """ | ||
| Query the default context of the user. | ||
| """ | ||
| if not self.user_context: | ||
| self.user_context = self.get_model('res.users').context_get() | ||
| return self.user_context | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.