Package: chalice-restful
Chalice-RESTfulA more structured way of writingChaliceAPIs.
Very similar toFlask-RESTful.MotivationIn pureChaliceyou only need to define plain functions
to handle routed HTTP-requests:app=Chalice('example')@app.route('/v1/items',methods=['GET'])defget_items():...@app.route('/v1/items',methods=['POST'])defadd_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'])defget_items():...@app.route('/v1/items',methods=['POST'])defadd_item():......@app.route('/v1/orders',methods=['GET'])defget_orders():...@app.route('/v1/orders',methods=['POST'])defadd_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 isexactlywhat the Chalice-RESTful for:app=Chalice('example')api=Api(app)@route('/v1/items')classItems(Resource):defget():...defpost():...@route('/v1/orders')classOrders(Resource):defget():...defpost():...api.add(Items)api.add(Orders)InstallationInstall Chalice-RESTful withpip:$pipinstallchalice-restfulUsageResourcesAn atomic block of Chalice-RESTful is aresource: any subclass of theResourcethat hasrouteattribute and at least one endpoint (get,post,put, etc.).
All resources should be then added to theApiobject that will take care of registering
each individual endpoint in theChaliceinstance.fromchaliceimportChalicefromchalice_restfulimportApi,Resourceapp=Chalice('example')api=Api(app)classItems(Resource):route='/v1/items'defget():...api.add(Items)It's worth mentioning that you can use configuration decoratorroutethat will addrouteattribute automatically while preserving pretty decorators syntax:fromchaliceimportChalicefromchalice_restfulimportApi,Resource,routeapp=Chalice('example')api=Api(app)@route('/v1/items')classItems(Resource):defget():...api.add(Items)HTTP MethodsCurrently, Chalice-RESTful supportsget,post,put,patchanddeleteendpoints,
which can be defined in resources.AuthorizationYou can add an authorization to resources or endpoints in several ways.API keyTo require an API key useapi_key_requiredconfiguration decorator:fromchaliceimportChalicefromchalice_restfulimportApi,Resource,api_key_required,routeapp=Chalice('example')api=Api(app)@route('/v1/items')@api_key_requiredclassItems(Resource):defget():...api.add(Items)You can decorate individual endpoints as well:fromchaliceimportChalicefromchalice_restfulimportApi,Resource,api_key_required,routeapp=Chalice('example')api=Api(app)@route('/v1/items')classItems(Resource):defget():...@api_key_requireddefpost():...@api_key_requireddefput():...api.add(Items)AuthorizersTo add an authorizer instance useauthorizerconfiguration decorator:fromchaliceimportChalice,IAMAuthorizerfromchalice_restfulimportApi,Resource,authorizer,routeapp=Chalice('example')api=Api(app)iam=IAMAuthorizer()@route('/v1/items')@authorizer(iam)classItems(Resource):defget():...api.add(Items)You can decorate individual endpoints as well:fromchaliceimportChalice,IAMAuthorizerfromchalice_restfulimportApi,Resource,authorizer,routeapp=Chalice('example')api=Api(app)iam=IAMAuthorizer()@route('/v1/items')classItems(Resource):defget():...@authorizer(iam)defpost():...@authorizer(iam)defput():...api.add(Items)Read more about Chalice authorizershere.CORSTo enable CORS usecorsconfiguration decorator:fromchaliceimportChalicefromchalice_restfulimportApi,Resource,cors,routeapp=Chalice('example')api=Api(app)@route('/v1/items')@corsclassItems(Resource):defget():...api.add(Items)You can decorate individual endpoints as well:fromchaliceimportChalicefromchalice_restfulimportApi,Resource,cors,routeapp=Chalice('example')api=Api(app)@route('/v1/items')classItems(Resource):defget():...@corsdefpost():...@corsdefput():...api.add(Items)LicenseThe package is licensed under theMITlicense.