Repository navigation
[ADD] http_api_dispatcher: generic dispatcher for JSON APIs - #689
Conversation
|
@lmignon following your advice I'm moving OCA/web-api#179 here 😉 |
Pending OCA#689 (http_api_dispatcher). To be removed once merged.
| for k, v in httprequest.headers.items() | ||
| if k not in BLACKLISTED_LOG_HEADERS | ||
| } | ||
| params = dict(getattr(request, "params", None) or {}) |
There was a problem hiding this comment.
@simahawk IMO, here you're at risk. You provide a way to a malware to consume physical resources by providing unrelated large payload...
There was a problem hiding this comment.
I added shortening of the strings before logging
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.
e870ffa to
3120851
Compare
|
/ocabot merge nobump |
|
What a great day to merge this nice PR. Let's do it! |
|
@simahawk The merge process could not be finalized, because command |
|
/ocabot merge nobump |
|
This PR looks fantastic, let's merge it! |
|
@simahawk The merge process could not be finalized, because command |
|
/ocabot merge nobump |
|
On my way to merge this fine PR! |
|
Congratulations, your PR was merged at 8b0eb10. Thanks a lot for contributing to OCA. ❤️ |
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_api_dispatcher: thehttp_apiroute type (parsing, validation hooks, JSON errors)usable by plain Odoo controllers
endpoint_spec: declare endpoints on services, for routes generatorshttp_api_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