Eve库简单教程
文章目錄
- 1. 簡介
- 2. 使用實例
- 2.1 hello world
- 2.2 支持認證功能
- 2.3 支持schema 校驗
1. 簡介
Eve是一個 open source 為人類設計的python rest api框架。它允許輕松地構建和部署高度可定制、功能齊全的RESTful Web服務。
Eve由 Flask 和 Cerberus 它提供本地支持 MongoDB 數據存儲。社區提供對SQL、ElasticSearch和Neo4JS后端的支持 extensions.
代碼庫在python 2.7、3.5+和pypy下進行了徹底的測試。
2. 使用實例
2.1 hello world
from eve import Evesettings = {'DOMAIN': {'people': {}}}app = Eve(settings=settings) app.run()啟動該程序,發送請求
$ curl -i http://example.com/people HTTP/1.1 200 OK2.2 支持認證功能
from eve import Eve from eve.auth import BasicAuthclass MyBasicAuth(BasicAuth):def check_auth(self, username, password, allowed_roles, resource, method):return username == 'admin' and password == 'secret'app = Eve(auth=MyBasicAuth)if __name__ == '__main__':app.run()2.3 支持schema 校驗
>>> schema = {'name': {'type': 'string'}, 'age': {'type': 'integer', 'min': 10}} >>> document = {'name': 'Little Joe', 'age': 5} >>> v.validate(document, schema) False >>> v.errors {'age': ['min value is 10']}>>> v = Validator() >>> v.require_all False>>> schema = { ... 'name': {'type': 'string'}, ... 'a_dict': { ... 'type': 'dict', ... 'require_all': True, ... 'schema': { ... 'address': {'type': 'string'} ... } ... } ... }>>> v.validate({'name': 'foo', 'a_dict': {}}, schema) False >>> v.errors {'a_dict': [{'address': ['required field']}]}>>> v.validate({'a_dict': {'address': 'foobar'}}, schema) True總結
- 上一篇: HTML中jquery轮播图旋转,jqu
- 下一篇: ABB基础学问:IRB1200详尽介绍