Adds SQLAlchemy support to your Flask application for handle apis.
Project description
SQLAlchemy-Api-Handler is an extension that adds support for handling apis with sqlalchemy. It helps to handle models with humanized ids once it is jsonified, throws api errors for some casting of value during the save time, and dictifies model objects into jsonified ones.
[![CircleCI](https://circleci.com/gh/betagouv/sqlalchemy-api-handler/tree/master.svg?style=svg)](https://circleci.com/gh/betagouv/sqlalchemy-api-handler/tree/master) [![Coverage Status](https://coveralls.io/repos/github/betagouv/sqlalchemy-api-handler/badge.svg)](https://coveralls.io/github/betagouv/sqlalchemy-api-handler)
Installing
Install and update using pip:
$ pip install -U SQLAlchemy-Api-Handler
A Simple Example
Suppose a request POST /users {“email”: “marx.foo@plop.fr”, name: “Marx Foo”} :
from flask import Flask, jsonify, request
from sqlalchemy_api_handler import ApiHandler
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite"
db = SQLAlchemy(app)
ApiHandler.set_db(db)
class User(ApiHandler, db.Model):
email = db.Column(db.String, unique=True, nullable=False)
name = db.Column(db.String, unique=True, nullable=False)
@app.route('/users', methods=['POST'])
def post_user():
user = User(**request.form)
ApiHandler.save(user)
return jsonify(as_dict(user))
The success result will have stored a user object at, let’s say id = 32, and so will fetch an object at humanized id = humanize(32), ie
{"id": "EA", "email": "marx.foo@plop.fr", name: "Marx Foo"}
Playing with nesting data
Suppose a request GET /offers
from flask import Flask, jsonify, request
from sqlalchemy.orm import relationship
from sqlalchemy_api_handler import ApiHandler
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite"
db = SQLAlchemy(app)
ApiHandler.set_db(db)
class Venue(ApiHandler, db.Model):
address = db.Column(db.String, unique=True, nullable=False)
name = db.Column(db.String, unique=True, nullable=False)
class Offer(ApiHandler, db.Model):
name = db.Column(db.String, unique=True, nullable=False)
venueId = db.Column(db.BigInteger,
db.ForeignKey("venue.id"),
nullable=False,
index=True)
venue = relationship('Venue',
foreign_keys=[venueId],
backref='offers')
class Stock(ApiHandler, db.Model):
available = db.Column(db.Integer, nullable=False)
offerId = db.Column(db.BigInteger,
db.ForeignKey('offer.id'),
index=True,
nullable=False)
offer = relationship('Offer',
foreign_keys=[offerId],
backref='stocks')
venue = Venue(address="Somewhere I belong", name="MyVenue")
offer = Offer(name="MyOffer")
stock = Stock(available=10)
stock.offer = offer
offer.venue = venue
ApiHandler.save(stock)
offer_includes = [
'stocks',
{
"key": 'venue',
"includes": [
'-address'
]
}
]
@app.route('/offers', methods=['GET'])
def get_offers():
offers = Offer.query.all()
return jsonify(as_dict(offers, includes=offer_includes))
The success will return
[
{
"id": "AE",
"name": "MyOffer",
"stocks": [
{
"available": 10,
"id": "AE"
}
],
"venue": {
"name": "MyVenue"
}
}
]
Links
Documentation: https://sqlalchemy-api-handler.betagouv.fr/
Issue tracker: https://github.com/betagouv/sqlalchemy-api-handler/issues
Test status: https://travis-ci.org/betagouv/sqlalchemy-api-handler
Test coverage: https://codecov.io/gh/betagouv/sqlalchemy-api-handler
SQLAlchemy: https://www.sqlalchemy.org
Deploy
First, make sure that the deploy environment is started:
./sqlaah start
In a second tab, then:
Change the __version__ into sqlalchemy_api_handler/__init__.py
Pre publish:
./sqlaah prepublish
Publish:
./sqlaah publish
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
Hashes for SQLAlchemy-Api-Handler-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a231c788c17412e06f856e1a46876dff32ff0fc9d379d26c96898070b0c1c166 |
|
MD5 | 20518911b7b9b5c315b7523e57426fb5 |
|
BLAKE2b-256 | 4e482272374262b3311050c1a0332837ceab2c6af7a76de40941a28d410eba8c |
Hashes for SQLAlchemy_Api_Handler-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d350041f4004b6388434a56ccb375de3158545bc59fd93f82ba5f3bfa07c2628 |
|
MD5 | 6660346dca217933edc9fd3499fe8745 |
|
BLAKE2b-256 | 5122f7a87984c69419a10b323ba68251cf737874a90f0bd33b8e26be4f394d24 |