Skip to main content

Add a new layer ("lâmina") to AWS lambda functions

Project description

Welcome to Lamina

PyPI PyPI - Python Version

This library adds a new layer ("lâmina") to AWS lambda functions, integrating synchronous and asynchronous code in a single function, which use Pydantic models to validate input and output data.


Install

$ pip install py-lamina

This library is compatible with Python 3.9, 3.10 and 3.11.


Usage

Create the models for Input and Output data:

# schemas.py

from pydantic import BaseModel

class ExampleInput(BaseModel):
    name: str
    age: int

class ExampleOutput(BaseModel):
    message: str

Create your AWS Lambda handler:

# main.py
from typing import Any, Dict, Tuple, Union
from lamina import lamina, Request
from .schemas import ExampleInput, ExampleOutput

@lamina(schema=ExampleInput, schema_out=ExampleOutput)
def handler(request: Request) -> Dict[str, Any]:
    response = {"message": f"Hello {request.data.name}, you are {request.data.age} years old!"}
    return response

You can also use an async handler:

# main.py
import asyncio

@lamina(schema=ExampleInput, schema_out=ExampleOutput)
async def handler(request: Request) -> Dict[str, Any]:
    await asyncio.sleep(1)
    response = {"message": f"Hello {request.data.name}, you are {request.data.age} years old!"}
    return response

The Response Status Code

Default value is 200. You can change it by returning a tuple with the response and the status code:

@lamina(schema=ExampleInput, schema_out=ExampleOutput)
def handler(request: Request) -> Tuple[Dict[str, Any], int]:
    response = {"message": f"Hello {request.data.name}, you are {request.data.age} years old!"}
    return response, 201

The Response Content Type

Default content type is application/json; charset=utf-8. You can change it by defining the content_type parameter:

@lamina(schema=ExampleInput, content_type=Lamina.HTML)
def handler(request: Request) -> Tuple[str, int]:
    html_404 = """
        <html>
            <head><title>404 Not Found</title></head>
            <body>
                <h1>404 Not Found</h1>
            </body>
        </html>
    """
    return html_404, 404

The Response Headers

Default header contains the Content Type defined in decorator or {"Content-Type": "application/json; charset=utf-8"} by default. You can add more headers it by returning a dict in the function return tuple:

@lamina(schema=ExampleInput, content_type=Lamina.HTML)
def handler(request: Request) -> str:
    return None, 403, {"Location": "https://www.example.com"}

This dict will be merged with the default header.

The Request object

The Request object has the following attributes:

  • data: The input data, already validated by the schema.
  • event: The original event received by the lambda function.
  • context: The original context received by the lambda function.

You can use lamina without one or both schemas, if you like:

# main.py
import json
from typing import Any, Dict

from lamina import lamina, Request


@lamina()
def handler(request: Request) -> Dict[str, Any]:
    body = request.event["body"]
    data = json.loads(body)
    response = {"message": f"Hello {data.name}, you are {data.age} years old!"}
    return response

Please note, if you do not define a SchemaIn, the data attribute will contain the original body from event. You need to validate it yourself and convert it to a dict, bytes, etc... you need before using the data received.


License

This project is licensed under the terms of the MIT license.

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

py_lamina-2.2.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

py_lamina-2.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file py_lamina-2.2.0.tar.gz.

File metadata

  • Download URL: py_lamina-2.2.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.11.1 readme-renderer/44.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/2.2.3 tqdm/4.66.5 importlib-metadata/8.5.0 keyring/25.4.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.15

File hashes

Hashes for py_lamina-2.2.0.tar.gz
Algorithm Hash digest
SHA256 17528f7f08d150b005df844ecef7d629dea01be243c9d5117eaf4fe1d8a714c5
MD5 12db739074c1bf4052e5a44bda8820cc
BLAKE2b-256 5018a88569fe64fd3c139fce18928465292516cd2e09c9f0d29f658ca8c1e5b6

See more details on using hashes here.

File details

Details for the file py_lamina-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: py_lamina-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.11.1 readme-renderer/44.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/2.2.3 tqdm/4.66.5 importlib-metadata/8.5.0 keyring/25.4.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.15

File hashes

Hashes for py_lamina-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dddf1ecbfdaf90e7132916faa53916ccdb6129bb62e47453fa6c665dcf2ac09d
MD5 f6d2b9bf10fcbbbdbe554cefc8e82f45
BLAKE2b-256 71f8459598f0416e0ea2309b3a2d13e80a272dfc0f62b4aabae3bd88f0532a1e

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