Skip to main content

Starlette API layer inherited from APIStar

Project description

Starlette API

Build Status codecov PyPI version

  • Version: 0.2.1
  • Status: Production/Stable
  • Author: José Antonio Perdiguero López

Introduction

That library aims to bring a layer on top of Starlette framework to provide useful mechanism for building APIs. It's based on API Star, inheriting some nice ideas like:

  • Schema system based on Marshmallow that allows to declare the inputs and outputs of endpoints and provides a reliable way of validate data against those schemas.
  • Dependency Injection that ease the process of managing parameters needed in endpoints.
  • Components as the base of the plugin ecosystem, allowing you to create custom or use those already defined in your endpoints, injected as parameters.
  • Starlette ASGI objects like Request, Response, Session and so on are defined as components and ready to be injected in your endpoints.

Requirements

  • Python 3.6+
  • Starlette 0.9+

Installation

$ pip install starlette-api

Example

from marshmallow import Schema, fields, validate
from starlette_api.applications import Starlette


# Data Schema
class Puppy(Schema):
    id = fields.Integer()
    name = fields.String()
    age = fields.Integer(validate=validate.Range(min=0))


# Database
puppies = [
    {"id": 1, "name": "Canna", "age": 6},
    {"id": 2, "name": "Sandy", "age": 12},
]


# Application
app = Starlette()


# Views
@app.route("/", methods=["GET"])
def list_puppies(name: str = None) -> Puppy(many=True):
    """
    List the puppies collection. There is an optional query parameter that 
    specifies a name for filtering the collection based on it.
    
    Request example:
    GET http://example.com/?name=Sandy
    
    Response example:
    200
    [
        {"id": 2, "name": "Sandy", "age": 12}
    ]
    """
    return [puppy for puppy in puppies if puppy["name"] == name]
    

@app.route("/", methods=["POST"])
def create_puppy(puppy: Puppy) -> Puppy:
    """
    Create a new puppy using data validated from request body and add it
    to the collection.
    
    Request example:
    POST http://example.com/
    {
        "id": 1,
        "name": "Canna",
        "age": 6
    }
    
    Response example:
    200
    {
        "id": 1,
        "name": "Canna",
        "age": 6
    }
    """
    puppies.append(puppy)
    
    return puppy

Credits

That library started mainly as extracted pieces from APIStar and adapted to work with Starlette.

Contributing

This project is absolutely open to contributions so if you have a nice idea, create an issue to let the community discuss it.

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

starlette-api-0.2.1.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_api-0.2.1-py3-none-any.whl (81.5 kB view details)

Uploaded Python 3

File details

Details for the file starlette-api-0.2.1.tar.gz.

File metadata

  • Download URL: starlette-api-0.2.1.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.11 CPython/3.7.2 Linux/4.20.0-arch1-1-ARCH

File hashes

Hashes for starlette-api-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0dbe12958c500b08ab538e1eb0682ffcaa57740b6d250abb0b9b5899e415a460
MD5 6806999365e3e92c84f535d0555ce841
BLAKE2b-256 ce8f126f6422f17c958650b5c56677995759cbd257928c1b1ec5d7cb65ddcf6a

See more details on using hashes here.

File details

Details for the file starlette_api-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: starlette_api-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 81.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.11 CPython/3.7.2 Linux/4.20.0-arch1-1-ARCH

File hashes

Hashes for starlette_api-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5ce501fff1ba6b5c321ae53c65e3c7d8f53c4c085454e19ad7f79b6d2261c0f
MD5 612f5dfcfc92a9fbcb05558ca1db5673
BLAKE2b-256 c49ee409477a8b9e1d913f37ddd96ab9078a8a0ebed5f9625a2f0d4420eef95e

See more details on using hashes here.

Supported by

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