Package: apistar-crud
API Star CRUDVersion:0.6.6Status:Production/StableAuthor:José Antonio Perdiguero LópezTable of ContentsAPI Star CRUDFeaturesResourceORMAdmin siteQuick startUsageSQLAlchemyPeeweeResourcesRoutesOverride methodsFilteringAdminMain pageResource listResource detailFeaturesAPI Star CRUD provides an easy way to define a REST resource and generic operations over it.ResourceThe resources are classes with a default implementation formethods:create: Create a new element for this resource.retrieve: Retrieve an element of this resource.update: Update (partially or fully) an element of this resource.delete: Delete an element of this resource.list: List resource collection withfilteringcapabilities.drop: Drop resource collection.ORMAPI Star CRUD supports the following ORM:SQLAlchemythroughapistar-sqlalchemy.Peeweethroughapistar-peewee-orm.Admin siteAPI Star CRUD serves an admin site to handle resources in a graphical way, by default this site is routed to/admin.Quick startInstall API Star CRUD:$pipinstallapistar-crud[peewee]or$ pip install apistar-crud[sqlalchemy]Follow the steps:Create aninput typeandoutput typefor your resource:Define amodelbased on your ORM.Build yourresourceusing the metaclass specific for your ORM.Add theroutesfor your resources.UsageSQLAlchemyExample of a fully functional resource based on SQLAlchemy.Create aninput typeandoutput type:classPuppyInputType(types.Type):name=validators.String()classPuppyOutputType(types.Type):id=validators.Integer()name=validators.String()Define amodel:classPuppyModel(Base):__tablename__="Puppy"id=Column(Integer,primary_key=True)name=Column(String)Theresource:fromapistar_crud.resource.sqlalchemyimportResourceclassPuppyResource(metaclass=Resource):name="puppy"verbose_name="Puppy"model=PuppyModelinput_type=PuppyInputTypeoutput_type=PuppyOutputTypemethods=("create","retrieve","update","delete","list","drop")The resource generates his ownroutes:fromapistarimportAppfromapistar_crud.routesimportroutesasresource_routesresource_routes.register(PuppyResource,"/puppy",admin=True)routes=[# ... your app routes]routes+=resource_routes.routes(admin="/admin")packages=("apistar_crud",)app=App(routes=routes,packages=packages)PeeweeExample of a fully functional resource based on Peewee.Create aninput typeandoutput type:classPuppyInputType(types.Type):name=validators.String()classPuppyOutputType(types.Type):id=validators.Integer()name=validators.String()Define amodel:classPuppyModel(peewee.Model):name=peewee.CharField()Theresource:fromapistar_crud.resource.peeweeimportResourceclassPuppyResource(metaclass=Resource):name="puppy"verbose_name="Puppy"model=PuppyModelinput_type=PuppyInputTypeoutput_type=PuppyOutputTypemethods=("create","retrieve","update","delete","list","drop")The resource generates his ownroutes:fromapistarimportAppfromapistar_crud.routesimportroutesasresource_routesresource_routes.register(PuppyResource,"/puppy",admin=True)routes=[# ... your app routes]routes+=resource_routes.routes(admin="/admin")packages=("apistar_crud",)app=App(routes=routes,packages=packages)ResourcesRoutesThe routes for resource methods are:MethodVerbURLcreatePOST/retrieveGET/{element_id}/updatePUT/{element_id}/deleteDELETE/{element_id}/listGET/dropDELETE/Override methodsTo customize CRUD methods you can override them like:fromapistar_crud.resource.peeweeimportResourceclassPuppyResource(metaclass=Resource):name="puppy"verbose_name="Puppy"model=PuppyModelinput_type=PuppyInputTypeoutput_type=PuppyOutputTypemethods=("create","retrieve","update","delete","list","drop")@classmethoddefcreate(cls,element:PuppyInputType)->PuppyOutputType:# Do your custom processFilteringDefaultlistimplementation has filtering capabilities but to reflect it in the schema is necessary to override that
method defining all the parameters that will be used as a filter and pass it as keywords to_list()method.It is a direct translation to ORM keyword arguments
so in case of SQLAlchemy it uses afilter_by()method and awhere()in Peewee case.importtypingfromapistarimporthttpfromapistar_crud.resource.peeweeimportResourcefromapistar_paginationimportPageNumberResponseclassPuppyResource(metaclass=Resource):name="puppy"verbose_name="Puppy"model=PuppyModelinput_type=PuppyInputTypeoutput_type=PuppyOutputTypemethods=("create","retrieve","update","delete","list","drop")@classmethoddeflist(cls,name:http.QueryParam,page:http.QueryParam=None,page_size:http.QueryParam=None)->typing.List[PuppyOutputType]:returnPageNumberResponse(page=page,page_size=page_size,content=cls._filter(name=name))AdminAPI Star CRUD provides an admin panel to manage resources, including filtering, editing and creating new entries in your
resources.To include admin panel in your application you simply has to add the routes as following:fromapistarimportAppfromapistar_crud.routesimportroutesasresource_routesresource_routes.register(PuppyResource,"/puppy",admin=True)routes=[# ... your app routes]routes+=resource_routes.routes(admin="/admin")packages=("apistar_crud",)app=App(routes=routes,packages=packages)Main pageMain page showing a list of manageable resources.Resource listConfigurable view of a list of resource entries. If that resource provides filtering parameters for list method, a
filter input will be available. The list of columns shown is also configurable.Resource detailPage for editing a resource entry or creating a new one.