Flask-Occam
Flask-Occam is a Flask extension that is designed to help developers create simple and easy to maintain REST APIs. It is a lightweight abstraction on top of Flask’s existing tools, designed to simplify API development and reduce the amount of boilerplate needed to perform common operations. Using this extension also promotes better code readability and maintainability during the application development lifecycle.
Installation
To install the latest stable release via pip, run:
$ pip install Flask-Occam
Alternatively with easy_install, run:
$ easy_install Flask-Occam
To install the bleeding-edge version of the project (not recommended):
$ git clone http://github.com/bprinty/Flask-Occam.git
$ cd Flask-Occam
$ python setup.py install
Usage
Below is a minimal application configured to take advantage of some of the extension’s core features:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_occam import Occam
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
occam = Occam(app, db)
The following is a minimal application highlighting most of the major features provided by the extension:
from wtforms import validators
from flask_occam import transactional, validate, paginate, log, optional
# models
class Item(db.Model):
__tablename__ = 'item'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False, unique=True, index=True)
url = db.Column(db.String(255))
def json(self):
return dict(
id=self.id,
name=self.name,
url=self.url
)
# endpoints
@app.route('/items')
class Items(object):
@paginate(limit=50, total=Item.count)
def get(self, limit, offset):
"""
GET /items
"""
items = Item.all(limit=limit, offset=offset)
return [x.json() for x in items], 200
@validate(name=str)
@transactional
@log.info('Created new user with name {name}')
def post(self):
"""
POST /items
"""
item = Item.create(**request.json)
return item.json(), 201
@app.route('/items/<id(Item):item>')
class SingleItem(object):
def get(self, item):
"""
GET /items/:id
"""
return item.json(), 200
@validate(
name=optional(str),
url=optional(validators.URL())
)
@transactional
@log.info('Changed metadata for item {item.name}')
def put(self, item):
"""
PUT /items/:id
"""
item.update(**request.json)
return item.json(), 200
@transactional
def delete(self, item):
"""
DELETE /items/:id
"""
item.delete()
return jsonify(msg='Deleted item'), 204
There’s quite a bit to unpack from the application detailed above, including:
Facilities for automatically resolving model identifiers into objects via url converters.
Automatic pagination (via response header) for requests.
Automatic database transaction support for endpoint handlers.
Tools for simpler logging of requests or API methods.
Automatic payload validation (with support for WTForms validators).
SQLAlchemy extensions for CRUD operations on models (providing a simpler API).
Documentation
For more detailed documentation, see the Docs.
Questions/Feedback
File an issue in the GitHub issue tracker.
Metadata
Release files for Flask-Occam 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Flask-Occam-0.2.1.tar.gz | 182.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_Occam-0.2.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 200.2 kB
Release files / Flask-Occam-0.2.1.tar.gz
| Download URL | Flask-Occam-0.2.1.tar.gz |
|---|---|
| Size | 182.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
95fae5597ade192ee6bec3210d80220a79e453d0c6e3790870363de1b62a2425
|
|
BLAKE2b-256 checksum How to use checksums |
565b51753570df603272195084cae14b9b6cc4ade2011748d640693275ea9089
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1
|
Release files / Flask_Occam-0.2.1-py2.py3-none-any.whl
| Download URL | Flask_Occam-0.2.1-py2.py3-none-any.whl |
|---|---|
| Size | 18.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
e70b9c59f1445b663b131b441283c5a9ef9f6bb2f0156a15f3a521063013ccf2
|
|
BLAKE2b-256 checksum How to use checksums |
328622ed7b1932bafd80f52a17f01fdb872b9174fea124e9335617513e764a1f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1
|