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
- Create End-Points (POST, application/json) : http://127.0.0.1:5000/create
- Request Payload :
{
"data": {
"first_name": "hmtmcse",
"last_name": "com",
"email": "hmtmcse.com@gmail.com",
"age": 7,
"income": 5000
}
}
- Details End-Points (GET): http://127.0.0.1:5000/details/
- Update End-Points (POST, application/json): http://127.0.0.1:5000/update
- Request Payload :
{
"data": {
"id": 1,
"first_name": "Touhid",
"last_name": "Mia",
"email": "hmtmcse.com@gmail.com",
"age": 7,
"income": 5000
}
}
- Soft Delete End-Points (DELETE): http://127.0.0.1:5000/delete/
- Restore Soft Delete End-Points (GET): http://127.0.0.1:5000/restore/
- List with pagination End-Points (GET): http://127.0.0.1:5000/list
- Params
- per-page
- page
- sort-field
- sort-order
- search
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
- Changes : https://opensource.problemfighter.org/flask/pf-flask-rest
- PyPI Releases : https://pypi.org/project/pf-flask-rest
- Source Code : https://github.com/problemfighter/pf-flask-rest
- Issue Tracker : https://github.com/problemfighter/pf-flask-rest/issues
- Website : https://www.problemfighter.com/open-source
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file PF-Flask-REST-1.0.1.tar.gz
.
File metadata
- Download URL: PF-Flask-REST-1.0.1.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 431c8d263d240d825fe750bd23ce1baaa7d247c915e0123b13b1c39f3fc5da68 |
|
MD5 | 91dd47c12a03bff78984be9d4fd72296 |
|
BLAKE2b-256 | 28201fa1dd9f31ef7e91c9e3e359d0864e6957f076e46e74a6f644d1bd81a8dc |
File details
Details for the file PF_Flask_REST-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: PF_Flask_REST-1.0.1-py3-none-any.whl
- Upload date:
- Size: 25.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a42f67fa336a1fced360bff66bd3cfffba3f9865264950ad79aa7d0ca741496 |
|
MD5 | 6ac86a35d59dedd356ec21a16569d0e2 |
|
BLAKE2b-256 | b848094f414e4d64a6d1cf440fd60076d6f23d566f95eba37e67706271ed83d1 |