Skip to main content

Chalice-RESTful

Build PyPI version

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.

Release files for chalice-restful 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chalice-restful 1.0.0
File Size Uploaded
chalice-restful-1.0.0.tar.gz 7.0 kB Details

Release files / chalice-restful-1.0.0.tar.gz

Download URL chalice-restful-1.0.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e5eb7c0f353dfcfa8fbcc079bb43e49bd00cf0c8d8ce3dcbebb25094b41af3d2
BLAKE2b-256 checksum
How to use checksums
4a0c672a82a1aad12507ce795cf26820f983173d7a059cef691bad94f7ecb233
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page