[![Build Status](https://travis-ci.org/QuantumBA/pyverless.svg?branch=master)](https://travis-ci.org/QuantumBA/pyverless)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pyverless.svg)](https://pypi.python.org/pypi/pyverless/)
[![PyPI license](https://img.shields.io/pypi/l/pyverless.svg)](https://pypi.python.org/pypi/pyverless/)
[![PyPI status](https://img.shields.io/pypi/status/pyverless.svg)](https://pypi.python.org/pypi/pyverless/)
# Pyverless
Developing complex APIs within AWS lambdas can be somewhat of a messy task. Lambdas are independent functions that have to work together in order to create a full-blown app, like atoms to a complex organism.
In order to define the infrastructure you may use a framework like [Serverless](https://serverless.com/), but you may find yourself copying and pasting blobs of code within your handler functions, namely for authentication, data validation, error handling and response creation to name a few.
**Enter Pyverless**
Pyverless is a mini-framework with a bunch of utilities that aims to help you create APIs using AWS Lambdas fast and in a consistent way. Pyverless provides the following.
- Class-Based Handlers
- Serializers
- Authentication handling
- JWT and cryptography
- Exceptions
- Configuration
- Warmup handling
Bring more consistency and development speed to your lambda-based APIs!
## Class-Based Handlers
Class based handlers (CBH) use the approach of Django's Class-Based Views to provide code reuse, consistency and generally abstract simple and common tasks. The aim of class-based handlers is to suit a wide range of applications by providing generic Handler classes and mixins.
Within AWS Lambda, a handler is a function that takes an event and a context and returns a response.
Generic CBH are based off the following base handler
### BaseHandler
This class provides the `as_handler()` method that returns a handler function (taking `event` and `context` as arguments).
Usage:
```python
class MyHandler(BaseHandler):
pass
_myHandler = MyHandler.as_handler()
```
There is a set of generic CBHs to handle basic CRUD operations within an API:
### CreateHandler
Handler that reads the request body and creates the object with each (key, value) pair as a parameter for the constructor.
The `model` attribute must be set on the handler and the `create_object` method can be overwritten.
Usage:
```python
class UserCreateHandler(CreateHandler):
model = MyUserClass # MyUserClass(k1=v1, k2=v2, ...) for each k,v on body
required_body_keys = ['email', 'password']
```
### RetrieveHandler
Handler that returns a serialized Object.
The `model` attribute must be set and `id` must be present on the pathParameters.
The user must overwrite either the `serializer` attribute or the `serialize` method.
Usage:
```python
class UserRetrieveHandler(RetrieveHandler):
model = MyUserClass
serializer = serialize_user
```
### UpdateHandler
Handler that sets self.object and for each (key, value) pair of the body
sets self.object.key = value.
The `model` attribute must be set and `id` must be present on the pathParameters.
Returns the serialized node and sets the HTTP status code to 200
Usage:
```python
class UserUpdateHandler(UpdateHandler):
model = MyUserClass
required_body_keys = ['title', 'body']
serializer = serialize_user
```
### DeleteHandler
Handler that sets self.object, calls its delete() method and sets the HTTP status code to 204.
The `model` attribute must be set and `id` must be present on the pathParameters.
The user can also overwrite the `get_queryset` method to limit the search.
Usage:
```python
class UserDeleteHandler(DeleteHandler):
model = MyUserClass
```
### ListHandler
Handler that returns a list of serialized nodes and sets the HTTP status code to 200.
The `model` attribute must be set and the user must overwrite either the `serializer` attribute
or the `serialize` method.
```python
class UserListHandler(ListHandler):
model = MyUserClass
serializer = user_serializer
def get_queryset(self):
return only_some_users
```
## Mixins
There are also a set of **mixins** available:
### RequestBodyMixin
This mixin provides the `get_body()` method which is in charge of gathering the request body dictionary. Define `required_body_keys` and `optinal_body_keys` as follows. Within the handler, you can access the body via `self.body` or by calling `get_body()`
```python
class MyHandler(RequestBodyMixin, BaseHandler):
required_body_keys = ['name', 'email']
optinal_body_keys = ['phone']
_myHandler = MyHandler.as_handler()
```
### AuthorizationMixin
This mixin provides the `get_user()` method in charge of getting the user out of an authenticated API call.
Within the handler, you can access the body via `self.user` or by calling `get_user()`. The user will be a object
of the class specified on pyverless settings as `USER_MODEL`.
### RequestBodyMixin
This mixin provides the `get_object()` method in charge of gathering a particular object,
you can access the object via `self.object`.
The `id` of the object will be taken from the pathParameters and
the user must set the `model` attribute on the handler.
### ListMixin
This mixin provides the `get_queryset()` method in charge of getting a list of objects,
you can access the list via `self.queryset`. The user must set the `model` attribute
and either the `serializer` attribute or `serialize()` method on the handler.
### S3FileMixin
This mixin provides the `get_file()` and `get_message_part()` methods in charge of
reading an event from aws S3, you can access the file via `self.file`.
The file will be a `dict()` with the following keys: bucket, owner, file_name, size.
***Warning: Only tested with objectCreated!!!!***
### SQSMessagesMixin
This mixin provides the `get_messages()` method in charge of reading an SQS event from aws.
You can access the list of messages via `self.messages`.
Each message will be a `dict()` with the following keys: attributes, text_message, queue_source, region.
## Serializers
**TODO**
Raw data
{
"_id": null,
"home_page": "https://github.com/QuantumBA/pyverless",
"name": "pyverless",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "serverless, api, development",
"author": "rperez",
"author_email": "rperez@op2aim.io",
"download_url": "https://files.pythonhosted.org/packages/23/a7/dd53631d528666d84d667e9f3143a72510640c4719e935a10b52cfbb96df/pyverless-0.0.54.tar.gz",
"platform": null,
"description": "[![Build Status](https://travis-ci.org/QuantumBA/pyverless.svg?branch=master)](https://travis-ci.org/QuantumBA/pyverless)\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pyverless.svg)](https://pypi.python.org/pypi/pyverless/)\n[![PyPI license](https://img.shields.io/pypi/l/pyverless.svg)](https://pypi.python.org/pypi/pyverless/)\n[![PyPI status](https://img.shields.io/pypi/status/pyverless.svg)](https://pypi.python.org/pypi/pyverless/)\n# Pyverless\n\nDeveloping complex APIs within AWS lambdas can be somewhat of a messy task. Lambdas are independent functions that have to work together in order to create a full-blown app, like atoms to a complex organism.\n\nIn order to define the infrastructure you may use a framework like [Serverless](https://serverless.com/), but you may find yourself copying and pasting blobs of code within your handler functions, namely for authentication, data validation, error handling and response creation to name a few.\n\n**Enter Pyverless**\n\nPyverless is a mini-framework with a bunch of utilities that aims to help you create APIs using AWS Lambdas fast and in a consistent way. Pyverless provides the following.\n\n- Class-Based Handlers\n- Serializers\n- Authentication handling\n- JWT and cryptography\n- Exceptions\n- Configuration\n- Warmup handling\n\nBring more consistency and development speed to your lambda-based APIs!\n\n## Class-Based Handlers\n\nClass based handlers (CBH) use the approach of Django's Class-Based Views to provide code reuse, consistency and generally abstract simple and common tasks. The aim of class-based handlers is to suit a wide range of applications by providing generic Handler classes and mixins.\n\nWithin AWS Lambda, a handler is a function that takes an event and a context and returns a response.\n\nGeneric CBH are based off the following base handler\n\n### BaseHandler\n\nThis class provides the `as_handler()` method that returns a handler function (taking `event` and `context` as arguments).\n\nUsage:\n\n```python\nclass MyHandler(BaseHandler):\n pass\n\n_myHandler = MyHandler.as_handler()\n```\n\nThere is a set of generic CBHs to handle basic CRUD operations within an API:\n\n### CreateHandler\nHandler that reads the request body and creates the object with each (key, value) pair as a parameter for the constructor.\n\nThe `model` attribute must be set on the handler and the `create_object` method can be overwritten.\n\nUsage:\n\n```python\nclass UserCreateHandler(CreateHandler):\n\n model = MyUserClass # MyUserClass(k1=v1, k2=v2, ...) for each k,v on body\n required_body_keys = ['email', 'password']\n```\n\n### RetrieveHandler\nHandler that returns a serialized Object.\n\nThe `model` attribute must be set and `id` must be present on the pathParameters.\n\nThe user must overwrite either the `serializer` attribute or the `serialize` method.\n\nUsage:\n\n```python\nclass UserRetrieveHandler(RetrieveHandler):\n\n model = MyUserClass\n serializer = serialize_user\n```\n\n### UpdateHandler\nHandler that sets self.object and for each (key, value) pair of the body\nsets self.object.key = value.\n\nThe `model` attribute must be set and `id` must be present on the pathParameters.\n\nReturns the serialized node and sets the HTTP status code to 200\n\nUsage:\n\n```python\nclass UserUpdateHandler(UpdateHandler):\n model = MyUserClass\n required_body_keys = ['title', 'body']\n serializer = serialize_user\n```\n\n\n### DeleteHandler\nHandler that sets self.object, calls its delete() method and sets the HTTP status code to 204.\n\nThe `model` attribute must be set and `id` must be present on the pathParameters.\n\nThe user can also overwrite the `get_queryset` method to limit the search.\n\nUsage:\n\n```python\nclass UserDeleteHandler(DeleteHandler):\n model = MyUserClass\n```\n### ListHandler\nHandler that returns a list of serialized nodes and sets the HTTP status code to 200.\n\nThe `model` attribute must be set and the user must overwrite either the `serializer` attribute\nor the `serialize` method.\n\n```python\nclass UserListHandler(ListHandler):\n model = MyUserClass\n serializer = user_serializer\n \n def get_queryset(self):\n return only_some_users\n```\n\n## Mixins\nThere are also a set of **mixins** available:\n\n### RequestBodyMixin\n\nThis mixin provides the `get_body()` method which is in charge of gathering the request body dictionary. Define `required_body_keys` and `optinal_body_keys` as follows. Within the handler, you can access the body via `self.body` or by calling `get_body()`\n\n```python\nclass MyHandler(RequestBodyMixin, BaseHandler):\n required_body_keys = ['name', 'email']\n optinal_body_keys = ['phone']\n\n_myHandler = MyHandler.as_handler()\n```\n\n### AuthorizationMixin\n\nThis mixin provides the `get_user()` method in charge of getting the user out of an authenticated API call.\nWithin the handler, you can access the body via `self.user` or by calling `get_user()`. The user will be a object\nof the class specified on pyverless settings as `USER_MODEL`.\n\n### RequestBodyMixin\n\nThis mixin provides the `get_object()` method in charge of gathering a particular object,\nyou can access the object via `self.object`.\nThe `id` of the object will be taken from the pathParameters and\nthe user must set the `model` attribute on the handler.\n\n### ListMixin\n\nThis mixin provides the `get_queryset()` method in charge of getting a list of objects,\nyou can access the list via `self.queryset`. The user must set the `model` attribute\nand either the `serializer` attribute or `serialize()` method on the handler.\n\n### S3FileMixin\n\nThis mixin provides the `get_file()` and `get_message_part()` methods in charge of\nreading an event from aws S3, you can access the file via `self.file`.\n\nThe file will be a `dict()` with the following keys: bucket, owner, file_name, size.\n\n***Warning: Only tested with objectCreated!!!!***\n\n### SQSMessagesMixin\n\nThis mixin provides the `get_messages()` method in charge of reading an SQS event from aws.\nYou can access the list of messages via `self.messages`.\n\nEach message will be a `dict()` with the following keys: attributes, text_message, queue_source, region.\n\n## Serializers\n\n**TODO**\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "A mini-framework providing tools to help you make complex APIs with serverless",
"version": "0.0.54",
"project_urls": {
"Homepage": "https://github.com/QuantumBA/pyverless",
"Repository": "https://github.com/QuantumBA/pyverless"
},
"split_keywords": [
"serverless",
" api",
" development"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "90b60880fcc887459a993827e462fddb26625dfb0653ad7cfed6313d6f648e83",
"md5": "7028c4928a561bda8d3919033d1dfe06",
"sha256": "92ebfde9fbd3f5099a3f5bed51442df91e4fd2a328e48f1d6539643a5b1d51ad"
},
"downloads": -1,
"filename": "pyverless-0.0.54-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7028c4928a561bda8d3919033d1dfe06",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 18502,
"upload_time": "2024-07-03T09:42:14",
"upload_time_iso_8601": "2024-07-03T09:42:14.477802Z",
"url": "https://files.pythonhosted.org/packages/90/b6/0880fcc887459a993827e462fddb26625dfb0653ad7cfed6313d6f648e83/pyverless-0.0.54-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "23a7dd53631d528666d84d667e9f3143a72510640c4719e935a10b52cfbb96df",
"md5": "477cdbb38194f6df7e001c88be129e37",
"sha256": "5b2de8ae620c4de9ba360c7bef235a548228927dac4013e09fa215b2ba2cf7e6"
},
"downloads": -1,
"filename": "pyverless-0.0.54.tar.gz",
"has_sig": false,
"md5_digest": "477cdbb38194f6df7e001c88be129e37",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 15953,
"upload_time": "2024-07-03T09:42:16",
"upload_time_iso_8601": "2024-07-03T09:42:16.687501Z",
"url": "https://files.pythonhosted.org/packages/23/a7/dd53631d528666d84d667e9f3143a72510640c4719e935a10b52cfbb96df/pyverless-0.0.54.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-03 09:42:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "QuantumBA",
"github_project": "pyverless",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pyverless"
}