koxudaxi / lambdantic

Pydantic model and lambda event handler for AWS Lambda

Home Page:https://koxudaxi.github.io/lambdantic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lambdantic

Pydantic model and lambda event handler for AWS Lambda

Build Status PyPI version codecov

This project is an experimental phase.

What is Lambdantic ?

The name means AWS Lambda + pydantic

lambdantic dispatch handler function from aws lambda events and to assign attributes from event object.

Installation

To install lambdantic:

$ pip install lambdantic

Example

API Gateway

The example is simple api which is invoked by API Gateway.

lambdantic parse Lambda Proxy Integration request.

The example should be deployed by Serverless, AWS CDK or other framework which supports Lambda Proxy Integration.

# handler.py
from typing import List, Optional

from pydantic import BaseModel, Schema

from lambdantic.apigateway import Handler

handler = Handler()


class Pet(BaseModel):
    pet_id: Optional[int] = Schema(..., alias='id')
    name: str
    age: int


# database
pets = [
        Pet(id=1, name='dog', age=3),
        Pet(id=2, name='cat', age=2)
    ]


@handler.get('/pets')
def get_pets() -> List[Pet]:
    return pets


@handler.get('/pets/<pet_id>')
def get_pet(pet_id: int) -> Pet:
    for pet in pets:
        if pet.pet_id == pet_id:
            return pet


@handler.post('/pets', body_model=Pet, status_code=201)
def crate_pet(pet: Pet) -> Pet:
    pet.pet_id = max(p.pet_id for p in pets)
    pets.append(pet)

    return pet

If you use Serverless then, the handler is defined like this example.

functions:
  pet:
    handler: handler.handler
    events:
      - http:
          method: any
          path: /{proxy+}

I show common parameters for Other framework

Handler: handler.handler
API Gateway API integration type : Lambda Proxy Integration
Path: /{proxy+}
method: ANY

Implemented

  • API Gateway (WIP)

Not Implemented

  • S3
  • SNS ... and more

Development

Install the package in editable mode:

$ git clone git@github.com:koxudaxi/lambdantic.git
$ pip install -e lambdantic

PyPi

https://pypi.org/project/lambdantic

Source Code

https://github.com/koxudaxi/lambdantic

Documentation

https://koxudaxi.github.io/lambdantic

License

lambdantic is released under the MIT License. http://www.opensource.org/licenses/mit-license

About

Pydantic model and lambda event handler for AWS Lambda

https://koxudaxi.github.io/lambdantic

License:MIT License


Languages

Language:Python 100.0%