Package: restful-ben
RESTful BenBen's had a nap, he's feeling RESTful and ready to go.A libray to assist creatingSQLAlchemy,Flask, andflask-restfulbased APIs.FeaturesRESTful resourcesGenerates POST, GET (individual and list), PUT, and DELETE endpoints based on a SQLAlchemy model and aMarshmallowschema.Query engineField selectionFilteringSortingPaginationAuthenticationUsername and password based sessionsCSRFSession endpoint - login (POST) and logout (DELETE)AuthorizationBasic role based authorizationRoles map to HTTP verbs (GET, POST, etc)UsageBasic APICreate a modelclassCat(BaseModel):__tablename__='cats'id=Column(Integer,primary_key=True)name=Column(String,nullable=False)pattern=Column(String)age=Column(Integer)created_at=Column(DateTime,nullable=False,server_default=func.now())updated_at=Column(DateTime,nullable=False,server_default=func.now(),onupdate=func.now())Create aMarshmallowschema to map a model to a JSON representation. This usesMarshmallow SQLAlchemyto generate the schema automatically.classCatSchema(ModelSchema):classMeta:model=Catid=field_for(Cat,'id',dump_only=True)created_at=field_for(Cat,'created_at',dump_only=True)updated_at=field_for(Cat,'updated_at',dump_only=True)cat_schema=CatSchema()cats_schema=CatSchema(many=True)Create a resource for single Cat access, eg/cats/:idclassCatResource(RetrieveUpdateDeleteResource):single_schema=cat_schemamodel=Catsession=db.sessionCreate a resource for listing Cats, eg/cats.classCatListResource(QueryEngineMixin,CreateListResource):single_schema=cat_schemamany_schema=cats_schemamodel=Catsession=db.sessionSetup your flask app:importflaskfromflask_sqlalchemyimportSQLAlchemyfromflask_restfulimportApifrom.routesimportCatListResource,CatResourcedb=SQLAlchemy()app=flask.Flask(__name__)app.config['SQLALCHEMY_DATABASE_URI']='some db'db.init_app(app)api=Api(app)withapp.app_context():db.create_all()api.add_resource(CatListResource,'/cats')api.add_resource(CatResource,'/cats/<int:instance_id>')Query EngineFilteringTo filter based on equality simple use the field name plus filter value for one or more fields, ex/cats?pattern=Tabby. Other operations are available by adding an operator at the end of the field name separated by two underscores, ex/cats?pattern__contains=Tabby.OperatorsOperatorDescriptionNotes / ExampleeqEquals - default/cats?pattern=Tabbyor/cats?pattern__eq=TabbyneNot Equals (!=)/cats?pattern__ne=TabbyltLess Than (<)lteLess Than or Equal To (<=)gtGreater Than (>)gteGreater Than or Equal To (>=)containsContainslikeLikeilikeCase Insensitive LikenotlikeNot LikenotilikeCase Insensitive Not LikestartswithStarts WithendswithEnds WithinIn/cats?name__in=Ada&name__in=LeonotinNot In/cats?name__notin=Ada&name__notin=LeoisIS - Helper fornullandtrue/false/cats?age__is=nullor/users?active__is=trueor/users?active__is=falseisnotIS NOT - Helper fornullandtrue/false/cats?age__isnot=nullOrderingUse the$order_byquery parameter to set ordering by one or more fields. Fields are separated by a comma (,) and are ascending by default. Add a minus to the beginning of the field to order by descending.Examples/cats?$order_by=name/cats?$order_by=pattern,-updated_atField selectionUse the$fieldsquery parameter to select a subset of fields. Fields are comma (,) separated.Examples/cats?$fields=id/cats?$fields=id,namePaginationUse the$pageand$page_sizequery parameters to paginate.$page_sizeis not required and is 100 by default.Examples/cats?$page=2/cats?$page=2&$page_size=10