FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
The key features that I like in FastApi are:
This Repository is aimed at helping those who are working on e-commerce applications.
- You have a good understanding of Python Language
- The project you want to use this codebase for uses an SQL Database
- You have an understanding of sqlalchemy as an ORM
The Repository contains 5 main files namely:
- main.py This file is the entry point for the application. It contains the main routes and redirects all user requested routes to their respective functions.
- crud.py This file contains the main functions that interact with the database, it is basically where the business logic happens.
- models.py This file is similar to the one in Django. In this file is where we define our database tables.
- schema.py This file contains the data schemas for our application.
- database.py This file contains our database connection string.
A basic E-commerce application has features such as Log in/ Register/ Cart functionalities/ Payment Functionalities/ Admin CRUD capabilities among other functionalities.
We have the following API's
- /register
- /login
- /get_user/username
- /add_item
- /get_item/id
- /del_item/id
- /add_to_cart/username
- /del_cart_item/id
- /payment
- /callback
TO NOTE: The MPESA module is not yet complete.I will be updating it in a few days
Just to show you how the flow works here is the working example of the get_item/id endpoint.
main.py
@app.get("/get_item/{id}", response_model=schemas.ItemAInfo)
def get_user(id, db:Session = Depends(get_db)):
db_item = crud.get_item_by_id(db, id=id)
if db_item is None:
raise HTTPException(status_code=400, detail="No item found")
return db_item
The above piece of code handles the endpoint and calls the function get_item_by_id in the crud.py file for further processing.
crud.py
def get_item_by_id(db: Session, id: int):
return db.query(models.ItemInfo).filter(models.ItemInfo.id == id).first()
The above piece of code handles the get_item_by_id call and queries the DB for an item with the id passed in the parameter
git clone
source fastapienv/bin/activate
pip install -r requirements.txt
cd sql_app
uvicorn main:app --reload
👤 Collins H. Munene
- Website: My portfolio
- Twitter: @Hillary Collins
- Github: @Collins Munene
- LinkedIn: @Collins Munene
Give a ⭐️ if this project helped you!