API Star tools to create CRUD resources.
Project description
API Star CRUD
- Version: 0.6.6
- Status: Production/Stable
- Author: José Antonio Perdiguero López
Table of Contents
Features
API Star CRUD provides an easy way to define a REST resource and generic operations over it.
Resource
The resources are classes with a default implementation for methods:
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 with filtering capabilities.drop
: Drop resource collection.
ORM
API Star CRUD supports the following ORM:
- SQLAlchemy through apistar-sqlalchemy.
- Peewee through apistar-peewee-orm.
Admin site
API Star CRUD serves an admin site to handle resources in a graphical way, by default this site is routed to /admin
.
Quick start
Install API Star CRUD:
$ pip install apistar-crud[peewee]
or
$ pip install apistar-crud[sqlalchemy]
Follow the steps:
- Create an input type and output type for your resource:
- Define a model based on your ORM.
- Build your resource using the metaclass specific for your ORM.
- Add the routes for your resources.
Usage
SQLAlchemy
Example of a fully functional resource based on SQLAlchemy.
Create an input type and output type:
class PuppyInputType(types.Type):
name = validators.String()
class PuppyOutputType(types.Type):
id = validators.Integer()
name = validators.String()
Define a model:
class PuppyModel(Base):
__tablename__ = "Puppy"
id = Column(Integer, primary_key=True)
name = Column(String)
The resource:
from apistar_crud.resource.sqlalchemy import Resource
class PuppyResource(metaclass=Resource):
name = "puppy"
verbose_name = "Puppy"
model = PuppyModel
input_type = PuppyInputType
output_type = PuppyOutputType
methods = ("create", "retrieve", "update", "delete", "list", "drop")
The resource generates his own routes:
from apistar import App
from apistar_crud.routes import routes as resource_routes
resource_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)
Peewee
Example of a fully functional resource based on Peewee.
Create an input type and output type:
class PuppyInputType(types.Type):
name = validators.String()
class PuppyOutputType(types.Type):
id = validators.Integer()
name = validators.String()
Define a model:
class PuppyModel(peewee.Model):
name = peewee.CharField()
The resource:
from apistar_crud.resource.peewee import Resource
class PuppyResource(metaclass=Resource):
name = "puppy"
verbose_name = "Puppy"
model = PuppyModel
input_type = PuppyInputType
output_type = PuppyOutputType
methods = ("create", "retrieve", "update", "delete", "list", "drop")
The resource generates his own routes:
from apistar import App
from apistar_crud.routes import routes as resource_routes
resource_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)
Resources
Routes
The routes for resource methods are:
Method | Verb | URL |
---|---|---|
create | POST | / |
retrieve | GET | /{element_id}/ |
update | PUT | /{element_id}/ |
delete | DELETE | /{element_id}/ |
list | GET | / |
drop | DELETE | / |
Override methods
To customize CRUD methods you can override them like:
from apistar_crud.resource.peewee import Resource
class PuppyResource(metaclass=Resource):
name = "puppy"
verbose_name = "Puppy"
model = PuppyModel
input_type = PuppyInputType
output_type = PuppyOutputType
methods = ("create", "retrieve", "update", "delete", "list", "drop")
@classmethod
def create(cls, element: PuppyInputType) -> PuppyOutputType:
# Do your custom process
Filtering
Default list
implementation 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 a filter_by()
method and a where()
in Peewee case.
import typing
from apistar import http
from apistar_crud.resource.peewee import Resource
from apistar_pagination import PageNumberResponse
class PuppyResource(metaclass=Resource):
name = "puppy"
verbose_name = "Puppy"
model = PuppyModel
input_type = PuppyInputType
output_type = PuppyOutputType
methods = ("create", "retrieve", "update", "delete", "list", "drop")
@classmethod
def list(cls, name: http.QueryParam, page: http.QueryParam=None, page_size: http.QueryParam=None) -> typing.List[PuppyOutputType]:
return PageNumberResponse(page=page, page_size=page_size, content=cls._filter(name=name))
Admin
API 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:
from apistar import App
from apistar_crud.routes import routes as resource_routes
resource_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 page
Main page showing a list of manageable resources.
Resource list
Configurable 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 detail
Page for editing a resource entry or creating a new one.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file apistar-crud-0.6.6.tar.gz
.
File metadata
- Download URL: apistar-crud-0.6.6.tar.gz
- Upload date:
- Size: 2.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.11.5 CPython/3.7.0 Linux/4.18.9-arch1-1-ARCH
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8325a48599e5bd4bf722b2111daf8ea890160ec4811dfdca137808b6f0df5c04 |
|
MD5 | ecbbaeb497d16dd7f686dbebfe0c1ee8 |
|
BLAKE2b-256 | 556257569bfaa48d9f4b545febbcd369a4ad38f32476130ece6497f70a258bf9 |
File details
Details for the file apistar_crud-0.6.6-py3-none-any.whl
.
File metadata
- Download URL: apistar_crud-0.6.6-py3-none-any.whl
- Upload date:
- Size: 13.7 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.11.5 CPython/3.7.0 Linux/4.18.9-arch1-1-ARCH
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea3ff859499b30f812a6f689315e4068f4e80596fcb95078b6b65ccbf034e1d6 |
|
MD5 | acf2655f5c5de69e8ec83a98cb4d9652 |
|
BLAKE2b-256 | ac195770fd069700032ab9bbe4dcde2734bea97634a58d0b4debcdbe4f91fb68 |