JonasKs / starlite

Light, Flexible and Extensible ASGI API framework

Home Page:https://starlite-api.github.io/starlite/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Starlite logo

PyPI - License PyPI - Python Version

Coverage Vulnerabilities Quality Gate Status Maintainability Rating Reliability Rating Security Rating

Discord

Starlite

Starlite is a light, opinionated and flexible ASGI API framework built on top of pydantic and Starlette.

Check out the Starlite documentation πŸ“š

Core Features

  • πŸ‘‰ Class based controllers
  • πŸ‘‰ Decorators based configuration
  • πŸ‘‰ Extended testing support
  • πŸ‘‰ Extensive typing support including inference, validation and parsing
  • πŸ‘‰ Full async (ASGI) support
  • πŸ‘‰ Layered dependency injection
  • πŸ‘‰ OpenAPI 3.1 schema generation with Redoc UI
  • πŸ‘‰ Route guards based authorization
  • πŸ‘‰ Simple middleware and authentication
  • πŸ‘‰ Support for pydantic models and pydantic dataclasses
  • πŸ‘‰ Support for standard library dataclasses
  • πŸ‘‰ Ultra-fast json serialization and deserialization using orjson

Installation

Using your package manager of choice:

pip install starlite

OR

poetry add starlite

OR

pipenv install starlite

Minimal Example

Define your data model using pydantic or any library based on it (see for example ormar, beanie, SQLModel etc.):

from pydantic import BaseModel, UUID4


class User(BaseModel):
    first_name: str
    last_name: str
    id: UUID4

You can alternatively use a dataclass, either the standard library one or the one from pydantic:

from uuid import UUID

# from pydantic.dataclasses import dataclass
from dataclasses import dataclass

@dataclass
class User:
    first_name: str
    last_name: str
    id: UUID

Define a Controller for your data model:

from typing import List

from pydantic import UUID4
from starlite import Controller, Partial, get, post, put, patch, delete

from my_app.models import User


class UserController(Controller):
    path = "/users"

    @post()
    async def create_user(self, data: User) -> User:
        ...

    @get()
    async def list_users(self) -> List[User]:
        ...

    @patch(path="/{user_id:uuid}")
    async def partially_update_user(self, user_id: UUID4, data: Partial[User]) -> User:
        ...

    @put(path="/{user_id:uuid}")
    async def update_user(self, user_id: UUID4, data: User]) -> User]:
        ...

    @get(path="/{user_id:uuid}")
    async def get_user(self, user_id: UUID4) -> User:
        ...

    @delete(path="/{user_id:uuid}")
    async def delete_user(self, user_id: UUID4) -> User:
        ...

Import your controller into your application's entry-point and pass it to Starlite when instantiating your app:

from starlite import Starlite

from my_app.controllers.user import UserController

app = Starlite(route_handlers=[UserController])

To run you application, use an ASGI server such as uvicorn:

uvicorn my_app.main:app --reload

Contributing

Starlite is open to contributions big and small. You can always join our discord server to discuss contributions and project maintenance. For guidelines on how to contribute, please see the contribution guide.

About

Light, Flexible and Extensible ASGI API framework

https://starlite-api.github.io/starlite/

License:MIT License


Languages

Language:Python 100.0%