Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/patrickmccallum/flask-prism
PRISM is the easy way to build manageable APIs for Flask
https://github.com/patrickmccallum/flask-prism
flask flask-api flask-prism json
Last synced: 3 days ago
JSON representation
PRISM is the easy way to build manageable APIs for Flask
- Host: GitHub
- URL: https://github.com/patrickmccallum/flask-prism
- Owner: patrickmccallum
- License: mit
- Created: 2015-10-05T05:54:09.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-10-15T11:52:08.000Z (almost 4 years ago)
- Last Synced: 2024-09-25T21:46:44.282Z (8 days ago)
- Topics: flask, flask-api, flask-prism, json
- Language: Python
- Homepage:
- Size: 55.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![alt text](http://flask-prism.readthedocs.io/en/latest/_static/prism_small.png "Flask-PRISM")
PRISM is a simple way to manage your Flask APIs, providing consistent models, version management, access control, and more while leaving you in full control of your code. It's even super easy to use.
Check out this Flask-SQLAlchemy model that's using PRISM:
```python
from prism import pclass User(db.Model):
# Define table name
__tablename__ = 'users'# Primary key, user ID
user_id = db.Column(db.Integer, primary_key=True, autoincrement=True, index=True)# First name
first_name = db.Column(db.String(255))
email = db.Column(db.String(255))# Password
password = db.Column(db.String(255))@p.api_representation()
def as_data(self):
return {
'id': self.user_id,
'email': self.email,
'first_name': self.first_name
}
```Now in our API let's set up a route to return all users. All we do is return our Flask-SQLAlchemy models through a PRISM **Refract**
```python
from flask import Blueprint
from models import User
from flask.ext.prism import Refractbp = Blueprint('api', __name__, url_prefix="/api")
@bp.route('/users')
def api_users_get():
# Return with as_list=True, this forces the JSON to be an array even if there's only one result.
return Refract(User.query.all(), as_list=True)```
Which returns a nice clean and easy output
```json
[
{
"user_id": 1,
"email": "[email protected]",
"first_name": "Roger"
},
{
"user_id": 2,
"email": "[email protected]",
"first_name": "Jennifer"
}
]
```Features
--------- Easily manage your JSON models from a predictable location
- Insert permission based rules into your JSON output
- Models and permission checks can be easily versioned by passing a ``version=INTEGER`` parameter### Installation
You can grab Flask-PRISM using pip (our Pypi page is [over here](https://pypi.python.org/pypi?name=Flask-PRISM&:action=display))
```sh
$ pip install Flask-PRISM
```Instantiate our **Prism** object in a separate file, in this example it's in ``prism.py``
```python
from flask.ext.prism import Prismp = Prism()
```And finally, call ``init_app()`` during setup.
```python
from flask import Flask
from flask.ext.prism import Refract
from prism import pfrom models import User
# Setup flask like we normally do
app = Flask(__name__)# Setup PRISM to see Flask
p.init_app(app)@app.route('/api/users/')
def api_user_get(user_id):
return Refract(User.query.get(user_id))if __name__ == '__main__':
app.run()
```Which returns a nice clean and easy output
```json
{
"user_id": 1,
"email": "[email protected]",
"first_name": "Roger"
}
```### Documentation
You can find all the Flask-PRISM documentation at [flask-prism.readthedocs.org](https://flask-prism.readthedocs.org/)
### Development
Want to contribute? Awesome!
If you've made a change you think will help other PRISM users, just open a pull request or raise an issue on GitHub.If you're not already there, it's over at
Didn't read the docs? My Twitter is [@patsnacks](https://twitter.com/patsnacks).
### Changelog
##### 0.4.0 BREAKING CHANGES
- Renamed ReturnableResponse to Refract, shorter, makes more sense given the context
- Fixed new line issue in response mapping##### 0.3.2
- Fixed an issue with the as_list parameter that could cause it to be ignored in certain cases
- Tweaked the docs##### 0.3.1
- Fixed issue that would cause deployments to fail with Apache's mod_wsgi, and subsequently AWS Elastic Beanstalk##### 0.3.0
- PRISM representations can now contain other objects with representations! They'll be auto converted on the way out
- Removed stray print statements used for debugging##### 0.2.2
- Added documentation##### 0.2.1
- Initial release### License
Flask-PRISM is distributed under the MIT license. See the LICENSE file included in this project for more.