Skip to main content

A micro web framework for AWS Lambda.

Project description

Vial

A micro web framework for AWS Lambda.

Installation

To add vial to your project, run the following command:

pip install pyvial

Usage

Entry Point

The main entry point of the application is always the Vial#__call__ function. When deploying to AWS Lambda, the Lambda handler should point to the Vial object in whichever file it's defined in. As an example:

from vial.app import Vial

app = Vial()

If this code snippet is defined in an app.py file, the handler would be app.app.

Basic API

from typing import Mapping
from vial.app import Vial

app = Vial()


@app.get("/hello-world")
def hello_world() -> Mapping[str, str]:
    return {"hello": "world"}

Path Parameters

You can define path parameters like this:

@app.get("/users/{user_id}")
def get_user(user_id: str) -> User:
    return user_service.get(user_id)

Vial supports some path parameter parsing as part of the invocation process. For example when using a UUID as a path parameter, Vial can convert it from a string to a UUID automatically:

from uuid import UUID

@app.get("/users/{user_id:uuid}")
def get_user(user_id: UUID) -> User:
    return user_service.get(user_id)

The following parsers are supported by default:

Parser Type
str str
bool bool
int int
float float
decimal decimal.Decimal
uuid uuid.UUID

You can register your own parser like this:

@app.parser("list")
def list_parser(value: str) -> List[str]:
    return [value]


@app.get("/users/{user_id:list}")
def get_user(user_ids: List[str]) -> User:
    return user_service.get(user_id)

As parsers are bound directly to the registered route function, they have to be defined before the route function that uses one is registered.

Blueprints

As your application grows, you may want to split certain functionality amongst blueprints, similar to other popular frameworks like Flask.

You can define a blueprint like this:

# store.py
app = Blueprint()


@app.get("/stores/{store_id}")
def get_store(store_id: str) -> Store:
    return store_service.get(store_id)


# app.py
from stores import app as stores_app


app = Vial()

app.register_blueprint(stores_app)

Json Encoding

You can customize how Vial serializes / deserializes JSON objects by passing a custom encoder. The below example shows how to substitute the native JSON module with another library like simplejson:

import simplejson
from vial.app import Vial, Json


class SimpleJson(Json):
    @staticmethod
    def dumps(value: Any) -> str:
        return simplejson.dumps(value)

    @staticmethod
    def loads(value: str) -> Any:
        return simplejson.loads(value)

class JsonVial:
    json_class = SimpleJson


app = SimpleJsonVial()

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

pyvial-0.4.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

pyvial-0.4.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file pyvial-0.4.0.tar.gz.

File metadata

  • Download URL: pyvial-0.4.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.2 Darwin/20.3.0

File hashes

Hashes for pyvial-0.4.0.tar.gz
Algorithm Hash digest
SHA256 73b7f2ee1e41dad750e9a754be54ab1ea16e55ec5f46259810b9cde91277be7f
MD5 37ad6f5ea3ef46f7e3e1717a6570052d
BLAKE2b-256 8f8b5a3ff1da5972d26e3468d87d3d02a2bbba04582ae97018f4691898222339

See more details on using hashes here.

File details

Details for the file pyvial-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pyvial-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.2 Darwin/20.3.0

File hashes

Hashes for pyvial-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffa07ef672296814dc92b3086d1fc090d7db914d2fb95b2fe5c194541473fb5a
MD5 c320028736af9af6af9954aedd3a32cc
BLAKE2b-256 273cd7bd10540ab753ca60a0484368cc71846e68ea9ff7fbaf317ea17658fd3c

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