Provides a more structured way of writing RESTful APIs with Chalice.
Project description
Chalice-RESTful
A more structured way of writing Chalice APIs. Very similar to Flask-RESTful.
Motivation
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)
Installation
Install Chalice-RESTful with pip
:
$ pip install chalice-restful
Usage
Resources
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)
HTTP Methods
Currently, Chalice-RESTful supports get
, post
, put
, patch
and delete
endpoints,
which can be defined in resources.
Authorization
You can add an authorization to resources or endpoints in several ways.
API key
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:
from chalice import Chalice
from chalice_restful import Api, Resource, api_key_required, route
app = Chalice('example')
api = Api(app)
@route('/v1/items')
class Items(Resource):
def get(): ...
@api_key_required
def post(): ...
@api_key_required
def put(): ...
api.add(Items)
Authorizers
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:
from chalice import Chalice, IAMAuthorizer
from chalice_restful import Api, Resource, authorizer, route
app = Chalice('example')
api = Api(app)
iam = IAMAuthorizer()
@route('/v1/items')
class Items(Resource):
def get(): ...
@authorizer(iam)
def post(): ...
@authorizer(iam)
def put(): ...
api.add(Items)
Read more about Chalice authorizers here.
CORS
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:
from chalice import Chalice
from chalice_restful import Api, Resource, cors, route
app = Chalice('example')
api = Api(app)
@route('/v1/items')
class Items(Resource):
def get(): ...
@cors
def post(): ...
@cors
def put(): ...
api.add(Items)
License
The package is licensed under the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file chalice-restful-1.0.0.tar.gz
.
File metadata
- Download URL: chalice-restful-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5eb7c0f353dfcfa8fbcc079bb43e49bd00cf0c8d8ce3dcbebb25094b41af3d2 |
|
MD5 | 4e71e131321271d226e80ec52546b95c |
|
BLAKE2b-256 | 4a0c672a82a1aad12507ce795cf26820f983173d7a059cef691bad94f7ecb233 |