Skip to content

Flask extension for creating REST APIs. You get autogenerated OpenAPI spec (with Redoc and Swagger Docs), Request parsing and validations (query, path, body, form and files). Response Validation. It uses pydantic under the hood.

License

Notifications You must be signed in to change notification settings

zero-shubham/flask-ease

Repository files navigation

FlaskEase

Flask extension for creating REST APIs. You get autogenerated OpenAPI spec (with Redoc and Swagger Docs), Request parsing and validations (query, path, body, form and files) and Response validation.

Checkout example here

Documentation

Documentation is coming soon :)

Try Example

$ git clone [email protected]:zero-shubham/flask-ease.git
$ cd flask-ease
$ poetry install
$ source "$( poetry env list --full-path )/bin/activate"
$ python example/main.py

Now go to http://127.0.0.1:5000/docs to find SwaggerUI docs for your API.

Simple Usage

# * example/resources/pet.py
from application import (
    FlaskEaseAPI,
    Depends,
    HTTPException,
    status,
    File
)
from schemas.pet import (
    PetCreationForm,
    PetInResp,
    PetInDB,
    PetsInResp
)
from utils.dependencies import get_current_user
from crud.pet import (
    add_new_pet_to_db,
    find_pet_by_id,
    get_all_pets_count_in_db,
    get_all_pets_in_db
)
from uuid import uuid4, UUID
from flask import send_from_directory

pets_blp = FlaskEaseAPI(
    blueprint_name="Pets",
    url_prefix="/pets"
)


@pets_blp.post(
    route="/",
    response_model=PetInResp,
    tags=["pets"],
    auth_required=True
)
def create_new_pet(
    obj_in: PetCreationForm,
    current_user=Depends(get_current_user)
):
    """
    Add a new pet to DB
    """

    if obj_in.owner != current_user["id"]:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            "You are not authorised for this operation."
        )

    new_pet = add_new_pet_to_db(PetInDB(
        id=str(uuid4()),
        **obj_in.dict()
    ).dict())
    return new_pet


@pets_blp.get(
    route="/<uuid:id>",
    response_model=PetInResp,
    tags=["pets"],
    auth_required=True
)
def get_pet_by_id(
    id: UUID
):
    """
    Get pet by id
    """
    pet = find_pet_by_id(id)
    return pet


@pets_blp.get(
    route="/",
    response_model=PetsInResp,
    tags=["pets"],
    auth_required=True
)
def get_all_pets(
    offset: int = 0,
    limit: int = 10,
    current_user=Depends(get_current_user)
):
    """
    Get all pets in db
    """
    pets = get_all_pets_in_db(
        offset,
        limit
    )
    count = get_all_pets_count_in_db()
    return PetsInResp(
        pets=pets,
        total_count=count
    )


@pets_blp.post(
    route="/<uuid:id>/photo",
    tags=["pets"],
    auth_required=True,
    responses={
        '204': 'File accepted and saved.'
    }
)
def add_pet_photo(
    id: UUID,
    photo: File("image/png"),
    current_user=Depends(get_current_user)
):
    """
    Add pet photo.
    """
    with open(f"{id}.png", "wb") as photoFile:
        photoFile.write(photo)
    return "True", 204

For a complete understanding check the example here

File-uploads are not yet supported via FlaskEase - to be added soon Now with File-upload and Multipart-Form support.

Abandoned project - chose to solve better problems. Use FastAPI or flask-smorest

About

Flask extension for creating REST APIs. You get autogenerated OpenAPI spec (with Redoc and Swagger Docs), Request parsing and validations (query, path, body, form and files). Response Validation. It uses pydantic under the hood.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages