带有可选json模式验证的azure函数的微框架

functionapprest的Python项目详细描述


功能测试

Build StatusLatest VersionPython Support

python路由mini framework for MS Azure Functions和可选的json模式验证。

**此存储库主要基于lambdarest project

功能

  • functionapp_handler带有内置分派器的函数构造函数
  • decorator注册函数以处理http方法
  • 使用相同的修饰符进行可选的json模式输入验证

安装

使用pip

pip install functionapprest

开始

此模块帮助您在azure函数中处理不同的http方法。

fromfunctionapprestimportfunctionapp_handler@functionapp_handler.handle('get')defmy_own_get(event):return{'this':'will be json dumped'}

高级用法

也可以根据json模式验证传入的json主体:

my_schema={'$schema':'http://json-schema.org/draft-04/schema#','type':'object','properties':{'body':{'type':'object','properties':{'foo':{'type':'string'}}}}}@functionapp_handler.handle('get',path='/with-schema/',schema=my_schema)defmy_own_get(event):return{'this':'will be json dumped'}

查询参数

查询参数也用json模式进行分析和验证。 查询数组应以逗号分隔,所有数字都转换为浮点数。

my_schema={'$schema':'http://json-schema.org/draft-04/schema#','type':'object','properties':{'query':{'type':'object','properties':{'foo':{'type':'array','items':{'type':'number'}}}}}}@functionapp_handler.handle('get',path='/with-params/',schema=my_schema)defmy_own_get(event):returnevent.json['query']

路由

您还可以使用path参数指定单个处理程序的反应路径:

@functionapp_handler.handle('get',path='/foo/bar/baz')defmy_own_get(event):return{'this':'will be json dumped'}

您还可以指定路径参数,这些参数将作为关键字参数传递:

@functionapp_handler.handle('get',path='/foo/<int:id>/')defmy_own_get(event,id):return{'my-id':id}

或者使用代理终结点:

@functionapp_handler.handle('get',path='/bar/<path:path>')defmy_own_get(event,path):return{'path':path}

在功能应用程序中使用

function.json

{"scriptFile":"handler.py","bindings":[{"authLevel":"anonymous","type":"httpTrigger","direction":"in","name":"req","methods":["get"],"route":"products/{product_id}"},{"type":"http","direction":"out","name":"$return"}]}

handler.py

fromfunctionapprestimportfunctionapp_handler,Request@functionapp_handler.handle('get',path='/products/<path:product_id>/')deflist_products(req:Request,product_id):query=req.json.get('query',{})body=req.json.get('body',{})headers=req.get('headers',{})params=req.get('params',{})response={'method':req.method,'url':req.url,'headers':headers,'params':params,'query':query,'body':body}returnresponsemain=functionapp_handler

测试

您可以使用pytest对当前的python版本运行测试。要运行当前python版本的测试,请运行pytest

有关测试依赖项,请参见^{},并使用pipenv install --dev安装它们。

贡献者

爱德华多穆拉尔

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

推荐PyPI第三方库


热门话题
java Spring数据JPA+Hibernate在不首先找到父实体的情况下保存子实体   php Java:如何从CLI接收命令   spring为java中的导出数据创建访问文件   java在Windows 8.1上安装Play Framework   java Spring启动白标签错误页面(类型=未找到,状态=404)   java如何在单击时从数组中绘制?   java fn:substringAfter()上次出现   java在IFR语句中使用方法返回   java onPause()或onStop()的名称   对关联对象的关联对象具有条件的java HQL查询   java只打印一次总值,无需迭代   java如何使用抽象Uri buildOn()方法?   如何在Java中执行sudo命令并获得错误输出?   java反射:避免对getConstructor(类<?>…)的未经检查的警告调用作为原始类型类的成员   Java:如何从类中创建的对象调用类方法?   java如何在电子邮件中嵌入图像?   java如何在Android上启用详细GC?   java什么是串行版本id?