Skip to main content

Better parameters for APIStar (and pydantic support)

Project description

Better handler parameters for APIStar (and pydantic support).

General usage is working as expected but documentation and OpenAPI schema aren’t done yet.

$ pip install apistar-pydantic

Usage

from apistar import App
from pydantic import BaseModel
from apistar_pydantic import (
    QueryParam, PathParam, DictBodyData, DictQueryData,
    PydanticBodyData as BodyData,
    PydanticQueryData as QueryData,
    Route, components,
)


#
# Declare models
#

class City(BaseModel):
    """City info"""
    name: str
    population: int

class Computer(BaseModel):
    """Computer info"""
    model: str
    price: float

class ComputerCity(City, Computer):
    """A computer in a city"""


#
# Create views
#

def resource_complete(param1: QueryParam[str],
                    param2: QueryParam[int],
                    param3: DictBodyData[dict],
                    param4: DictQueryData[dict]):
    return repr(locals())

def resource_query(city: QueryData[City]):
    return "%s has %d citizens." % (city.name, city.population)

def resource_body(computer: BodyData[Computer]):
    return "%s costs R$ %.2f" % (computer.model, computer.price)

def resource_mixed(city: QueryData[City],
                computer: BodyData[Computer]):
    return ComputerCity(**city.dict(), **computer.dict())


#
# Start the app
#

app = App(
    routes=[
        Route('/resource', 'GET', resource_query),
        Route('/resource_query', 'GET', resource_query),
        Route('/resource_body', 'POST', resource_body),
        Route('/resource_mixed', 'POST', resource_mixed),
    ],
    components=[
        *components
    ]
)

if __name__ == '__main__':
    app.serve('127.0.0.1', 3000, debug=True)

Project details


Download files

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

Source Distribution

apistar-pydantic-0.1.0.tar.gz (196.7 kB view details)

Uploaded Source

File details

Details for the file apistar-pydantic-0.1.0.tar.gz.

File metadata

File hashes

Hashes for apistar-pydantic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bdd429465b307c91ad5e196b3364c805747b2bf3d8159830f6b364a23641c040
MD5 a140b1c1b5bf5f12e93030724d9524c6
BLAKE2b-256 f500396c536aee6facfba53fd67c1cfc5e17f5511773e99f80ff772d22ce063f

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