Add remote changes

This commit is contained in:
Pavel Knyazev 2022-12-08 19:02:45 +03:00
parent 0373c631bb
commit 14ede0de19
1 changed files with 53 additions and 0 deletions

53
main.py Normal file
View File

@ -0,0 +1,53 @@
from fastapi import APIRouter, Depends, FastAPI, Request
from fastapi.testclient import TestClient
app = FastAPI()
async def check_current_user(request: Request, user_id: str):
# Imagine, there's a check that user exists
request.state.current_user_id = user_id
async def check_current_project(request: Request, project_id: str):
# Imagine, there's a check that project exists
request.state.current_project_id = project_id
async def current_user(request: Request):
return request.state.current_user_id
async def current_project(request: Request):
return request.state.current_project_id
router_users = APIRouter(prefix="/users")
router_projects = APIRouter(
prefix="/{user_id}/projects",
dependencies=[Depends(check_current_user)],
)
router_issues = APIRouter(
prefix="/{project_id}/issues",
dependencies=[Depends(check_current_project)],
)
@router_issues.get("/{issue_id}")
async def get_issue(
issue_id: str,
current_user: str = Depends(current_user),
current_project: str = Depends(current_project),
):
return {
"user_id": current_user,
"project_id": current_project,
"issue_id": issue_id,
}
router_projects.include_router(router_issues)
router_users.include_router(router_projects)
app.include_router(router_users)