Skip to content

Commit ed9d8bc

Browse files
committed
[IMP]fastapi: obtain endpoint via common method
1 parent 01013f6 commit ed9d8bc

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

fastapi/fastapi_dispatcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def dispatch(self, endpoint, args):
2727
# don't parse the httprequest let starlette parse the stream
2828
self.request.params = {} # dict(self.request.get_http_params(), **args)
2929
environ = self._get_environ()
30-
root_path = "/" + environ["PATH_INFO"].split("/")[1]
30+
path = environ["PATH_INFO"]
3131
# TODO store the env into contextvar to be used by the odoo_env
3232
# depends method
33-
with fastapi_app_pool.get_app(env=request.env, root_path=root_path) as app:
34-
uid = request.env["fastapi.endpoint"].sudo().get_uid(root_path)
33+
with fastapi_app_pool.get_app(env=request.env, root_path=path) as app:
34+
uid = request.env["fastapi.endpoint"].sudo().get_uid(path)
3535
data = BytesIO()
3636
with self._manage_odoo_env(uid):
3737
for r in app(environ, self._make_response):

fastapi/models/fastapi_endpoint.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,15 @@ def _reset_app_cache_marker(self):
211211
"""
212212

213213
@api.model
214-
def get_app(self, root_path):
215-
record = self.search([("root_path", "=", root_path)])
214+
@tools.ormcache("path")
215+
def get_endpoint(self, path):
216+
root_path = "/" + path.split("/")[1]
217+
endpoint = self.search([("root_path", "=", root_path)])[:1] or False
218+
return endpoint
219+
220+
@api.model
221+
def get_app(self, path):
222+
record = self.get_endpoint(path)
216223
if not record:
217224
return None
218225
app = FastAPI()
@@ -236,9 +243,9 @@ def _clear_fastapi_exception_handlers(self, app: FastAPI) -> None:
236243
self._clear_fastapi_exception_handlers(route.app)
237244

238245
@api.model
239-
@tools.ormcache("root_path")
240-
def get_uid(self, root_path):
241-
record = self.search([("root_path", "=", root_path)])
246+
@tools.ormcache("path")
247+
def get_uid(self, path):
248+
record = self.get_endpoint(path)
242249
if not record:
243250
return None
244251
return record.user_id.id

0 commit comments

Comments
 (0)