Marshmallow API Utilities
A collection of tools for REST API development with Marshmallow.
| Package | |
| Meta |
Basic usage
from datetime import date
from flask import Flask
from flask_smorest import Api
from marshmallow_dataclass import dataclass as ma_dataclass
from sqlalchemy import Column, Select, select
from sqlalchemy.orm import DeclarativeBase
from marshmallow_api_utils.fields import dump_only_field, optional_field, required_field
from marshmallow_api_utils.ma_dataclass import MaDataclass
from marshmallow_api_utils.models.sortable import Sortable
class Base(DeclarativeBase):
pass
class Pet(Base):
__tablename__ = 'pets'
id = Column(st.Integer, primary_key=True)
name = Column(st.String)
birthdate = Column(st.Date)
@ma_dataclass
class PetDTO(MaDataclass):
id: int = dump_only_field()
name: str = required_field(help='This will show up in the OpenAPI docs.')
birthdate: dt.date = optional_field()
age: int = dump_only_field(sortable=False)
@ma.pre_dump
def add_age(self, data, many, **kwargs):
data['age'] = int((date.today() - data.birthdate) / 365.25)
return data
@ma_dataclass
class PetQueryParams(Sortable, MaDataclass):
class Meta:
dto_schema = PetDTO.Schema()
flask = Flask(__name__)
app.config["API_TITLE"] = "My API"
app.config["API_VERSION"] = "v1"
app.config["OPENAPI_VERSION"] = "3.0.2"
api = Api(app)
blp = Blueprint("pets", "pets", url_prefix="/pets", description="Operations on pets")
@blp.route("/")
class Pets(MethodView):
@blp.arguments(PetQueryParams.Schema, location="query")
@blp.response(200, Pet.Schema(many=True))
def get(self, query_params: PetQueryParams):
"""List pets"""
stmt = select(Pet)
stmt = query_params.apply_sort(stmt)
return stmt.all()
License
Marshmallow API Utilities is distributed under the terms of the MIT license.
Metadata
Release files for marshmallow_api_utils 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| marshmallow_api_utils-0.2.5.tar.gz | 15.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| marshmallow_api_utils-0.2.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.9 kB
Release files / marshmallow_api_utils-0.2.5.tar.gz
| Download URL | marshmallow_api_utils-0.2.5.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
06d66eefad265f8f7063696e26553a5a2b247a54812972537533801ad1fed3da
|
|
BLAKE2b-256 checksum How to use checksums |
2004a5ddd8e32c2b279632ae9606ae39bc23c5491b24181c0c7b507eb4dbdc90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.26.0
|
Release files / marshmallow_api_utils-0.2.5-py3-none-any.whl
| Download URL | marshmallow_api_utils-0.2.5-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c10e5f4130da7a0f10c377f5e0564e7be0aee7caed13e46de5faf98f9e8ffd0
|
|
BLAKE2b-256 checksum How to use checksums |
325faff96f8ddf02b0b86ca7f49a629eac6086806cc3850d4bb8905588f28a3c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.26.0
|