添加到实现HATEOAS的python eve API资源的链接
我正在使用构建一个API 我的设计很简单,它有两个资源,用户和设备:添加到实现HATEOAS的python eve API资源的链接,python,hateoas,eve,Python,Hateoas,Eve,我正在使用构建一个API 我的设计很简单,它有两个资源,用户和设备: /用户[/ID] /用户/ID/设备[/ID] 代码是(settings.py)是: 默认情况下,HATEOAS处于启用状态。 在前面的参考资料中,我希望有一个指向用户设备的链接,指向/users/54465ae80640fd0f60f6aa09/devices,因为此端点存在,在代码(用户设备)中定义,并且工作正常 我可以让pyhon eve了解用户和用户设备之间的关系,将此设备链接添加到用户资源?否则,用户54465a
- /用户[/ID]
- /用户/ID/设备[/ID]
{
"_updated": "Tue, 21 Oct 2014 13:08:56 GMT",
"_etag": "d6ff9457f5b196a8c245a7dc91e7fca0d28c5268",
"_links": {
"self": {
"href": "/users/54465ae80640fd0f60f6aa09",
"title": "user"
},
"devices": {
"href": "/users/54465ae80640fd0f60f6aa09/devices",
"title": "devices"
},
"parent": {
"href": "",
"title": "home"
},
"collection": {
"href": "/users",
"title": "users"
}
},
"_created": "Tue, 21 Oct 2014 13:08:56 GMT",
"_id": "54465ae80640fd0f60f6aa09",
"nickName": "superuser",
"email": "super@user.com"
}
哪里是“显而易见的”如何获取设备
非常感谢。您还必须在用户=>设备之间添加链接
users_schema = {
'nickName': {
'type': 'string',
'required': True,
},
'data_relation': {
'resource': 'user_devices', # link to device on a field _id
'field': '_id',
'embeddable': True
},
'email': {
'type': 'string',
'required': True,
'unique': True
}
}
我创建了一个自定义挂钩,将一个项目与其他集合链接起来 将下一个代码添加到应用程序中,然后定义项目与相关集合之间的数据关系 例如,我的菜单由父节点链接,因此我的模式上有两个关系:
- 父菜单为当前项-->子菜单的项
- 父菜单-->子菜单
def createLink(ref_collection, ref_field, ref_value):
print(f"createLink({ref_collection}, {ref_field}, {ref_value})")
ref_value = f"{ref_value}"
linkSufix = "/" + ref_value if ref_field == "_id" else "?where={\"" + ref_field + "\":\"" + ref_value + "\"}"
linkRel = {
"title": ref_collection,
"href": ref_collection + linkSufix,
}
print(f"createLink result: \n{linkRel}")
return linkRel
def add_links(resource_name, resource, schema):
linked_item_keys = list(key for key in schema if "data_relation" in schema[key] and key in resource)
print(f"linked_item_keys: {linked_item_keys}")
for key in linked_item_keys:
print(f"link needed for: {resource_name}.{key}")
itemSchema = schema[key]
item = resource[key]
data_relation = itemSchema["data_relation"]
ref_collection = data_relation["resource"]
ref_field = data_relation["field"]
link = createLink(ref_collection, ref_field, item)
links = resource["_links"] if "_links" in resource else []
link_rel = ref_collection if resource_name == ref_collection and key == "_id" else key
links[link_rel] = link
resource["_links"] = links
def add_links_to_item(resource_name, response):
print(f"------------------------------------")
print(f"(on_fetched_item) {resource_name} ")
print(f"response: \n{response}")
schema = config.DOMAIN[resource_name]["schema"]
print(f"""schema: \n{schema}""")
add_links(resource_name, response, schema)
app = Eve()
app.on_fetched_item += add_links_to_item
希望有帮助!!嗨!谢谢!我试过了,但我仍然没有在“用户”链接中与“用户设备”有任何关系。你能提供完整的代码吗?再次感谢!数据关系它提供的唯一功能是渲染完整的对象添加
?embedded={“用户设备”。\u id:1}
在查询结束时。预期的HATEOAS行为是创建一个新的链接部分,我认为Eve开箱即用没有涵盖该部分
users_schema = {
'nickName': {
'type': 'string',
'required': True,
},
'data_relation': {
'resource': 'user_devices', # link to device on a field _id
'field': '_id',
'embeddable': True
},
'email': {
'type': 'string',
'required': True,
'unique': True
}
}
"menu": {
"type": "dict",
"schema": {
"_id": {
"type": "objectid",
"data_relation": {
"resource": "menu",
"field": "parent_menu",
"embeddable": False,
}
},
"id": { "type": "string" } ,
"label": { "type": "string" },
"url": { "type": "string" },
"parent_menu": {
"type": "objectid",
"data_relation": {
"resource": "menu",
"field": "_id",
"embeddable": True,
},
},
},
},
{
"_id":"5c6ab8a5467a938b027aae64",
"id":"root",
"label":"Home",
"url":"/",
"_links":{
...
"self":{
"title":"Menu",
"href":"menu/5c6ab8a5467a938b027aae64"
},
...
"menu":{
"title":"menu",
"href":"menu?where={\"parent_menu\":\"5c6ab8a5467a938b027aae64\"}"
}
}
}
{
"_id":"5c6ab8a5467a938b027aae65",
"id":"submenu1",
"label":"Submenu1",
"url":"/#submenu1",
"parent_menu":"5c6ab8a5467a938b027aae64",
"_links":{
...
"self":{
"title":"Menu",
"href":"menu/5c6ab8a5467a938b027aae65"
},
...
"menu":{
"title":"menu",
"href":"menu?where={\"parent_menu\":\"5c6ab8a5467a938b027aae65\"}"
},
"parent_menu":{
"title":"menu",
"href":"menu/5c6ab8a5467a938b027aae64"
}
}
}
def createLink(ref_collection, ref_field, ref_value):
print(f"createLink({ref_collection}, {ref_field}, {ref_value})")
ref_value = f"{ref_value}"
linkSufix = "/" + ref_value if ref_field == "_id" else "?where={\"" + ref_field + "\":\"" + ref_value + "\"}"
linkRel = {
"title": ref_collection,
"href": ref_collection + linkSufix,
}
print(f"createLink result: \n{linkRel}")
return linkRel
def add_links(resource_name, resource, schema):
linked_item_keys = list(key for key in schema if "data_relation" in schema[key] and key in resource)
print(f"linked_item_keys: {linked_item_keys}")
for key in linked_item_keys:
print(f"link needed for: {resource_name}.{key}")
itemSchema = schema[key]
item = resource[key]
data_relation = itemSchema["data_relation"]
ref_collection = data_relation["resource"]
ref_field = data_relation["field"]
link = createLink(ref_collection, ref_field, item)
links = resource["_links"] if "_links" in resource else []
link_rel = ref_collection if resource_name == ref_collection and key == "_id" else key
links[link_rel] = link
resource["_links"] = links
def add_links_to_item(resource_name, response):
print(f"------------------------------------")
print(f"(on_fetched_item) {resource_name} ")
print(f"response: \n{response}")
schema = config.DOMAIN[resource_name]["schema"]
print(f"""schema: \n{schema}""")
add_links(resource_name, response, schema)
app = Eve()
app.on_fetched_item += add_links_to_item