Skip to main content

a little opinionated framework for creating spic & span apis

Project description

spic

Status Code Coverage Ruff Hatch project PyPI - Version PyPI - Python Version Wheel Snyk


spic

Table of Contents

Intro

spic is an ASGI based api router with minimal dependencies and strong typing.

Rationale

  • popular libraries have dependencies which are moving commercial
  • no popular libraries for core python dataclass structures

Features

familiar methods

# ./main.py

from spic.app import Slip

app = Slip(title="my-api", version="0.1")

@app.get("/health")
async def status_handler():
    return {"status": "healthy ✨"}

app.collapse() # collapse routes

# hypercorn main:app --reload

dependency injection

# ./main.py

from spic.params import Header, Query

...

@app.get("/merged")
async def merged_params(x_usr_token: Header[str], query: Query[str]):
    return {
        "X-Usr-Token": x_usr_token,
        "query", query
    }


app.collapse()

dataclass support

# ./main.py
from dataclasses import dataclass
from spic.params import Header, Query

...

@dataclass
class QueryArgs:
    query: Query[str]
    x_usr_token: Header[str]

@app.get("/class_base")
async def model_params(model: QueryArgs):
    return {
        "X-Usr-Token": model.x_usr_token,
        "query", model.query
    }

generic dataclass serialization (pyserde)

# ./models.py
from serde import serde
from dataclasses import dataclass

@serde
@dataclass
class QueryArgs:
    query: Query[str]
    x_usr_token: Header[str]

# OR

# ./models.py
from spic.utils import schema

@schema
class QueryArgs:
    query: Query[str]
    x_usr_token: Header[str]

...
# ./main.py
...
@app.get("/serializable")
async def get_model(model: QueryArgs):
    return model # json

beartype validation

# ./client.py
from httpx import get
...

get(
    "http://localhost:8000/class_base?query=query-str-value" # missing header
).json()
# powered by beartype

# >> {
# >>  "errors": [
# >>    {
# >>      "key": "X_Usr_Token",
# >>      "expected": "string",
# >>      "given": "None",
# >>      "sources": ["header"]
# >>    }
# >>  ]
# >> }

performance

We currently test 3 primary use cases:

  • request param args -> function kwargs (no metaclass & valid)
    • 1.953ms overhead deserialization + reserialization (1000 ops)
  • request param args -> metaclass kwargs (dataclass)
    • 2.718ms overhead deserialization + reserialization (1000 ops)
  • invalid request param -> metaclass kwargs (raises errors & generates exception subclass)
    • 2.865ms overhead deserialization + reserialization (1000 ops)

Installation

pip install spic[core]

License

spic is distributed under the terms of the MIT license.

Download files

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

Source Distribution

spic-0.1.0.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

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

spic-0.1.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file spic-0.1.0.tar.gz.

File metadata

  • Download URL: spic-0.1.0.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for spic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea5b343d5fdfe0e43fa2f95f3369cb59c9b9d1f409da036af5a6b937cff7a520
MD5 912d548805e25714894924c3647dfb50
BLAKE2b-256 053b91cf493d3eda57bb240711f60ef9356a7fbb38fa923926e74a8e82afcb7e

See more details on using hashes here.

File details

Details for the file spic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: spic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for spic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c10cf9f341ca618cac417eca38d39349bcfedc38e3c072de9c364d340ccff3a
MD5 d009804d08263483e355e3f8e1ae78ad
BLAKE2b-256 c1c556c690a6851db607edc5897d5acd7be395a48b98bd87e1c039fcdc77c026

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