Search 1.9 billion lines of Odoo code on GitHub

base_rest

Author: ACSONE SA/NV, Odoo Community Association (OCA)
License: LGPL-3
Branch: 13.0-fixcrashdesc
Repository: acsone/rest-framework
Dependencies: component, and web
Languages: CSS (2, 0.1%), HTML (716, 22.7%), JavaScript (62, 2.0%), Python (2000, 63.5%), SVG (76, 2.4%), Sass (12, 0.4%), XML (69, 2.2%), and reStructuredText (213, 6.8%)
Other branches: 10.0, 10.0-abi_master, 10.0-add_base_rest, 10.0-backport-context-provider-lmi, 10.0-backport-from-14.0, 10.0-backport-rest-log, 10.0-base_rest_inherit, 10.0-data_model-lmi, 10.0-fix_base_rest_registry, 10.0-fixrestlog-len, 10.0-improve-doc, 10.0-osi_master, 11.0-backport-from-12-13, 11.0-fix_base_rest_registry, 11.0-improve-doc, 12.0, 12.0-asw_master, 12.0-backport-improve-exception-wrap, 12.0-base_rest-cors-options, 12.0-datamodel-experiment, 12.0-fix_base_rest_registry, 12.0-new-api-wip, 13.0, 13.0-48-port, 13.0-backport-base-rest-pydantic, 13.0-backport-extendable, 13.0-base_rest-cors-options, 14.0, 14.0-abi_master, 14.0-add-pydantic, 14.0-base-rest-pydantic-lmi, 14.0-base_res_service_add_properties-qgr, 14.0-deprecated-implicit-rest-method, 14.0-fdl_master, 14.0-fix-path-parameter-name, 14.0-fix-response-processing, 14.0-fixcrashdesc, 14.0-moz_master, 15.0, 15.0-initial, 16.0, 16.0-fastapi, 16.0-mfr_fix_base_rest_missing_argument, 16.0-upgrade-pre-commit, 9.0, and oca-port-pr--from-14.0-to-13.0
Other repositories: 0x130C/odoo-shopinvader, AdriaGForgeFlow/odoo-shopinvader, Ahmad-Datasmith/rest-framework, Albin-John/odoo-shopinvader, Change2improve/rest-framework, Digital5-Odoo/rest-framework, Gabinete-Digital/rest-framework, Laurent-Corron/odoo-shopinvader, LeartS/rest-framework, LevelPrime/rest-framework, NeatNerdPrime/rest-framework, OCA-MUK/rest-framework, OCA/rest-framework, SerpentConsultingServices/odoo-shopinvader, SetRac/rest-framework, SeuMarco/rest-framework, TDu/rest-framework, Tomaskarpovic/rest-framework, acsone/odoo-shopinvader, adrienpeiffer/odoo-shopinvader, ajinvn2019/rest-framework, akretion/odoo-shopinvader, akretion/rest-framework, alecvinent/rest-framework, altanmur/rest-framework, andres-mejia/rest-framework, antoinecln/odoo-shopinvader, ap-wtioit/odoo-shopinvader, baotnp/rest-framework, bigmoney-team/rest-framework, boky1993/rest-framework, cedvict/rest-framework, clementmbr/odoo-shopinvader, coopiteasy/rest-framework, czgonroad/odoo-shopinvader, darshan-odoo/rest-framework, dndoanh/rest-framework, elmosolutions/rest-framework, gaanto/rest-framework, gastonfeng/rest-framework, glm-conseil/rest-framework, grap/odoo-shopinvader, guewen/rest-framework, gurneyalex/rest-framework, guwenfeng/rest-framework, hinfo506/rest-framework, hminle/rest-framework, huntergps/rest-framework, id42sistemas/rest-framework, jacky82669513/rest-framework, jamesvibar/rest-framework, janverb/rest-framework, jobiols/oca-rest-framework, jorgeblanc9/rest-framework, josuf567/rest-framework, jriveros/odoo-shopinvader, jsilvestar/odoo-shopinvader, lebanggit/rest-framework, led-startup-studio/rest-framework, lideritjnma/rest-framework, marvin981973/rest-framework, maxindo/rest-framework, mohamed-helmy/rest-framework, ngnamuit/rest-framework, novawish/rest-framework, odoogap/rest-framework, oliverzgy/rest-framework, one2pret/rest-framework, osvalr/rest-framework, pscloud/rest-framework, qq470647251/odoo-shopinvader, royalline1/rest-framework, sanube/rest-framework, sc4you/odoo-shopinvader, simahawk/rest-framework, skander-cherif/rest-framework, themreza/rest-framework, ursais/rest-framework, vidtsin/odoo-shopinvader, wahello/rest-framework, warp10/rest-framework, westlyou/odoo-shopinvader, wuhuizhong/odoo-shopinvader, x0rzkov/odoo-rest-framework, xaoxaodigital/rest-framework, xoe-labs/rest-framework, xubiuit/rest-framework, xwh123807/rest-framework, yasmanycastillo/rest-framework, and yvaucher/odoo-shopinvader

<h1 class="title">Base Rest</h1> <p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/rest-framework/tree/13.0/base_rest"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/rest-framework-13-0/rest-framework-13-0-base_rest"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/271/13.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p> <p>This addon provides the basis to develop high level REST APIs for Odoo.</p> <p>As Odoo becomes one of the central pieces of enterprise IT systems, it often becomes necessary to set up specialized service interfaces, so existing systems can interact with Odoo.</p> <p>While the XML-RPC interface of Odoo comes handy in such situations, it requires a deep understanding of Odoo’s internal data model. When used extensively, it creates a strong coupling between Odoo internals and client systems, therefore increasing maintenance costs.</p> <p>Once developed, an <a class="reference external" href="https://spec.openapis.org/oas/v3.0.3">OpenApi</a> documentation is generated from the source code and available via a <a class="reference external" href="https://swagger.io/tools/swagger-ui/">Swagger UI</a> served by your odoo server at <cite>https://my_odoo_server/api-docs</cite>.</p> <p><strong>Table of contents</strong></p> <div class="contents local topic" id="contents"> <ul class="simple"> <li><a class="reference internal" href="#configuration" id="id5">Configuration</a></li> <li><a class="reference internal" href="#usage" id="id6">Usage</a></li> <li><a class="reference internal" href="#known-issues-roadmap" id="id7">Known issues / Roadmap</a></li> <li><a class="reference internal" href="#changelog" id="id8">Changelog</a><ul> <li><a class="reference internal" href="#id1" id="id9">12.0.2.0.1</a></li> <li><a class="reference internal" href="#id2" id="id10">12.0.2.0.0</a></li> <li><a class="reference internal" href="#id3" id="id11">12.0.1.0.1</a></li> <li><a class="reference internal" href="#id4" id="id12">12.0.1.0.0</a></li> </ul> </li> <li><a class="reference internal" href="#bug-tracker" id="id13">Bug Tracker</a></li> <li><a class="reference internal" href="#credits" id="id14">Credits</a><ul> <li><a class="reference internal" href="#authors" id="id15">Authors</a></li> <li><a class="reference internal" href="#contributors" id="id16">Contributors</a></li> <li><a class="reference internal" href="#maintainers" id="id17">Maintainers</a></li> </ul> </li> </ul> </div> <a name="configuration"></a> <h2><a class="toc-backref" href="#id5">Configuration</a></h2> <p>If an error occurs when calling a method of a service (ie missing parameter, ..) the system returns only a general description of the problem without details. This is done on purpose to ensure maximum opacity on implementation details and therefore lower security issue.</p> <p>This restriction can be problematic when the services are accessed by an external system in development. To know the details of an error it is indeed necessary to have access to the log of the server. It is not always possible to provide this kind of access. That's why you can configure the server to run these services in development mode.</p> <p>To run the REST API in development mode you must add a new section '<strong>[base_rest]</strong>' with the option '<strong>dev_mode=True</strong>' in the server config file.</p> <pre> <code lang="cfg">[base_rest] dev_mode=True</code> </pre> <p>When the REST API runs in development mode, the original description and a stack trace is returned in case of error. <strong>Be careful to not use this mode in production</strong>.</p> <a name="usage"></a> <h2><a class="toc-backref" href="#id6">Usage</a></h2> <p>To add your own REST service you must provides at least 2 classes.</p> <ul class="simple"> <li>A Component providing the business logic of your service,</li> <li>A Controller to register your service.</li> </ul> <p>The business logic of your service must be implemented into a component (<code>odoo.addons.component.core.Component</code>) that inherit from 'base.rest.service'</p> <p>Initially, base_rest expose by default all public methods defined in a service. The conventions for accessing methods via HTTP were as follows:</p> <ul class="simple"> <li>The method <code>def get(self, _id)</code> if defined, is accessible via HTTP GET routes <code>&lt;string:_service_name&gt;/&lt;int:_id&gt;</code> and <code>&lt;string:_service_name&gt;/&lt;int:_id&gt;/get</code>.</li> <li>The method <code>def search(self, **params)</code> if defined, is accessible via the HTTP GET routes <code>&lt;string:_service_name&gt;/</code> and <code>&lt;string:_service_name&gt;/search</code>.</li> <li>The method <code>def delete(self, _id)</code> if defined, is accessible via the HTTP DELETE route <code>&lt;string:_service_name&gt;/&lt;int:_id&gt;</code>.</li> <li>The <code>def update(self, _id, **params)</code> method, if defined, is accessible via the HTTP PUT route <code>&lt;string:_service_name&gt;/&lt;int:_id&gt;</code>.</li> <li>Other methods are only accessible via HTTP POST routes <code>&lt;string:_service_name&gt;</code> or <code>&lt;string:_service_name&gt;/&lt;string:method_name&gt;</code> or <code>&lt;string:_service_name&gt;/&lt;int:_id&gt;</code> or <code>&lt;string:_service_name&gt;/&lt;int:_id&gt;/&lt;string:method_name&gt;</code></li> </ul> <pre> <code lang="python">from odoo.addons.component.core import Component class PingService(Component): _inherit = 'base.rest.service' _name = 'ping.service' _usage = 'ping' _collection = 'my_module.services' # The following method are 'public' and can be called from the controller. def get(self, _id, message): return { 'response': 'Get called with message ' + message} def search(self, message): return { 'response': 'Search called search with message ' + message} def update(self, _id, message): return {'response': 'PUT called with message ' + message} # pylint:disable=method-required-super def create(self, **params): return {'response': 'POST called with message ' + params['message']} def delete(self, _id): return {'response': 'DELETE called with id %s ' % _id} # Validator def _validator_search(self): return {'message': {'type': 'string'}} # Validator def _validator_get(self): # no parameters by default return {} def _validator_update(self): return {'message': {'type': 'string'}} def _validator_create(self): return {'message': {'type': 'string'}}</code> </pre> <p>Once you have implemented your services (ping, ...), you must tell to Odoo how to access to these services. This process is done by implementing a controller that inherits from <code>odoo.addons.base_rest.controllers.main.RestController</code></p> <pre> <code lang="python">from odoo.addons.base_rest.controllers import main class MyRestController(main.RestController): _root_path = '/my_services_api/' _collection_name = my_module.services</code> </pre> <p>In your controller, _'root_path' is used to specify the root of the path to access to your services and '_collection_name' is the name of the collection providing the business logic for the requested service/</p> <p>By inheriting from <code>RestController</code> the following routes will be registered to access to your services</p> <pre> <code lang="python">&#64;route([ ROOT_PATH + '&lt;string:_service_name&gt;', ROOT_PATH + '&lt;string:_service_name&gt;/search', ROOT_PATH + '&lt;string:_service_name&gt;/&lt;int:_id&gt;', ROOT_PATH + '&lt;string:_service_name&gt;/&lt;int:_id&gt;/get' ], methods=['GET'], auth=&quot;user&quot;, csrf=False) def get(self, _service_name, _id=None, **params): method_name = 'get' if _id else 'search' return self._process_method(_service_name, method_name, _id, params) &#64;route([ ROOT_PATH + '&lt;string:_service_name&gt;', ROOT_PATH + '&lt;string:_service_name&gt;/&lt;string:method_name&gt;', ROOT_PATH + '&lt;string:_service_name&gt;/&lt;int:_id&gt;', ROOT_PATH + '&lt;string:_service_name&gt;/&lt;int:_id&gt;/&lt;string:method_name&gt;' ], methods=['POST'], auth=&quot;user&quot;, csrf=False) def modify(self, _service_name, _id=None, method_name=None, **params): if not method_name: method_name = 'update' if _id else 'create' if method_name == 'get': _logger.error(&quot;HTTP POST with method name 'get' is not allowed. &quot; &quot;(service name: %s)&quot;, _service_name) raise BadRequest() return self._process_method(_service_name, method_name, _id, params) &#64;route([ ROOT_PATH + '&lt;string:_service_name&gt;/&lt;int:_id&gt;', ], methods=['PUT'], auth=&quot;user&quot;, csrf=False) def update(self, _service_name, _id, **params): return self._process_method(_service_name, 'update', _id, params) &#64;route([ ROOT_PATH + '&lt;string:_service_name&gt;/&lt;int:_id&gt;', ], methods=['DELETE'], auth=&quot;user&quot;, csrf=False) def delete(self, _service_name, _id): return self._process_method(_service_name, 'delete', _id)</code> </pre> <p>As result an HTTP GET call to '<a class="reference external" href="http://my_odoo/my_services_api/ping">http://my_odoo/my_services_api/ping</a>' will be dispatched to the method <code>PingService.search</code></p> <p>In addition to easily exposing your methods, the module allows you to define data schemas to which the exchanged data must conform. These schemas are defined on the basis of <a class="reference external" href="https://docs.python-cerberus.org/en/stable/">Cerberus schemas</a> and associated to the methods using the following naming convention. For a method <cite>my_method</cite>:</p> <ul class="simple"> <li><code>def _validator_my_method(self):</code> will be called to get the schema required to validate the input parameters.</li> <li><code>def _validator_return_my_method(self):</code> if defined, will be called to get the schema used to validate the response.</li> </ul> <p>In order to offer even more flexibility, a new API has been developed.</p> <p>This new API replaces the implicit approach used to expose a service by the use of a python decorator to explicitly mark a method as being available via the REST API: <code>odoo.addons.base_rest.restapi.method</code>.</p> <pre> <code lang="python">class PartnerNewApiService(Component): _inherit = &quot;base.rest.service&quot; _name = &quot;partner.new_api.service&quot; _usage = &quot;partner&quot; _collection = &quot;base.rest.demo.new_api.services&quot; _description = &quot;&quot;&quot; Partner New API Services Services developed with the new api provided by base_rest &quot;&quot;&quot; &#64;restapi.method( [([&quot;/&lt;int:id&gt;/get&quot;, &quot;/&lt;int:id&gt;&quot;], &quot;GET&quot;)], output_param=restapi.CerberusValidator(&quot;_get_partner_schema&quot;), auth=&quot;public&quot;, ) def get(self, _id): return {&quot;name&quot;: self.env[&quot;res.partner&quot;].browse(_id).name} def _get_partner_schema(self): return { &quot;name&quot;: {&quot;type&quot;: &quot;string&quot;, &quot;required&quot;: True} }</code> </pre> <p>Thanks to this new api, you are now free to specify your own routes but also to use other object types as parameter or response to your methods. For example, <cite>base_rest_datamodel</cite> allows you to use Datamodel object instance into your services.</p> <pre> <code lang="python">from marshmallow import fields from odoo.addons.base_rest import restapi from odoo.addons.component.core import Component from odoo.addons.datamodel.core import Datamodel class PartnerSearchParam(Datamodel): _name = &quot;partner.search.param&quot; id = fields.Integer(required=False, allow_none=False) name = fields.String(required=False, allow_none=False) class PartnerShortInfo(Datamodel): _name = &quot;partner.short.info&quot; id = fields.Integer(required=True, allow_none=False) name = fields.String(required=True, allow_none=False) class PartnerNewApiService(Component): _inherit = &quot;base.rest.service&quot; _name = &quot;partner.new_api.service&quot; _usage = &quot;partner&quot; _collection = &quot;base.rest.demo.new_api.services&quot; _description = &quot;&quot;&quot; Partner New API Services Services developed with the new api provided by base_rest &quot;&quot;&quot; &#64;restapi.method( [([&quot;/&quot;, &quot;/search&quot;], &quot;GET&quot;)], input_param=restapi.Datamodel(&quot;partner.search.param&quot;), output_param=restapi.Datamodel(&quot;partner.short.info&quot;, is_list=True), auth=&quot;public&quot;, ) def search(self, partner_search_param): &quot;&quot;&quot; Search for partners :param partner_search_param: An instance of partner.search.param :return: List of partner.short.info &quot;&quot;&quot; domain = [] if partner_search_param.name: domain.append((&quot;name&quot;, &quot;like&quot;, partner_search_param.name)) if partner_search_param.id: domain.append((&quot;id&quot;, &quot;=&quot;, partner_search_param.id)) res = [] PartnerShortInfo = self.env.datamodels[&quot;partner.short.info&quot;] for p in self.env[&quot;res.partner&quot;].search(domain): res.append(PartnerShortInfo(id=p.id, name=p.name)) return res</code> </pre> <a name="known-issues-roadmap"></a> <h2><a class="toc-backref" href="#id7">Known issues / Roadmap</a></h2> <p>The <a class="reference external" href="https://github.com/OCA/rest-framework/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement+label%3Abase_rest">roadmap</a> and <a class="reference external" href="https://github.com/OCA/rest-framework/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Abase_rest">known issues</a> can be found on GitHub.</p> <a name="changelog"></a> <h2><a class="toc-backref" href="#id8">Changelog</a></h2> <a name="id1"></a> <h3><a class="toc-backref" href="#id9">12.0.2.0.1</a></h3> <ul class="simple"> <li>_validator_...() methods can now return a cerberus <code>Validator</code> object instead of a schema dictionnary, for additional flexibility (e.g. allowing validator options such as <code>allow_unknown</code>).</li> </ul> <a name="id2"></a> <h3><a class="toc-backref" href="#id10">12.0.2.0.0</a></h3> <ul class="simple"> <li>Licence changed from AGPL-3 to LGPL-3</li> </ul> <a name="id3"></a> <h3><a class="toc-backref" href="#id11">12.0.1.0.1</a></h3> <ul class="simple"> <li>Fix issue when rendering the jsonapi documentation if no documentation is provided on a method part of the REST api.</li> </ul> <a name="id4"></a> <h3><a class="toc-backref" href="#id12">12.0.1.0.0</a></h3> <p>First official version. The addon has been incubated into the <a class="reference external" href="https://github.com/akretion/odoo-shopinvader">Shopinvader repository</a> from Akretion. For more information you need to look at the git log.</p> <a name="bug-tracker"></a> <h2><a class="toc-backref" href="#id13">Bug Tracker</a></h2> <p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>. 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 <a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p> <p>Do not contact contributors directly about support or help with technical issues.</p> <a name="credits"></a> <h2><a class="toc-backref" href="#id14">Credits</a></h2> <a name="authors"></a> <h3><a class="toc-backref" href="#id15">Authors</a></h3> <ul class="simple"> <li>ACSONE SA/NV</li> </ul> <a name="contributors"></a> <h3><a class="toc-backref" href="#id16">Contributors</a></h3> <ul class="simple"> <li>Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;</li> <li>Sébastien Beau &lt;<a class="reference external" href="mailto:sebastien.beau&#64;akretion.com">sebastien.beau&#64;akretion.com</a>&gt;</li> </ul> <a name="maintainers"></a> <h3><a class="toc-backref" href="#id17">Maintainers</a></h3> <p>This module is maintained by the OCA.</p> <a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a> <p>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.</p> <p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p> <p><a class="reference external" href="https://github.com/lmignon"><img alt="lmignon" src="https://github.com/lmignon.png?size=40px" /></a></p> <p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/13.0/base_rest">OCA/rest-framework</a> project on GitHub.</p> <p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>