Skip to main content

Flask REST API by Problem Fighter Library

Project description

In the name of God, the Most Gracious, the Most Merciful.

PF-Flask-REST

Problem Fighter Flask Representational State Transfer (PF-Flask-REST) library is build for rapid API development & Monolithic application development. It has useful class and methods which allow developer to build set of CRUD API end points within 5 minutes. Automatic Data validation, Data processing, & Data CRUD with Database. Let's see what is waiting for us.




Documentation

Install and update using pip:

pip install -U PF-Flask-REST

Codes

from flask import Flask
from marshmallow import fields
from pf_flask_db.pf_app_model import AppModel
from pf_flask_rest.helper.pf_flask_rest_crud_helper import RestCRUDHelper
from pf_flask_rest.api.pf_app_api_def import APIAppDef
from pf_flask_db.pf_app_database import app_db
from pf_flask_rest.pf_flask_rest import pf_flask_rest

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///pf-flask-rest-quick-start.sqlite"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

# Database Initialization
app_db.init_app(app)


# Person Model
class Person(AppModel):
    first_name = app_db.Column(app_db.String(150), nullable=False)
    last_name = app_db.Column(app_db.String(150))
    email = app_db.Column(app_db.String(120), nullable=False)
    age = app_db.Column(app_db.Integer)
    income = app_db.Column(app_db.Float, default=0)


# Person DTO
class PersonDTO(APIAppDef):
    class Meta:
        model = Person
        load_instance = True

    first_name = fields.String(required=True, error_messages={"required": "Please enter first name"})
    last_name = fields.String(allow_none=True)
    email = fields.Email(required=True, error_messages={"required": "Please enter first name"})
    age = fields.Integer(allow_none=True)
    income = fields.Float(allow_none=True)


# Person Update DTO with Primary Key
class PersonUpdateDTO(PersonDTO):
    class Meta:
        model = Person
        load_instance = True

    id = fields.Integer(required=True, error_messages={"required": "Please enter id"})


# Create Database Tables
with app.app_context():
    app_db.create_all()

# REST API Initialization
pf_flask_rest.init_app(app)


# Initialization Build-in REST CRUD system
rest_curd_helper = RestCRUDHelper(Person)

@app.route('/')
def bismillah():
    return "PF Flask REST Example"


# CREATE REQUEST with JSON Data
@app.route("/create", methods=['POST'])
def create():
    return rest_curd_helper.rest_create(PersonDTO())


# DETAILS by person id
@app.route("/details/<int:id>", methods=['GET'])
def details(id: int):
    return rest_curd_helper.rest_details(id, PersonDTO())


# UPDATE by person existing data
@app.route("/update", methods=['POST'])
def update():
    return rest_curd_helper.rest_update(PersonUpdateDTO())


# SOFT DELETE person entity
@app.route("/delete/<int:id>", methods=['DELETE'])
def delete(id: int):
    return rest_curd_helper.rest_delete(id)


# RESTORE SOFT DELETED person entity
@app.route("/restore/<int:id>", methods=['GET'])
def restore(id: int):
    return rest_curd_helper.rest_restore(id)


# LIST of person entity with pagination
@app.route("/list", methods=['GET'])
def list():
    search_fields = ['first_name', 'last_name', 'email']
    return rest_curd_helper.rest_paginated_list(PersonDTO(), search_fields=search_fields)


if __name__ == '__main__':
    app.run()

PF Flask REST Test the API end points

Open POSTMan or any other REST API client and try below end-points

{
    "data": {
    	"first_name": "hmtmcse",
    	"last_name": "com",
    	"email": "hmtmcse.com@gmail.com",
    	"age": 7,
    	"income": 5000
    }
}



{
    "data": {
    	"id": 1,
    	"first_name": "Touhid",
    	"last_name": "Mia",
    	"email": "hmtmcse.com@gmail.com",
    	"age": 7,
    	"income": 5000
    }
}

Please find the Documentation with example from hmtmcse.com




Donate

Problem Fighter develops and supports PF-Flask-REST and the libraries it uses. In order to grow the community of contributors and users, and allow the maintainers to devote more time to the projects.




Contributing

For guidance on setting up a development environment and how to make a contribution to PF-Flask-REST, see the contributing guidelines.




Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PF-Flask-REST-1.0.1.tar.gz (16.2 kB view hashes)

Uploaded Source

Built Distribution

PF_Flask_REST-1.0.1-py3-none-any.whl (25.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page