From 9e00a5f744fca1d61f2b69acdb1eae8e63e46d17 Mon Sep 17 00:00:00 2001 From: Anthony Al Lazkani Date: Fri, 22 Nov 2024 00:35:58 +0200 Subject: [PATCH] Add router to FastAPI for improved route management - Implemented a new router for organizing API endpoints. - Registered the router with the main FastAPI app. - Enhanced modularity and readability of the codebase. --- src/main.py | 14 +++----------- src/routers/cards.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 src/routers/cards.py diff --git a/src/main.py b/src/main.py index 46af390..d23880d 100644 --- a/src/main.py +++ b/src/main.py @@ -1,20 +1,12 @@ +from fastapi import FastAPI, HTTPException from typing import Annotated -from fastapi import FastAPI, HTTPException - -from src.entities.cards import CARDS +import src.routers.cards as cards app = FastAPI() +app.include_router(cards.router, prefix="/cards") @app.get("/") async def root(): return {"message": "Hello World!"} - - -@app.get("/cards/{card}") -def get_cards(card: str): - for key in CARDS: - if key in card: - return {"cards": CARDS[card]} - raise HTTPException(status_code=404, detail="Cards with this type not found") diff --git a/src/routers/cards.py b/src/routers/cards.py new file mode 100644 index 0000000..67c5c64 --- /dev/null +++ b/src/routers/cards.py @@ -0,0 +1,13 @@ +from fastapi import APIRouter, HTTPException + +from src.entities.cards import CARDS + +router = APIRouter() + + +@router.get("/{card}") +async def get_cards(card: str): + for key in CARDS: + if key == card: + return {"cards": CARDS[card]} + raise HTTPException(status_code=404, detail="Cards with this type not found")