为令人难以置信的aws lambdas提供有用的实用工具

pyfaaster的Python项目详细描述


皮法斯特

Build Status

作为服务的python函数的有用实用程序(从aws lambda开始)。

问题

作为一种服务的功能可以是快乐的。当与像python这样简洁的语言搭配时,您将开始 重新考虑像rails、django等“web框架”的需求:将您的函数与 faas提供程序的api网关,您可以用最少的代码运行。当然,在注入环境、格式化响应、检查参数等方面仍有一些样板代码。

概念

pyfaaster的目标是 穿过拐杖,让你和你的Python爱好者一起哼唱。pyfaaster完成 这个目标是为lambda函数提供有用的中间件(即decorators)。此外,pyfaaster可用于其优秀的Makefileserverless.yml示例。

干杯!

用法

以下是有用中间件的非详尽列表和详细信息。更多信息可在pyfaaster.aws.handler_decorators中找到。

环境变量

你不想把os.environ洒在你的代码中。让我们来帮你吧。

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')

配置文件

同样,不要担心注入那些s3配置文件。

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 == < { 
    'load': lambda : {contents of config bucket},
    'save': lambda d : save d into config bucket,
    }

响应格式

唉,您必须手动将lambda返回值转换为api网关的预期字典吗?不要忘记将所有json序列化到body元素中!哦,等等……只需使用

import pyfaaster as faas

@faas.http_response(default_error_message='Failed to handle something.")
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.

授权

你得确认一下你的令牌范围,朋友!

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!'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java检查整数是0还是检查变量是null更好?   java Android Kotlin(初学者)使用File(),并从ACTION\u GET\u内容返回Uri   java JavaFx在“内部场景”和根场景之间切换   spring将XMLBean配置转换为java配置   java JPA HIBERNATE映射列两次(embeddedID和POJO)   c#单态模式模型在什么情况下适用?   java请求。getRemoteUser在特定时间后返回null?   spring boot中PUT api控制器的java my单元测试用例失败   java在字符串中互换地解析和替换值   java Android JNI在应用程序中检测到错误:调用JNI GetMethodID时出现挂起异常   JavaSpringDataMongo:使用非简单键持久化映射   爪哇玻璃鱼连接被拒绝   java如何在用户注册时发送特定电子邮件id的自动回复?   Java列表:实例化时和之后的赋值之间的差异