Skip to main content

Create Starlette Plugins easier

Project description

starlette-plugins – A helper to make plugins faster with Starlette

Starlette-Plugins is a helper to write plugins easier

Tests Status PYPI Version

Requirements

  • python >= 3.7

Installation

starlette-plugins should be installed using pip:

pip install starlette-plugins

Usage

Let’s imagine that we need to write Starlette plugins for Peewee ORM.

from aiopeewee import db_url

from starlette_plugins import StarlettePlugin

class Peewee(StarlettePlugin):

    # Give your plugin a name
    name = 'db'

    # Prepare a default configuration
    config = {
        'url': 'sqlite+async:///db.sqlite',
        'connection_params': {},
    }

def __init__(self, app=None, **settings):
    super(Peewee, self).__init__(app, **settings)
    self.database = None

def setup(self, app, **settings):
    """Setup the plugin."""
    super(Peewee, self).setup(app, **settings)
    self.database = db_url.connect(self.config.url, **self.config.connection_params)

async def middleware(self, scope, receive, send, app):
    """An optional ASGI middleware."""
    try:
        await self.database.connect_async()
        await app(scope, receive, send)
    finally:
        await self.database.close_async()

async def shutdown(self, scope):
    """ The methods are supported: `on_startup`, `on_shutdown`."""
    if hasattr(self.database, 'close_all'):
        self.database.close_all()

Use the plugin

from starlette.applications import Starlette

db = Peewee(url='postgres+async://database')
db.setup(app)

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/starlette-plugins/issues

Contributing

Development of the project happens at: https://github.com/klen/starlette-plugins

License

Licensed under a MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

starlette_plugins-0.0.6-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file starlette_plugins-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: starlette_plugins-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.0

File hashes

Hashes for starlette_plugins-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4bf06d05efa8bbfcc9f6bfe60222b2d04157aad15105d2d402979b47958091af
MD5 2367eb0e98aab9a827a4dba7ede5aec4
BLAKE2b-256 2ef956bceba255db3588beddbc5b33dde8f009ceec385e50e7a3b751086e2abf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page