from typing import Annotated from asyncreq import request from fastapi import APIRouter, Header, HTTPException, Request from src.api.config import API_USERS from src.api.routes.auth import checkAuth from src.api.schemas.user import User, UserMin, UserRegister router = APIRouter() @router.post("", response_model=UserMin) async def create_users(user: UserRegister, req: Request): request_id = req.state.request_id header = {"x-api-request-id": request_id} (response, status, _) = await request( f"{API_USERS}", "POST", json=user.model_dump(), headers=header ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response @router.post("/airline", response_model=UserMin) async def create_airline( user: UserRegister, req: Request, authorization: Annotated[str | None, Header()] = None, ): await checkAuth(req, authorization, roles=["admin"]) request_id = req.state.request_id header = {"x-api-request-id": request_id} data = user.model_dump() data["role"] = "airline" (response, status, _) = await request( f"{API_USERS}", "POST", json=data, headers=header ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response @router.get("/{id}", response_model=User) async def get_user( id: str, req: Request, authorization: Annotated[str | None, Header()] = None ): await checkAuth(req, authorization, userId=id) request_id = req.state.request_id header = {"x-api-request-id": request_id} (response, status, _) = await request(f"{API_USERS}/{id}", "GET", headers=header) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response @router.put("/{id}", response_model=User) async def update_user( id: str, user: UserRegister, req: Request, authorization: Annotated[str | None, Header()] = None, ): await checkAuth(req, authorization, userId=id) request_id = req.state.request_id header = {"x-api-request-id": request_id} (response, status, _) = await request( f"{API_USERS}/{id}", "PUT", json=user.model_dump(), headers=header ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response @router.delete("/{id}") async def delete_user( id: str, req: Request, authorization: Annotated[str | None, Header()] = None ): await checkAuth(req, authorization, userId=id) request_id = req.state.request_id header = {"x-api-request-id": request_id} (response, status, _) = await request(f"{API_USERS}/{id}", "DELETE", headers=header) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) return response