<p dir="auto">I need to implement object permission in my application. I can handle this in each endpoint but it will lead to a lot of duplicated code.</p>
<p dir="auto">For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@router.get(...)
def read_item(..., item_id: int, ...):
    item = service.get_item(id=item_id)
    
    if item.owner_id != current_user.id:
        raise NotOwnerException

    ..."><pre class="notranslate"><code class="notranslate">@router.get(...)
def read_item(..., item_id: int, ...):
    item = service.get_item(id=item_id)
    
    if item.owner_id != current_user.id:
        raise NotOwnerException

    ...
</code></pre></div>
<p dir="auto">This is a simple scenario.<br>
Is there a way to handle object permission without having to repeat code, maybe using dependencies injection?<br>
I will need in some way to pass item_id to the dependency but I think that it's not possibile.<br>
Has anyone found a viable solution?</p>
<p dir="auto">I could also create a function like this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="def check_permission(item: Item, current_user: User)
    ..."><pre class="notranslate"><code class="notranslate">def check_permission(item: Item, current_user: User)
    ...
</code></pre></div>
<p dir="auto">and call this function in each endpoint, but it doesn't seem a very elegant solution.</p>
<p dir="auto">What do you think?</p>
<h3 dir="auto">First Check</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I added a very descriptive title to this issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I used the GitHub search to find a similar issue and didn't find it.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I searched the FastAPI documentation, with the integrated search.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/swagger-api/swagger-ui">Swagger UI</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
</ul>
<h3 dir="auto">Commit to Help</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I commit to help with one of those options <g-emoji class="g-emoji" alias="point_up_2" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f446.png">👆</g-emoji></li>
</ul>
<h3 dir="auto">Example Code</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# An example based very roughly on https://github.com/grpc/grpc/blob/master/examples/python/helloworld/async_greeter_client.py
from fastapi import FastAPI, Depends

app = FastAPI()

import grpc
import helloworld_pb2
import helloworld_pb2_grpc

async def get_stub():
    async with grpc.aio.insecure_channel('localhost:50051') as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        yield stub

@app.get(&quot;/&quot;)
async def my_endpoint_with_a_grpc_stub(stub = Depends(get_stub)):
    response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
    return f&quot;Greeter client received: {response.message}&quot;"><pre class="notranslate"><span class="pl-c"># An example based very roughly on https://github.com/grpc/grpc/blob/master/examples/python/helloworld/async_greeter_client.py</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>, <span class="pl-v">Depends</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()

<span class="pl-k">import</span> <span class="pl-s1">grpc</span>
<span class="pl-k">import</span> <span class="pl-s1">helloworld_pb2</span>
<span class="pl-k">import</span> <span class="pl-s1">helloworld_pb2_grpc</span>

<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">get_stub</span>():
    <span class="pl-k">async</span> <span class="pl-k">with</span> <span class="pl-s1">grpc</span>.<span class="pl-s1">aio</span>.<span class="pl-en">insecure_channel</span>(<span class="pl-s">'localhost:50051'</span>) <span class="pl-k">as</span> <span class="pl-s1">channel</span>:
        <span class="pl-s1">stub</span> <span class="pl-c1">=</span> <span class="pl-s1">helloworld_pb2_grpc</span>.<span class="pl-v">GreeterStub</span>(<span class="pl-s1">channel</span>)
        <span class="pl-k">yield</span> <span class="pl-s1">stub</span>

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">my_endpoint_with_a_grpc_stub</span>(<span class="pl-s1">stub</span> <span class="pl-c1">=</span> <span class="pl-v">Depends</span>(<span class="pl-s1">get_stub</span>)):
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">stub</span>.<span class="pl-v">SayHello</span>(<span class="pl-s1">helloworld_pb2</span>.<span class="pl-v">HelloRequest</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'you'</span>))
    <span class="pl-k">return</span> <span class="pl-s">f"Greeter client received: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">response</span>.<span class="pl-s1">message</span><span class="pl-kos">}</span></span>"</span></pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">I am looking for advice on how to correctly use FastAPIs dependency injection the GRPC aio library (I need to use this to make requests to a dependent service)</p>
<p dir="auto">The examples all seem to open a channel and use it but then the docs for the sync bits imply that I should multiplex over a single channel <a href="https://github.com/grpc/grpc/tree/master/examples/python/multiplex">https://github.com/grpc/grpc/tree/master/examples/python/multiplex</a> while the AIO docs warn of thread safety and other things (<a href="https://grpc.github.io/grpc/python/grpc_asyncio.html" rel="nofollow">https://grpc.github.io/grpc/python/grpc_asyncio.html</a>) .</p>
<p dir="auto">Unlike the sync lib I cannot init the channel when the file is imported because there is no event loop when that occurs. Should I be doing it in a startup event ?</p>
<p dir="auto">Has anyone used the grpc.aio lib with fast api?</p>
<h3 dir="auto">Operating System</h3>
<p dir="auto">Linux</p>
<h3 dir="auto">Operating System Details</h3>
<p dir="auto">Debian / Ubuntu containers</p>
<h3 dir="auto">FastAPI Version</h3>
<p dir="auto">0.85.0</p>
<h3 dir="auto">Python Version</h3>
<p dir="auto">3.10</p>
<h3 dir="auto">Additional Context</h3>
<p dir="auto"><em>No response</em></p>