Flask extension for simplifying REST API development.
Project description
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.
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 Flask-Occam-0.2.1.tar.gz
.
File metadata
- Download URL: Flask-Occam-0.2.1.tar.gz
- Upload date:
- Size: 182.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95fae5597ade192ee6bec3210d80220a79e453d0c6e3790870363de1b62a2425 |
|
MD5 | a06773a194c9ee160132caf3365292ae |
|
BLAKE2b-256 | 565b51753570df603272195084cae14b9b6cc4ade2011748d640693275ea9089 |
File details
Details for the file Flask_Occam-0.2.1-py2.py3-none-any.whl
.
File metadata
- Download URL: Flask_Occam-0.2.1-py2.py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e70b9c59f1445b663b131b441283c5a9ef9f6bb2f0156a15f3a521063013ccf2 |
|
MD5 | 61f60a545d987a8871e9dd836c8af0f4 |
|
BLAKE2b-256 | 328622ed7b1932bafd80f52a17f01fdb872b9174fea124e9335617513e764a1f |