Are you getting your roles from the JWT token? a db user query? Did you want to do it as a decorator? You’ll need to add role access control logic to each endpoint you want to protect.
A db query, probably. I was hoping for an existing solution in the starlette world, to be honest.
Are you getting your roles from the JWT token? a db user query? Did you want to do it as a decorator? You’ll need to add role access control logic to each endpoint you want to protect.
Hi @wshayes getting roles/claims from a JWT token is a super interesting topic for me but I’m curious what is your suggestion
current_user: User = Depends(get_current_active_user))
when using app.add_route for graphql?
So in my api I'm returning a pydantic model, and fastAPI is converting it to json. If I test or demonstrate a endpoint directly via the browser (rather than through docs), is there a way to get FastAPI to prettyprint (indent) the json?
it's already the case @nrshapiro the returned json is indented, see this screen for instance:
dict(skip_defaults=True)
You can add any fields you want to a JWT payload. I’ve seen tokens that have several dozen fields and are around 4kb in size - I think that’s a bit overdoing it :) It’s not a problem from the JWT spec/browsers/servers - just adds more IO to each request.
Thanks @wshayes, my first thought is to find a viable solution to receive authorization claims/roles from an external authentication system and spend it over a microservices architecture based on FastAPI
starlette.responses.JSONResponse
) looks like this: class JSONResponse(Response):
media_type = "application/json"
def render(self, content: typing.Any) -> bytes:
return json.dumps(
content,
ensure_ascii=False,
allow_nan=False,
indent=None,
separators=(",", ":"),
).encode("utf-8")
indent=4
and separators=(", ", ": ")
in json.dumps
I think it will render closer to the "pretty" way
import json
import typing
from starlette.responses import Response
class PrettyJSONResponse(Response):
media_type = "application/json"
def render(self, content: typing.Any) -> bytes:
return json.dumps(
content,
ensure_ascii=False,
allow_nan=False,
indent=4,
separators=(", ", ": "),
).encode("utf-8")
@app.get("/", response_class=PrettyJSONResponse)
async def get_some_json():
...
var code="@app.get("/long_task", status_code=202) # 202=Accepted
async def long_task():
from backend.workers import tasks
mytask = tasks.long_task.apply_async(kwargs={'num':30})
return {'task_id': mytask.task_id}
@app.get("/long_task/status/{task_id}", status_code=200) # 200=OK
async def long_task_status(task_id: str):
from backend.workers import tasks
result = tasks.long_task.AsyncResult(task_id)
return {'status': result.status}
"
``` @app.get("/long_task", status_code=202) # 202=Accepted
async def long_task():
from backend.workers import tasks
mytask = tasks.long_task.apply_async(kwargs={'num':30})
return {'task_id': mytask.task_id}
@app.get("/long_task/status/{task_id}", status_code=200) # 200=OK
async def long_task_status(task_id: str):
from backend.workers import tasks
result = tasks.long_task.AsyncResult(task_id)
return {'status': result.status}
```