Eve是一款Python的REST API架構,用於構建和部署高可定製的、全功能的RESTful的Web服務。Eve是一個開源項目,遵循BSD開源協議,已在Python 2.6、2.7以及Python 3.3版本下進行了非常全面的測試。
特色
強調REST
非常全面的CRUD操作
可自訂的資源端點
自訂多重專案端點
篩選和排序
分頁
HATEOAS
JSON和XML渲染
條件請求
資料完整性和並發控制
多個添加操作
資料驗證
可擴充的資料驗證
資源級緩衝控制
版本
驗證
CORS跨地資源共用
預設情況下唯讀
預設值
預定義的資料庫過濾器
預測
事件關聯
速率限制
MongoDB支援
和別的架構一樣,咱們需要安裝eve的模組
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J25400-0.jpg" title="pip.jpg" alt="113532639.jpg" />
直接yum安裝吧,我自己選擇mongodb的時候,都會用10gen的。
cat /etc/yum.repos.d/10gen.repo
[10gen]name=10gen Repositorybaseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64gpgcheck=0
開始安裝吧~
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J220L-1.jpg" title="mon.jpg" alt="113307581.jpg" />
好了,咱們現在就測試下吧:
啟動server.py還需要做的就是配上一個資料庫、設定檔預設是settings.py)、啟動指令碼來讓API可以線上使用。總之,你會發現,配置和調優API是一件非常簡單的事情。
server端
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J212T-2.jpg" title="11.jpg" alt="115007669.jpg" />
用戶端
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J23110-3.jpg" title="22.jpg" alt="115032430.jpg" />
官方還推薦了一個eve demo
https://github.com/nicolaiarocci/eve-demo
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J23630-4.jpg" title="88.jpg" alt="115802876.jpg" />
demo裡面有幾個執行個體 ~
返回是可以定義的:
xml
curl -H "Accept: application/xml" -i "http://127.0.0.1:5000/"
json
curl -H "Accept: application/json" -i "http://127.0.0.1:5000/"
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J21926-5.jpg" title="888.jpg" alt="121116936.jpg" />
在這定義規則啥的 ~
Here is how the complete people definition looks in our updated settings.pyfile:
people = { # 'title' tag used in item links. Defaults to the resource title minus # the final, plural 's' (works fine in most cases but not for 'people') 'item_title': 'person', # by default the standard item entry point is defined as # '/people/<ObjectId>'. We leave it untouched, and we also enable an # additional read-only entry point. This way consumers can also perform # GET requests at '/people/<lastname>'. 'additional_lookup': { 'url': '[\w]+', 'field': 'lastname' }, # We choose to override global cache-control directives for this resource. 'cache_control': 'max-age=10,must-revalidate', 'cache_expires': 10, # most global settings can be overridden at resource level 'resource_methods': ['GET', 'POST'], 'schema': schema}
插入資料~
curl -d '[{"firstname": "rui", "lastname": "fengyun"}, {"firstname": "li", "lastname": "liying"}]' -H 'Content-Type: application/json' http://127.0.0.1:5000/people
查詢資料~
curl -i http://eve-demo.herokuapp.com/people?where={"lastname": "liying"}
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J24C0-6.jpg" title="QQ20131116123948.jpg" alt="124028547.jpg" />
我們從mongo的cli中瞅瞅,看看資料
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/122J2G11-7.jpg" title="QQ20131116125039.jpg" alt="125358837.jpg" />
In the above response, a Last-Modified header is included. It can be used later to retrieve only the items that have changed since:
curl -H "If-Modified-Since: Wed, 05 Dec 2012 09:53:07 UTC" -i http://127.0.0.1:5000/people/
eve 其實在一定程度上可以理解為mongodb的http介面 就是redis的webdis一樣的
類似mysql的取出lastname欄位為Doe的資料
$ curl -i http://eve-demo.herokuapp.com/people?where={"lastname": "Doe"}
and the native Python syntax:
$ curl -i http://eve-demo.herokuapp.com/people?where=lastname=="Doe"
排序的方式
$ curl -i http://eve-demo.herokuapp.com/people?sort=[("lastname", -1)]
可以做出mongodb limit 的效果
$ curl -i http://eve-demo.herokuapp.com/people/?where={"lastaname": "Doe"}&sort=[("firstname",1)]&page=5
多次插入資料
$ curl -d 'item1={"firstname": "barack", "lastname": "obama"}' -d 'item2={"firstname": "mitt", "lastname": "romney"}' http://127.0.0.1/people
Response:
{ [ "status": "OK", "updated": "Thu, 22 Nov 2012 15:22:27 UTC", "_id": "50ae43339fa12500024def5b", "_links": {"self": {"href": "eve-demo.herokuapp.com/people/50ae43339fa12500024def5b", "title": "person"}} ], [ "status": "OK", "updated": "Thu, 22 Nov 2012 15:22:27 UTC", "_id": "50ae43339fa12500024def5c", "_links": {"self": {"href": "eve-demo.herokuapp.com/people/50ae43339fa12500024def5c", "title": "person"}} ]}
總結: eve是個強大又可以擴充res架構,也可以把他用為mongodb的http介面 ~
我對他的理解還是有些片面,有時間再好好看看。
本文出自 “峰雲,就她了。” 部落格,謝絕轉載!