Skip to content

Commit

Permalink
[IMP] fastapi: Optionally authenticated partner
Browse files Browse the repository at this point in the history
Defines specific dependencies to support case where te authentication is optional.
  • Loading branch information
lmignon committed Mar 14, 2024
1 parent a23d79f commit 792df9f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
34 changes: 34 additions & 0 deletions fastapi/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ def authenticated_partner_impl() -> Partner:
See the fastapi_endpoint_demo for an example"""


def optionally_authenticated_partner_impl() -> Partner | None:
"""This method has to be overriden when you create your fastapi app
and you need to get an optional authenticated partner into your endpoint.
"""


def authenticated_partner_env(
partner: Annotated[Partner, Depends(authenticated_partner_impl)]
) -> Environment:
"""Return an environment with the authenticated partner id in the context"""
return partner.with_context(authenticated_partner_id=partner.id).env


def optionally_authenticated_partner_env(
partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)],
env: Annotated[Environment, Depends(odoo_env)],
) -> Environment:
"""Return an environment with the authenticated partner id in the context if
the partner is not None
"""
if partner:
return partner.with_context(authenticated_partner_id=partner.id).env
return env

Check warning on line 68 in fastapi/dependencies.py

View check run for this annotation

Codecov / codecov/patch

fastapi/dependencies.py#L67-L68

Added lines #L67 - L68 were not covered by tests


def authenticated_partner(
partner: Annotated[Partner, Depends(authenticated_partner_impl)],
partner_env: Annotated[Environment, Depends(authenticated_partner_env)],
Expand All @@ -66,6 +84,22 @@ def authenticated_partner(
return partner_env["res.partner"].browse(partner.id)


def optionally_authenticated_partner(
partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)],
partner_env: Annotated[Environment, Depends(optionally_authenticated_partner_env)],
) -> Partner | None:
"""If you need to get access to the authenticated partner if the call is
authenticated, you can add a dependency into the endpoint definition on this
method.
This method defer from authenticated_partner by the fact that it returns
None if the partner is not authenticated .
"""
if partner:
return partner_env["res.partner"].browse(partner.id)
return None

Check warning on line 100 in fastapi/dependencies.py

View check run for this annotation

Codecov / codecov/patch

fastapi/dependencies.py#L99-L100

Added lines #L99 - L100 were not covered by tests


def paging(
page: Annotated[int, Query(gte=1)] = 1, page_size: Annotated[int, Query(gte=1)] = 80
) -> Paging:
Expand Down
14 changes: 13 additions & 1 deletion fastapi/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
from fastapi.testclient import TestClient

from ..context import odoo_env_ctx
from ..dependencies import authenticated_partner_impl
from ..dependencies import (
authenticated_partner_impl,
optionally_authenticated_partner_impl,
)


@tagged("post_install", "-at_install")
Expand Down Expand Up @@ -105,6 +108,15 @@ def _create_test_client(
)
if partner or authenticated_partner_impl not in dependencies:
dependencies[authenticated_partner_impl] = partial(lambda a: a, partner)
if partner and optionally_authenticated_partner_impl in dependencies:
raise ValueError(

Check warning on line 112 in fastapi/tests/common.py

View check run for this annotation

Codecov / codecov/patch

fastapi/tests/common.py#L112

Added line #L112 was not covered by tests
"You cannot provide an override for the optionally_authenticated_partner_impl "
"dependency when creating a test client with a partner."
)
if partner or optionally_authenticated_partner_impl not in dependencies:
dependencies[optionally_authenticated_partner_impl] = partial(
lambda a: a, partner
)
app = app or self.default_fastapi_app or FastAPI()
router = router or self.default_fastapi_router
if router:
Expand Down

0 comments on commit 792df9f

Please sign in to comment.