Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 5 additions & 2 deletions Authentification.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
"email": "johndoe@example.com",
"hashed_password": "fakehashedsecret",
"disabled": False,
"admin": True,
},
"alice": {
"username": "alice",
"full_name": "Alice Wonderson",
"email": "alice@example.com",
"hashed_password": "fakehashedsecret2",
"disabled": True,
"admin": False,
},
}

Expand All @@ -35,6 +37,7 @@ class User(BaseModel):
email: str = None
full_name: str = None
disabled: bool = None
admin: bool = None


class UserInDB(User):
Expand Down Expand Up @@ -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
31 changes: 18 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]}


Expand All @@ -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)}


Expand All @@ -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,
}


Expand All @@ -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}

Expand Down
4 changes: 3 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")