无功效超媒体扩展模式
dougrain-forms的Python项目详细描述
一个超媒体表单生成器,遵循迈克·凯利的非正式草稿。 格式大致基于 this Gist按Mike Kelly。
安装
pip install dougrain-forms
用法
示例:
from dougrain_forms import FormsMixin class FormsDocument(Document, FormsMixin): pass doc = FormsDocument.empty() doc.add_link('self', '/foo') doc.set_form( 'attack', '/attacks', headers={ 'Content-Type': 'application/json' }, method='POST', schema=ATTACK_SCHEMA ) print(doc.as_object())
输出:
{ "_forms": { "attack": { "headers": { "Content-Type": "application/json" }, "href": "/attacks", "method": "POST", "schema": { "required": [ "name", "damage" ], "type": "object", "properties": { "name": { "type": "string" }, "damage": { "minimum": 0, "type": "integer", "description": "How much does it hurt?" } }, "title": "Damage Schema" } } }, "_links": { "self": { "href": "/foo" } } }