diff --git a/.env b/.env index b539844..5842ce8 100644 --- a/.env +++ b/.env @@ -1,8 +1,12 @@ +TITLE="FastApi Decorator Builder" +DESCRIPTION="Description de votre API" ADMIN_EMAIL="deadpool@example.com" APP_NAME="ChimichangAppAAAAAA" ITEMS_PER_USER=2 - +COMMAND_LOAD="python -m uvicorn main:app --reload" +URL="http://127.0.0.1:8000" +SAVE_COUNT_FILE="saved_count.txt" #ENV="development" #DATABASE_PASSWORD="motDePasseSecret" -#THIRD_API_PRIVATE_KEY="cleSecrete" +#THIRD_API_PRIVATE_KEY="cleSecrete" \ No newline at end of file diff --git a/Authentification.py b/Authentification.py index 0521ba0..109c8db 100644 --- a/Authentification.py +++ b/Authentification.py @@ -12,6 +12,7 @@ "email": "johndoe@example.com", "hashed_password": "fakehashedsecret", "disabled": False, + "admin": True, }, "alice": { "username": "alice", @@ -19,6 +20,7 @@ "email": "alice@example.com", "hashed_password": "fakehashedsecret2", "disabled": True, + "admin": False, }, } @@ -35,6 +37,7 @@ class User(BaseModel): email: str = None full_name: str = None disabled: bool = None + admin: bool = None class UserInDB(User): @@ -71,7 +74,7 @@ async def get_current_active_user(current_user: Annotated[User, Depends(get_curr return current_user -async def get_current_inactive_user(current_user: Annotated[User, Depends(get_current_user)]): - if not current_user.disabled: +async def get_current_admin_user(current_user: Annotated[User, Depends(get_current_user)]): + if current_user.disabled: raise HTTPException(status_code=400, detail="Function not available because you are an Active user") return current_user diff --git a/app.py b/app.py index 7409600..c4ca110 100644 --- a/app.py +++ b/app.py @@ -1,17 +1,21 @@ import asyncio from typing import Annotated from functools import lru_cache +from dotenv import load_dotenv from fastapi import FastAPI, APIRouter, Depends, HTTPException, Query from fastapi.security import OAuth2PasswordRequestForm from Authentification import User, get_current_active_user, fake_users_db, UserInDB, fake_hash_password, \ - get_current_inactive_user + get_current_admin_user from pydantic import BaseModel - from settings import Settings +load_dotenv() +settings1 = Settings() my_router = APIRouter() -app = FastAPI() +app = FastAPI(title=settings1.title, + description= settings1.description, + ) @lru_cache @@ -61,12 +65,12 @@ def wrapper(**kwargs): @fast_api_decorator(route="/add/", method=["GET"], type_args=[int, int]) -def add_function(x: Annotated[int, Query(description="Int we'll add something")], a: Annotated[int, Query(description="Int added")]): +def add_function(x: Annotated[int, Query(description="Int we'll add something")], a: Annotated[int, Query(description="Int added")],current_user: User = Depends(get_current_active_user)): return {f"{x} + {a} equals": x + a} @fast_api_decorator(route="/sous/", method=["GET"], type_args=[int, list]) -def sous_function(x: Annotated[int, Query(description="Int we'll substract something")], lst: Annotated[list[int], Query(description="List of 2 int that will be substracted")]): +def sous_function(x: Annotated[int, Query(description="Int we'll substract something")], lst: Annotated[list[int], Query(description="List of 2 int that will be substracted")],current_user: User = Depends(get_current_active_user)): return {f"{x} - {lst[0]} - {lst[1]} equals": x - lst[0] - lst[1]} @@ -75,8 +79,9 @@ def read_users_me(current_user: User = Depends(get_current_active_user)): return current_user -@fast_api_decorator(route="/power/", method=["POST"], type_args=[int, int]) -def power_function(x: Annotated[int, Query(description="Int we'll add something")], a: Annotated[int, Query(description="Int added")], current_user: User = Depends(get_current_inactive_user)): + +@fast_api_decorator(route="/power/", method=["POST"],type_args=[int, int]) +def power_function(x: Annotated[int, Query(description="Int we'll add something")], a: Annotated[int, Query(description="Int added")], current_user: User = Depends(get_current_active_user)): return {f"{x} to the power of {a}": int(x)**int(a)} @@ -99,11 +104,11 @@ async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]): @app.get("/info") -async def info(settings: Annotated[Settings, Depends(get_settings)]): +async def info(): return { - "app_name": settings.app_name, - "admin_email": settings.admin_email, - "items_per_user": settings.items_per_user, + "app_name": settings1.app_name, + "admin_email": settings1.admin_email, + "items_per_user": settings1.items_per_user, } @@ -114,12 +119,12 @@ class InputDiv(BaseModel): # Pour faire une requête avec un argument "Body" ou un json avec des arguments il faut passer # par une méthode "POST" et pas "GET" @fast_api_decorator(route="/div/", method=["POST"], type_args=[int, InputDiv]) -def div_function(x: Annotated[int, Query(description="Int we will divide something")], item: InputDiv): +def div_function(x: Annotated[int, Query(description="Int we will divide something")], item: InputDiv,current_user: User = Depends(get_current_active_user)): return {f"{x} / {item.div} equals": item.div} @app.get("/stats") -async def get_stats(): +async def get_stats(current_user: User = Depends(get_current_admin_user)): request_count = get_saved_value() return {"request_count": request_count} diff --git a/settings.py b/settings.py index 10e85c9..179e4ff 100644 --- a/settings.py +++ b/settings.py @@ -2,7 +2,9 @@ class Settings(BaseSettings): + title: str = "FastAPIBuilder" + description: str = "default" app_name: str = "Awesome" admin_email: str = "Et" items_per_user: int = 50 - model_config = SettingsConfigDict(env_file=".env") + #model_config = SettingsConfigDict(env_file=".env")