Add a new layer ("lâmina") to AWS lambda functions
Project description
Welcome to Lamina
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 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for py_lamina-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26450724fea9a6755235efaeacf3860b445f1da658d4130844d9dc1a78d9ed87 |
|
MD5 | 8c0be5e4c3430343b546dd46df697039 |
|
BLAKE2b-256 | fa732fb3b04d219b4d74d431314b0793bee2784c7dd823c249ca5256d630bc0c |