StarMallow framework
Project description
StarMallow
StarMallow is a starlette based web framework for building robust APIs with Python and Marshmallow. It's been inspired by FastAPI, but uses Marshmallow instead of Pydantic for it's schema definitions. It's reason for existing is simply because marshmallow is a much more powerful serialization engine, and in most cases it's fast enough. An example of Pydantic's limitations can be found in Issue 2277
Example
Create it
Create a file main.py
with:
from typing import Annotated
from marshmallow_dataclass import dataclass
from starmallow import Body, Path, StarMallow
app = StarMallow()
# Minimum example
@app.get("/path/{item_id}")
def get_id(item_id):
return item_id
# Example with explicit location option
@dataclass
class MyBody:
item_id: int
sub_item_id: int
@app.get("/body")
async def get_body(body: MyBody = Body()) -> int:
return body.item_id
# Example with explicit marshmallow schema
class MyBodySchema(ma.Schema):
item_id = mf.Integer()
@app.get("/path/body_schema")
def get_body_from_schema(body: Dict[str, int] = Body(model=MyBodySchema)) -> int:
return body['item_id']
# Example with Annotated
@app.get("/body_annotated")
async def get_body_annotated(body: Annotated[MyBody, Body()]) -> int:
return body.item_id
Run it
Run the server with:
❯ uvicorn sample_server:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [84092]
INFO: Started server process [87944]
INFO: Waiting for application startup.
INFO: Application startup complete.
View the docs
- Open the browser at http://localhost:8000/docs to view the Swagger UI.
- Open the browser at http://localhost:8000/redoc to view the Redoc UI.
HTTP Endpoints
You can also use class-based views. This can make it easier to organize your code and gives you an easy migration path if you use flask-smorest
from marshmallow_dataclass import dataclass
from starmallow import StarMallow
from starmallow.decorators import route
from starmallow.endpoints import APIHTTPEndpoint
app = StarMallow()
@dataclass
class Pet:
name: str
@app.api_route('/')
class Pets(APIHTTPEndpoint):
def get(self) -> Pet:
return Pet.get()
def post(self, pet: Pet) -> Pet:
pet.create()
return pet
@app.api_route('/id/{pet_id}')
class PetById(ApiHttpEndpoint):
@route(deprecated=True) # Specify @route if you need to override parameters
def get(self, pet_id: int) -> Pet:
return Pet.get(pet_id)
@route(status_code=204) # Specify @route if you need to override parameters
def get(self, pet_id: int):
Pet.get(pet_id).delete()
Optional Dependencies
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 starmallow-0.6.1.tar.gz
.
File metadata
- Download URL: starmallow-0.6.1.tar.gz
- Upload date:
- Size: 801.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.24.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a561f2421b7a1535c24b1856f7ea852b4e068ff470d92a9b08bda8af4f265716 |
|
MD5 | d5ea94426d77cb3cd2e239f7049309af |
|
BLAKE2b-256 | 768a2b4a841172f8674a64653fc160c611249b4c72f78c34394c9c7f57059044 |
File details
Details for the file starmallow-0.6.1-py3-none-any.whl
.
File metadata
- Download URL: starmallow-0.6.1-py3-none-any.whl
- Upload date:
- Size: 56.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.24.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2bd6cf4bb0fc601a8e171cfe49c942a1fac3966649f1a0bf2d6b72a440082ba |
|
MD5 | 34d475ba7af22a7f55b088100c755070 |
|
BLAKE2b-256 | 662291dc75ca90c9b17ab71181f26f8f120f25a0735f023dde0bb7247ccdc101 |