Repository navigation
Conversation
Pending OCA#179 (http_json_dispatcher). To be removed once merged.
63e7a78 to
810a3df
Compare
Pending OCA#179 (http_json_dispatcher). To be removed once merged.
|
@simahawk I like the idea and the approach. It seems like a good step towards reducing the amount of code while adding more value on top of Odoo's standard features while also providing a safe path away from Regarding:
I agree, but even though Odoo controllers can be inherited, if the business logic is implemented directly in a route method, it becomes difficult to extend as well. That's why, regardless of the approach, the actual logic should be delegated to a model, wizard, or transient model. 😏 For the record, pydantic models are extendable.... (you need extra libs and addon but it works like a charm)
|
|
@simahawk http_json is may be too restrictive.... REST doesn't limit to json. You could receive multipart request with a form part with a json document and others part with binary files.... |
That's correct but I couldn't find a better naming and a json payload is what is going to handle 99% of the times 😅 Would you prefer
One of the nice pros of this approach is that you can use standard controllers and as such you can use Same for the Cerberus validator #182 that you can use on std controllers. |
|
Curious thing: I first chose that name but I found it too generic 😆 |
fb10938 to
bf8a38b
Compare
Route type `http_api`: generic dispatcher for HTTP routes speaking JSON. * parse query string and body (JSON, forms) once * set lang from `Accept-Language` * validate/convert input and output via pluggable params declared on the route * serialize results as JSON * map exceptions to JSON error responses (format compatible w/ base_rest) * `ir.http` hooks to extend each step of the pipeline (`_http_api_*`) Lazy params: `ApiCall` exposes `query`, `body` and `params` as cached properties, parsed on first access. `params` merges them: query, then body, then URL args. CSRF: the token is read from the `X-CSRF-Token` header first; the query and the body are parsed only when the header is missing. The `csrf_token` key is transport data: it is never part of the params of the endpoint. Parsers: routes override the body parsers via `body_parsers` (mimetype map, formerly `parsers`) and the query parser via `query_parser`. Params: `ApiParam.from_params(call)` receives the `ApiCall` and reads `call.params`, `call.query` or `call.body` as it needs. A param reading only the query never parses the body. Errors: `dev_mode` is configured in the `[http_api]` section.
bf8a38b to
f00fde4
Compare
Pending OCA#179 (http_api_dispatcher). To be removed once merged.
|
renamed module back to http_api* and routing type to http_api |
Motivation
base_restties every API to its components and to therestapiroute type, so an API can't leavebase_rest(shopfloor included) without a replacement. This module provides the common plumbing of an API endpoint as a route type of its own,http_json, independent of components. APIs can then move to it one endpoint at a time. The error format and the params stay compatible withbase_rest.Key features
http_jsonroute type: params parsed once from the query string and the body (JSON or forms)Accept-Languageheaderhttp_json_cerberus)A bit more of context in comparison to base_rest/fastapi (from module's readme)
base_rest(OCA/rest-framework) has been the foundation of many APIs,including the Shopfloor apps. Over time it showed some limits:
documentation and routing are bound together
and you cannot use one piece without the others
components, a second registrywith its own inheritance and boot lifecycle,
which is often used only to look up a class by name
making routing hard to follow and to customize
The
fastapistack is a way to go for new standalone APIs,but moving existing APIs to it means rewriting every service and schema and endpoints are not extendable models.
Plus, it pulls in more external dependencies that for many cases are not needed.
This module is the base of a lighter stack, made of small modules
that can be used separately and extended via standard Odoo inheritance:
http_json_dispatcher: thehttp_jsonroute type (parsing, validation hooks, JSON errors)usable by plain Odoo controllers
endpoint_spec: declare endpoints on services, for routes generatorshttp_json_cerberus: Cerberus validationopenapi_doc_base: API docs from the actual routesendpoint_route_handlergenerators: routes computed from the codewithout storing a row per route
Goals:
base_reststack without moving tofastapicomponent, staying highly customizable(routing keys per route,
ir.httphooks per database)base_restclients (error format, validation behavior)to allow a gradual migration
The only doubt that I have ATM is: should this stay here or go to rest-framework repo? (even though is not strictly bound to "REST" stuff.
@lmignon would be nice to have your feedback 🙏
One last thing: as this work it's a derivation of base_rest, I've tried to keep the copyright where it made sense, but please let me know if I left something behind 😉