基于python的REST框架eve测试与mongodb的数据操作

Eve是一款Python的REST API框架,用于构建和部署高可定制的、全功能的RESTful的Web服务。Eve是一个开源项目,遵循BSD开源协议,已在Python 2.6、2.7以及Python 3.3版本下进行了非常全面的测试。

特色


  • 强调REST

  • 非常全面的CRUD操作

  • 可自定义的资源端点

  • 自定义多个项目端点

  • 筛选和排序

  • 分页

  • HATEOAS

  • JSON和XML渲染

  • 条件请求

  • 数据完整性和并发控制

  • 多个添加操作

  • 数据验证

  • 可扩展的数据验证

  • 资源级缓存控制

  • 版本

  • 验证

  • CORS跨地资源共享

  • 默认情况下只读

  • 默认值

  • 预定义的数据库过滤器

  • 预测

  • 事件关联

  • 速率限制

  • MongoDB支持



和别的框架一样,咱们需要安装eve的模块

113532639.jpg



直接yum安装吧,我自己选择mongodb的时候,都会用10gen的。

cat /etc/yum.repos.d/10gen.repo

[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0


开始安装吧~

113307581.jpg



好了,咱们现在就测试下吧:

启动server.py还需要做的就是配上一个数据库、配置文件(默认是settings.py)、启动脚本来让API可以在线使用。总之,你会发现,配置和调优API是一件非常简单的事情。


server端

115007669.jpg

客户端

115032430.jpg


官方还推荐了一个eve demo

https://github.com/nicolaiarocci/eve-demo


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/"


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"}



124028547.jpg


我们从mongo的cli中瞅瞅,看看数据

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接口 ~

我对他的理解还是有些片面,有时间再好好看看。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值