Skip to content

CollinsMunene/FastApi-E-commerce-Applications-Endpoints-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastApi E-commerce Applications Endpoints 👋

Introduction

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:

  • Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
  • Fast to code: Increase the speed to develop features by about 200% to 300%. *
  • Fewer bugs: Reduce about 40% of human (developer) induced errors. *
  • Easy: Designed to be easy to use and learn. Less time reading docs.
  • Robust: Get production-ready code. With automatic interactive documentation.
  • Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
  • This Repository is aimed at helping those who are working on e-commerce applications.

    A few assumptions:

    1. You have a good understanding of Python Language
    2. The project you want to use this codebase for uses an SQL Database
    3. You have an understanding of sqlalchemy as an ORM

    The Repository contains 5 main files namely:

    1. main.py
    2. This file is the entry point for the application. It contains the main routes and redirects all user requested routes to their respective functions.
    3. crud.py
    4. This file contains the main functions that interact with the database, it is basically where the business logic happens.
    5. models.py
    6. This file is similar to the one in Django. In this file is where we define our database tables.
    7. schema.py
    8. This file contains the data schemas for our application.
    9. database.py
    10. This file contains our database connection string.

    Endpoints

    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

    Code Samples

    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

    Install

    git clone 
    source fastapienv/bin/activate
    pip install -r requirements.txt
    cd sql_app

    Usage

    uvicorn main:app --reload

    Author

    👤 Collins H. Munene

    Show your support

    Give a ⭐️ if this project helped you!

    About

    This repository shows FastApi in action, while creating api's easily

    Resources

    License

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published