py-lamina


Namepy-lamina JSON
Version 2.0.2 PyPI version JSON
download
home_pagehttps://github.com/megalus/lamina
SummaryAdd a new layer ("lâmina") to AWS lambda functions
upload_time2023-10-03 16:51:57
maintainer
docs_urlNone
authorChris Maillefaud
requires_python>3.9,<4
licenseMIT
keywords aws lambda decorator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Welcome to Lamina

<p align="center">
<a href="https://pypi.org/project/py-lamina/" target="_blank">
<img alt="PyPI" src="https://img.shields.io/pypi/v/py-lamina"/></a>
<a href="https://www.python.org" target="_blank">
<img alt="PyPI - Python Version" src="https://img.shields.io/pypi/pyversions/py-lamina"/>
</a>
</p>

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

```shell
$ 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:

```python
# schemas.py

from pydantic import BaseModel

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

class ExampleOutput(BaseModel):
    message: str
```

Create your AWS Lambda handler:

```python
# 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:

```python
# 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:

```python
@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:

```python
@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:

```python
@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:

```python
# 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.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/megalus/lamina",
    "name": "py-lamina",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">3.9,<4",
    "maintainer_email": "",
    "keywords": "aws,lambda,decorator",
    "author": "Chris Maillefaud",
    "author_email": "chrismaille@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/2e/f0/d5d302e92f54d19c2ef7d9560f715a52de9598948733c67e7d633d7ab1ac/py_lamina-2.0.2.tar.gz",
    "platform": null,
    "description": "# Welcome to Lamina\n\n<p align=\"center\">\n<a href=\"https://pypi.org/project/py-lamina/\" target=\"_blank\">\n<img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/py-lamina\"/></a>\n<a href=\"https://www.python.org\" target=\"_blank\">\n<img alt=\"PyPI - Python Version\" src=\"https://img.shields.io/pypi/pyversions/py-lamina\"/>\n</a>\n</p>\n\nThis library adds a new layer (\"l\u00e2mina\") to AWS lambda functions, integrating synchronous and asynchronous code in a\nsingle function, which use Pydantic models to validate input and output data.\n\n---\n\n### Install\n\n```shell\n$ pip install py-lamina\n```\n\nThis library is compatible with Python 3.9, 3.10 and 3.11.\n\n---\n\n### Usage\n\nCreate the models for Input and Output data:\n\n```python\n# schemas.py\n\nfrom pydantic import BaseModel\n\nclass ExampleInput(BaseModel):\n    name: str\n    age: int\n\nclass ExampleOutput(BaseModel):\n    message: str\n```\n\nCreate your AWS Lambda handler:\n\n```python\n# main.py\nfrom typing import Any, Dict, Tuple, Union\nfrom lamina import lamina, Request\nfrom .schemas import ExampleInput, ExampleOutput\n\n@lamina(schema=ExampleInput, schema_out=ExampleOutput)\ndef handler(request: Request) -> Dict[str, Any]:\n    response = {\"message\": f\"Hello {request.data.name}, you are {request.data.age} years old!\"}\n    return response\n```\n\nYou can also use an async handler:\n\n```python\n# main.py\nimport asyncio\n\n@lamina(schema=ExampleInput, schema_out=ExampleOutput)\nasync def handler(request: Request) -> Dict[str, Any]:\n    await asyncio.sleep(1)\n    response = {\"message\": f\"Hello {request.data.name}, you are {request.data.age} years old!\"}\n    return response\n```\n\n### The Response Status Code\nDefault value is 200. You can change it by returning a tuple with the response and the status code:\n\n```python\n@lamina(schema=ExampleInput, schema_out=ExampleOutput)\ndef handler(request: Request) -> Tuple[Dict[str, Any], int]:\n    response = {\"message\": f\"Hello {request.data.name}, you are {request.data.age} years old!\"}\n    return response, 201\n```\n\n### The Response Content Type\nDefault content type is `application/json; charset=utf-8`. You can change it by defining the `content_type` parameter:\n\n```python\n@lamina(schema=ExampleInput, content_type=Lamina.HTML)\ndef handler(request: Request) -> Tuple[str, int]:\n    html_404 = \"\"\"\n        <html>\n            <head><title>404 Not Found</title></head>\n            <body>\n                <h1>404 Not Found</h1>\n            </body>\n        </html>\n    \"\"\"\n    return html_404, 404\n```\n\n### The Response Headers\nDefault header contains the Content Type defined in decorator or `{\"Content-Type\": \"application/json; charset=utf-8\"}`\nby default.\nYou can add more headers it by returning a dict in the function return tuple:\n\n```python\n@lamina(schema=ExampleInput, content_type=Lamina.HTML)\ndef handler(request: Request) -> str:\n    return None, 403, {\"Location\": \"https://www.example.com\"}\n```\n\nThis dict will be merged with the default header.\n\n### The Request object\n\nThe `Request` object has the following attributes:\n* `data`: The input data, already validated by the schema.\n* `event`: The original event received by the lambda function.\n* `context`: The original context received by the lambda function.\n\nYou can use lamina without one or both schemas, if you like:\n\n```python\n# main.py\nimport json\nfrom typing import Any, Dict\n\nfrom lamina import lamina, Request\n\n\n@lamina()\ndef handler(request: Request) -> Dict[str, Any]:\n    body = request.event[\"body\"]\n    data = json.loads(body)\n    response = {\"message\": f\"Hello {data.name}, you are {data.age} years old!\"}\n    return response\n```\n\nPlease note, if you do not define a SchemaIn, the `data` attribute will contain the **original** body from event. You\nneed to validate it yourself and convert it to a dict, bytes, etc... you need before using the data received.\n\n---\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Add a new layer (\"l\u00e2mina\") to AWS lambda functions",
    "version": "2.0.2",
    "project_urls": {
        "Homepage": "https://github.com/megalus/lamina",
        "Repository": "https://github.com/megalus/lamina"
    },
    "split_keywords": [
        "aws",
        "lambda",
        "decorator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f8dc75df63dd0725653eb757f94f3fc35f3c3a1a9f25b9ce4a132f339b0951f7",
                "md5": "8e081cfda7235f44dfb0b4cc517bc6a3",
                "sha256": "081137160a132274ef6ce918f79c75e4f40f4504b3921aa509571a1ed63c8933"
            },
            "downloads": -1,
            "filename": "py_lamina-2.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8e081cfda7235f44dfb0b4cc517bc6a3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">3.9,<4",
            "size": 5355,
            "upload_time": "2023-10-03T16:51:56",
            "upload_time_iso_8601": "2023-10-03T16:51:56.089510Z",
            "url": "https://files.pythonhosted.org/packages/f8/dc/75df63dd0725653eb757f94f3fc35f3c3a1a9f25b9ce4a132f339b0951f7/py_lamina-2.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2ef0d5d302e92f54d19c2ef7d9560f715a52de9598948733c67e7d633d7ab1ac",
                "md5": "00be2d3eb26edf3d8d1dc0a82124cd1b",
                "sha256": "2acb8b49c73183347c440f055a594481dee74b1ae1dc99eb1b94c116f1215f7c"
            },
            "downloads": -1,
            "filename": "py_lamina-2.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "00be2d3eb26edf3d8d1dc0a82124cd1b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">3.9,<4",
            "size": 4929,
            "upload_time": "2023-10-03T16:51:57",
            "upload_time_iso_8601": "2023-10-03T16:51:57.735314Z",
            "url": "https://files.pythonhosted.org/packages/2e/f0/d5d302e92f54d19c2ef7d9560f715a52de9598948733c67e7d633d7ab1ac/py_lamina-2.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-03 16:51:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "megalus",
    "github_project": "lamina",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "py-lamina"
}
        
Elapsed time: 0.12197s