Build a REST API with Flask
Project description
Flask-RESTly
Features
- Decorators-based routing
- JSON and Protobuf built-in serialization
- Custom serializer support
- Automatic REST-like response codes
Todo
- HATEOAS
- ...and few more :)
Usage
Please see examples for more details.
Quick start
pip install flask-restly
By default flask-restly
uses JSON serializer.
from flask import Flask
from flask_restly import FlaskRestly
from flask_restly.decorator import resource, get, delete
app = Flask(__name__)
rest = FlaskRestly(app)
rest.init_app(app)
@resource(name='employees')
class EmployeesResource:
@get('/<id>')
def get_employee(self, id):
return dict(id=int(id))
@get('/')
def get_employees(self):
return dict(entites=[
dict(id=1),
dict(id=2)
])
@delete('/<id>')
def delete_employee(self, **kwargs):
return
with app.app_context():
EmployeesResource()
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5001, debug=True)
$ python main.py
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 210-167-642
* Running on http://127.0.0.1:5001/ (Press CTRL+C to quit)
Protobuf
# employee.proto
syntax = "proto3";
message Employee {
int32 id = 1;
string name = 2;
}
from flask import Flask
from flask_restly import FlaskRestly
from flask_restly.decorator import resource, get, body, post
from flask_restly.serializer import protobuf
from employee_pb2 import Employee
app = Flask(__name__)
app.config['RESTLY_SERIALIZER'] = protobuf
rest = FlaskRestly(app)
rest.init_app(app)
@resource(name='employees')
class EmployeesResource:
@get('/<int:id>')
@body(Employee)
def get_employee(self, id):
return dict(id=id, name="some name")
@post('/')
@body(Employee, Employee)
# @body(outgoing=Employee, incoming=Employee)
def create_employee(self, body):
# print(body)
return dict(id=1, name=body.get('name'))
with app.app_context():
EmployeesResource()
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5001, debug=True)
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
flask-restly-0.3.0.tar.gz
(7.2 kB
view hashes)
Built Distribution
Close
Hashes for flask_restly-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8df7a3b206a6491ed6e471c88c8c1732c5dc86078e85c7f21b8a2ae9a09ca7b |
|
MD5 | 8d70552ec73611557254689e213e2c7c |
|
BLAKE2b-256 | 24e1d0a1bb762a369d04df07f2e1ef47029f49f2744c7ddfe6f3dc2aff718b82 |