python restful框架比较_Eve——python restful框架初试

使用之前

安装 eve

安装 mongodb, 并启动它

选择 curl或者postman 用来测试,推荐postman

第一个demo

创建两个文件main.py 和 settings.py放在同一目录下,第一次用eve的两个坑,一个是mongodb没启动,一个就是这个setting.py了

settting.py是设置文件,包含各种配置,必须存在一个名为DOMAIN的字典,DOMAIN定义了要提供的资源 以字典形式表示

在settings.py写入如下内容

DOMAIN = {'people': {}}

main.py就不解释了

在main.py中写入如下内容

from eve import Eve

app = Eve()

app.run()

Eve是基于Flask的,所以这样的写法对于用过Flask的人应该很熟悉

然后运行 main.py 用postman做get请求访问 http://127.0.0.1:5000 可以得到类似下面的结果

{

"_links": {

"child": [

{

"title": "people",

"href": "people"

}

]

}

}

Eve的api遵循HATEOAS原则,关于HATEOAS,可以看维基和这篇文章

之后用postman访问 http://127.0.0.1:5000/people 这个资源,得到如下内容

{

"_items": [],

"_links": {

"self": {

"title": "people",

"href": "people"

},

"parent": {

"title": "home",

"href": "/"

}

},

"_meta": {

"max_results": 25,

"total": 0,

"page": 1

}

}

可以得到一个items列表,links和meta,items是该资源组中所有资源,links中包含self和parent,是指向自己和所在资源组的连接,meta是元信息,这里不讨论

默认情况下。这些api都是只读的,PUT,POST或者DELETE操作都会返回405

如果想要允许读写,在setting中写入

# 启用对资源组的增删查

# 如果忽略这一行,默认只提供查

RESOURCE_METHODS = ['GET', 'POST', 'DELETE']

# 启用对单个资源的增删改查

# 忽略情况下只提供查

ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']

这些设置是全局的,会影响到所有资源和资源组的设定,可以在各个资源点独立设定允许的方法

可选:

MONGO_HOST = 'localhost'

MONGO_PORT = 27017

MONGO_USERNAME = ''

MONGO_PASSWORD = ''

MONGO_DBNAME = 'apitest'

完善第一个demo

我们给第一个demo补上添加的功能

数据验证:

创建一个字典schema,定义people的结构

schema = {

'firstname':{

'type':'string',

'minlength': 1,

'maxlength':10,

},

'lastname':{

'type': 'string',

'minlength': 1,

'maxlength': 115,

'required': True

},

'role':{

'type':'list',

'allowed':["author","contributor","copy"],

},

'location':{

'type':'dict',

'schema':{

'address':{'type':'string'},

'city':{'type':'string'}

}

},

'born':{

'type':'datetime',

},

}

这是基于Cerberus语法的模式定义的,具体语法可以看Cerberus官网

之后我们设置people属性

people= {

'item_title': 'person',

# 默认情况下查找资源要同过/people/才能找到

# 这里添加新的只读路径,可以通过lastname来获得资源

'additional_lookup': {

'url':'regex("[\w]+")',

'field':'lastname',

},

# 控制缓存

'cache_control':'max-age=10,must-revalidate',

'cache_expires': 10,

# 覆盖全局的读写方法

'resource_methods':['GET','POST'],

# 设定结构

'schema':schema,

}

最后更新资源设置

DOMAIN={'people':people}

用postman做post添加数据

[{"firstname": "barack", "lastname": "obama"}, {"firstname": "mitt", "lastname": "romney"}]

然后查看数据 http://127.0.0.1:5000/people

{

"_items": [

{

"_id": "5a30e8527111c41e885ff38a",

"firstname": "barack",

"_etag": "adab38db64dea06ebfe1c0e783a28bde88565c3f",

"_links": {

"self": {

"href": "people/5a30e8527111c41e885ff38a",

"title": "person"

}

},

"_created": "Wed, 13 Dec 2017 08:44:02 GMT",

"lastname": "obama",

"_updated": "Wed, 13 Dec 2017 08:44:02 GMT"

},

{

"_id": "5a30e8527111c41e885ff38b",

"firstname": "mitt",

"_etag": "39426631ce8050ff7ad6982e02281289cff4919b",

"_links": {

"self": {

"href": "people/5a30e8527111c41e885ff38b",

"title": "person"

}

},

"_created": "Wed, 13 Dec 2017 08:44:02 GMT",

"lastname": "romney",

"_updated": "Wed, 13 Dec 2017 08:44:02 GMT"

}

],

"_meta": {

"page": 1,

"max_results": 25,

"total": 2

},

"_links": {

"parent": {

"href": "/",

"title": "home"

},

"self": {

"href": "people",

"title": "people"

}

}

通过新增加的路径查看 http://127.0.0.1:5000/people/obama

{

"_created": "Wed, 13 Dec 2017 07:24:04 GMT",

"lastname": "obama",

"_links": {

"parent": {

"title": "home",

"href": "/"

},

"collection": {

"title": "people",

"href": "people"

},

"self": {

"title": "person",

"href": "people/5a30d5947111c40f80df511e"

}

},

"_updated": "Wed, 13 Dec 2017 07:24:04 GMT",

"_etag": "94237cbea7a9a93d6db2dfb1c78a6ee84c4f8c89",

"_id": "5a30d5947111c40f80df511e",

"firstname": "barack"

}

感觉还行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值