A more structured way of writing Chalice APIs. Very similar to Flask-RESTful.
In pure Chalice you only need to define plain functions to handle routed HTTP-requests:
app = Chalice('example')
@app.route('/v1/items', methods=['GET'])
def get_items(): ...
@app.route('/v1/items', methods=['POST'])
def add_item(): ...
This is great for building any kind of API, but in REST we deal with higher-level objects –– resources, where each represents a set of functions related to each other.
Though we still limited to write them in one place:
app = Chalice('example')
@app.route('/v1/items', methods=['GET'])
def get_items(): ...
@app.route('/v1/items', methods=['POST'])
def add_item(): ...
...
@app.route('/v1/orders', methods=['GET'])
def get_orders(): ...
@app.route('/v1/orders', methods=['POST'])
def add_order(): ...
It'd be much better if we could use an object-oriented approach, so closely related functions will be grouped together in a class.
And this is exactly what the Chalice-RESTful for:
app = Chalice('example')
api = Api(app)
@route('/v1/items')
class Items(Resource):
def get(): ...
def post(): ...
@route('/v1/orders')
class Orders(Resource):
def get(): ...
def post(): ...
api.add(Items)
api.add(Orders)
Install Chalice-RESTful with pip
:
$ pip install chalice-restful
An atomic block of Chalice-RESTful is a resource: any subclass of the Resource
that has route
attribute and at least one endpoint (get
, post
, put
, etc.).
All resources should be then added to the Api
object that will take care of registering
each individual endpoint in the Chalice
instance.
from chalice import Chalice
from chalice_restful import Api, Resource
app = Chalice('example')
api = Api(app)
class Items(Resource):
route = '/v1/items'
def get(): ...
api.add(Items)
It's worth mentioning that you can use configuration decorator route
that will add
route
attribute automatically while preserving pretty decorators syntax:
from chalice import Chalice
from chalice_restful import Api, Resource, route
app = Chalice('example')
api = Api(app)
@route('/v1/items')
class Items(Resource):
def get(): ...
api.add(Items)
Currently, Chalice-RESTful supports get
, post
, put
, patch
and delete
endpoints,
which can be defined in resources.
You can add authorization to resources or endpoints in several ways.
To require an API key, use api_key_required
configuration decorator:
from chalice import Chalice
from chalice_restful import Api, Resource, api_key_required, route
app = Chalice('example')
api = Api(app)
@route('/v1/items')
@api_key_required
class Items(Resource):
def get(): ...
api.add(Items)
You can decorate individual endpoints as well:
...
@route('/v1/items')
class Items(Resource):
def get(): ...
@api_key_required
def post(): ...
@api_key_required
def put(): ...
...
To add an authorizer instance, use authorizer
configuration decorator:
from chalice import Chalice, IAMAuthorizer
from chalice_restful import Api, Resource, authorizer, route
app = Chalice('example')
api = Api(app)
iam = IAMAuthorizer()
@route('/v1/items')
@authorizer(iam)
class Items(Resource):
def get(): ...
api.add(Items)
You can decorate individual endpoints as well:
...
iam = IAMAuthorizer()
@route('/v1/items')
class Items(Resource):
def get(): ...
@authorizer(iam)
def post(): ...
@authorizer(iam)
def put(): ...
...
Read more about Chalice authorizers here.
To enable CORS, use cors
configuration decorator:
from chalice import Chalice
from chalice_restful import Api, Resource, cors, route
app = Chalice('example')
api = Api(app)
@route('/v1/items')
@cors
class Items(Resource):
def get(): ...
api.add(Items)
You can decorate individual endpoints as well:
...
@route('/v1/items')
class Items(Resource):
def get(): ...
@cors
def post(): ...
@cors
def put(): ...
...
The package is licensed under the MIT license.