Skip to main content

Starlette Fancy

We add two more layer on top of Starlette: "validator" and "processor". Validators are responsible for data validations and processors do logic of the web app.

Installation

To install base package:

pip install starlette-fancy

we use "pydantic" package for "PydanticValidator" and "databases" package for "DatabaseProcessor" and to support different JSON libraries we use "starlette-marshal" package. You can install each with following commands:

pip install starlette-fancy[pydantic]
pip install starlette-fancy[databases]
pip install starlette-fancy[starlette-marshal]

to install all packages at once:

pip install starlette-marshal[full]

Validators

In validator layer we receive data from a request then validate them and return validated data. Input data can come from request body, querystring and sometimes in could be response body.

We don't specify any library or method to validate data. One may create its own validator from the scratch. All you need to do is to inherit from "Validator" class and then implement its __call__ method.

an example could be like this:

class CustomValidator(Validator):
    async def __call__(
            self,
            data: Union[list, dict],
            source: Literal["body", "query_params"] = "body",
            response: bool = False,
            partial: bool = False,
    ) -> dict:
        ...

PydanticValidator

There is built in validator that powered with Pydantic library. First you have to create models:

class PostParams(BaseModel):
    title: str
    slug: str
    content: str
    published_at: datetime


class PostBody(BaseModel):
    id: Optional[UUID]

now we can have a simple validator like this:

class PostValidator(PydanticValidator):
    params_model = PostParams
    request_model = PostBody

Processors

Processor layer is after validator, and it receives validated data. In this layer we write logic of our program, and we return the processed. You should inherit from "Processor" class and then override one of "get", "post", "put", "delete" or "patch" methods. Here an example:

class CustomProcessor(Processor):
    async def get(self, validated_data: dict) -> Any:
        return {
            'validated_data': validated_data,
        }

DatabaseProcessor

A common scenario is when we want to map an HTTP method to a database query. Suppose we have the following queries:

INSERT_POSTS: str = '''
    insert into posts (title, slug, content, published_at)
    values (:title, :slug, :content, :published_at)
    returning *
'''

SELECT_POSTS: str = '''
    select id, title, slug, content, published_at
    from posts
    where true
'''

UPDATE_POSTS: str = '''
    update posts
    set title        = coalesce(:title, title),
        slug         = coalesce(:slug, slug),
        content      = coalesce(:content, content),
        published_at = coalesce(:published_at, published_at)
    where id = :id
    returning *
'''

DELETE_POSTS: str = '''
    delete
    from posts
    where id = :id
    returning *
'''

now a DatabaseProcessor example is like this:

database: Database = Database(url=DATABASE_URL)


class PostProcessor(DatabaseProcessor):
    database = database
    insert_query = INSERT_POSTS
    select_query = SELECT_POSTS
    update_query = UPDATE_POSTS
    delete_query = DELETE_POSTS

Endpoints

This isn't a new thing, just normal Starlette endpoint. But we only handle request and response in this layer. We get request and pass the values to validator then receive validated data and give it to processor layer. At the end we create a response with what processor returned to us.

Here an example:

class CustomEndpoint(Endpoint):
    validator: Validator
    processor: Processor

    async def get(self, request: Request) -> Response:
        query_params: dict = dict(request.query_params)

        validated_data: dict = await self.validator(
            data=query_params,
            source="query_params",
        )
        processed_data: list = await self.processor.get(
            validated_data=validated_data,
        )

        return JSONResponse(content=processed_data)

CRUDEndpoint

To create a fast CRUD endpoint you can use this class. With "PydanticValidator" and "DatabaseProcessor" it's really simple:

class PostEndpoint(CRUDEndpoint):
    validator = PostValidator()
    processor = PostProcessor()

there's a function to generate routes for our CRUD resource:

routes = generate_crud_routes('/posts', endpoint=PostEndpoint, id_type='uuid')

TODO

  • Pydantic validator
  • marshmallow validator
  • ORM processor
  • database processor
  • CRUD endpoint
  • gRPC endpoint
  • proxy endpoint

Metadata

Release files for starlette-fancy 0.1.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 starlette-fancy 0.1.0
File Size Uploaded Provenance
starlette-fancy-0.1.0.tar.gz 11.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for starlette-fancy 0.1.0
File Interpreter ABI Platform
starlette_fancy-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.1 kB

Release files / starlette-fancy-0.1.0.tar.gz

Download URL starlette-fancy-0.1.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
07ba351474b37cbf5e6f4d32286bd38bfa54c76813e77e6dadfbb37865ad834c
BLAKE2b-256 checksum
How to use checksums
f7b293498e816446c6176fd6539922ee0d3a903eeece727ba81281a683c8e96f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / starlette_fancy-0.1.0-py3-none-any.whl

Download URL starlette_fancy-0.1.0-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cbc355ff4509a9422f3adb0fbc0c089433b174ca26d329f368f3b9b805f414bb
BLAKE2b-256 checksum
How to use checksums
7e0a44977d7d72d046a78bca8602d4c29221ed6652f6485ae30c178ff2c2f591
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Security for starlette-fancy 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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