直接绑定到模型的aoihttp的restful端点
aiohttp-rest的Python项目详细描述
aiohttp_rest使创建restful aiohttp端点变得容易,这些端点直接绑定到模型,只需很少的修改。
用法
创建您的模型:
classPerson:def__init__(self,name,age):self.name=nameself.age=age
创建使用aiohttp_rest.RestResource:
的aiohttp应用程序fromaiohttp.webimportApplication,run_appfromaiohttp_restimportRestResourcefrompersonimportPersonpeople={}app=Application()person_resource=RestResource('people',people,Person,('name','age'),'name')person_resource.register(app.router)run_app(app)
与API交互:
http POST localhost:8000/people name=andrew age=24 http localhost:8000/people/andrew http PUT localhost:8000/people/andrew/age age=25 http DELETE localhost:8000/people/andrew
RestResource需要一些东西才能使所有这些工作:
- 资源的名称,这将构成url的基础。
- 如上所示,模型的工厂方法可以是模型的init方法或更复杂的方法。
- 存储模型的集合,它应该是类似于dict的对象。
- 要公开给api的属性列表。
- 要视为集合ID的属性。在上面的例子中,我们使用名称作为id,所以这就是我们在url中用来引用集合中特定实例的内容。
安装
git clone github.com/atbentley/aiohttp_rest cd aiohttp_rest python setup.py install
测试
pip install -r build-requirements.txt py.test tests
许可证
麻省理工学院