# pyfaaster
Useful Utilities for Python Functions as a Service (starting with AWS Lambda).
## Problem
Functions as a Service can be joyful. When paired with a concise language like Python, you start to
rethink the need for a "web framework" like Rails, Django, etc: couple your functions with your
FaaS provider's API Gateway and you are off an running with minimal code. Of course, there is still some boilerplate code around injecting environment, formatting responses, checking arguments, etc.
## Concept
The goal of [pyfaaster](https://github.com/Cloudzero/pyfaaster) is to cut
through the cruft and get you humming along with your Python FaaS. `pyfaaster` accomplishes
this goal by providing you with useful middleware (i.e. decorators) for your lambda functions. Additionally, `pyfaaster` can be used for its excellent `Makefile` and `serverless.yml` examples.
Cheers!
## Usage
The following is a non-exhaustive list and details of the useful middleware. More can be found in `pyfaaster.aws.handler_decorators`.
### Environment Variables
You don't want to sprinkle `os.environ` throughout your code. Let us do that for you.
```
import pyfaaster as faas
@faas.environ_aware(['REQUIRED_ENV'], ['OPTIONAL_ENV'])
def handler(event, context, REQUIRED_ENV=None, OPTIONAL_ENV=None):
assert REQUIRED_ENV == os.environ['REQUIRED_ENV'] # <- faas.environ_aware will return early with a useful message if this is not set
assert OPTIONAL_ENV == os.environ.get('OPTIONAL_ENV')
```
### Configuration Files
Similarly, don't worry about injecting those S3 Configuration files.
```
import pyfaaster as faas
@faas.configuration_aware('config.json', True) # S3 key to a config file, create if not there
def handler(event, context, configuration=None):
assert configuration == < contents of 'CONFIG' S3 bucket / config.json >
```
### Response Format
Sigh, you have to manually convert your lambda return values to API Gateway's expected dictionary? Don't forget to serialize all your json into the `body` element! Oh wait ... just use
```
import pyfaaster as faas
@faas.apig_response
def handler(event, context, **kwargs):
return { 'my': 'important', 'json': 'data'} # <- will end up as the json serialized `body` in an API Gateway compatible dict with statusCode.
```
### Authorization
You gotta confirm your token scopes, friend!
```
import pyfaaster as faas
# Checks the event.requestContext.authorizer for the given scopes. This works nicely with AWS custom
# authorizers. An example one is coming to this library soon.
@faas.scopes('read:profile', 'update:email')
def handler(event, context, **kwargs):
return 'Hello, Secure World!'
```
PyPI 官网下载 | pyfaaster-0.1.1.tar.gz
版权申诉
96 浏览量
2022-02-01
19:09:12
上传
评论
收藏 13KB GZ 举报
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
最新资源
- html动态爱心代码一(附源码)
- c40539bc-071a-486c-9d52-9d0c18d62dac 4.html
- 基于物理的非视域成像(NLOS)算法,利用了nerf+python源码+文档说明
- yuluer知更鸟.7z(1).001
- 基于Qt实现医院信息管理系统c++源码+文档说明+数据库(期末大作业)
- 基于python实现的医院信息管理系统完整源码+sql数据库+详细注释(高分课程设计)
- 基于python的眼底图像视杯视盘分割项目源码+文档说明+截图演示+详细注释(高分课程设计)
- ImageBasedModellingEdu-贰壹贰叁零
- DFFmeasurement-数据预处理
- ImageBasedModellingEdu-回文素数c语言
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈