用于生成aws lambda处理程序的有用装饰器的集合

lambda-handlers的Python项目详细描述


lambda处理程序

一个有助于指定aws lambda处理程序的固执己见的python包。

它包括输入验证、错误处理和响应格式化。

内容

开始

使用以下命令安装lambda-handlers

pip install lambda-handlers

如果要使用验证,应该在 Marshmallowjsonschema

要使用其中一个安装:

pip install 'lambda-handlers[marshmallow]'

pip install 'lambda-handlers[jsonschema]'

快速启动

默认情况下,http_handlerdecorator确保解析请求体 作为json,还将响应格式化为json,格式为:

  • 一个足够的状态码,
  • CORS标题,和
  • 处理程序返回正文中的值。
fromlambda_handlerimporthttp_handler@http_handler()defhandler(event,context):returnevent['body']

示例

跳过默认的cors头并配置它。

fromlambda_handlerimporthttp_handlerfromlambda_handlers.responseimportcors@http_handler(cors=cors(origin='localhost',credentials=False),)defhandler(event,context):returnevent['body']

使用jsonschema验证用户模型的输入。

fromtypingimportDict,Anyfromlambda_handlerimportvalidators,http_handleruser_schema:Dict[str,Any]={'type':'object','properties':{'user_id':{'type':'number'},},}@http_handler(validator=validators.jsonschema(body=user_schema),)defhandler(event,context):user=event['body']returnuser

使用棉花糖验证输入和中的用户模型 响应体。

fromlambda_handlerimportvalidators,http_handlerfrommarshmallowimportSchema,fieldsclassUserSchema(Schema):user_id=fields.Integer(required=True)classResponseSchema(Schema):body=fields.Nested(UserSchema,required=True)headers=fields.Dict(required=True)statusCode=fields.Integer(required=True)@http_handler(validator=validators.marshmallow(body=UserSchema(),response=ResponseSchema(),),)defhandler(event,context):user=event['body']returnuser

使用源代码

此项目使用pipenv来管理其依赖项 以及python环境。您可以通过以下方式安装:

pip install --user pipenv

我们建议对每个单独的项目使用python虚拟环境。 为此,我们建议使用pyenv

安装

对于生产,克隆此存储库后, 您可以使用以下命令安装此项目及其依赖项:

cd <clone_dest>
make install

发展

对于开发,您还应该安装开发依赖项, 所以请改为运行:

cd <clone_dest>
make install-dev

这将以开发模式安装所有依赖项和此项目。

测试

我们使用tox来运行代码检查程序。

您可以通过在项目的顶层运行tox来运行测试。

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

推荐PyPI第三方库


热门话题
尝试通过java驱动程序连接时,mongodb服务器上的SSLhandshake失败   使用PlayFramework的Azure网站中的java Logback   java在另一个ArrayList中使用ArrayList处理复杂的JSON响应   java无法在另一台机器上运行eclipse tomcat中的war文件   java GZIPOutputStream有什么替代方案吗?   java Nashorn调试在Nashorn中运行的javascript   java文本短信未发送,即使toast显示已发送   java Hibernatesearch 5.0 spatial不确定是否在散列中存储lat/lon   java我想创建一个带有文本视图的计数器   java安卓:如何正确地同步资源   java使用mockito。当不知道方法调用的参数时   firebase Java使用HTTP v1发送错误字符的中文通知   java Hibernate无法映射到表?   java使用对象映射器解析复杂JSON   java Selenium Grid 2并行测试用例执行   java所有项目在列表视图中重复