I am building an API using python-eve.
My design is something simple, it has two resources, users and devices:
- /users[/ID]
- /users/ID/devices[/ID]
The code is (settings.py) is:
users_schema = {
'nickName': {
'type': 'string',
'required': True,
},
'email': {
'type': 'string',
'required': True,
'unique': True
}
}
devices_schema = {
'name': {
'type': 'string',
'required': True,
},
'user_id': {
'type': 'objectid',
'data_relation': {
'resource': 'users',
'embeddable': True
},
}
}
users = {
'item_title': 'user',
'url': 'users',
'schema': users_schema,
}
user_devices = {
'resource_title': 'devices',
'url': 'users/<regex("[a-f0-9]{24}"):user_id>/devices',
'schema': devices_schema,
'datasource': {
'source': 'devices',
}
}
DOMAIN = {
'users': users,
'user_devices': user_devices
}
If I create an user, the user resource looks like (/users/54465ae80640fd0f60f6aa09):
{
"_updated": "Tue, 21 Oct 2014 13:08:56 GMT",
"_etag": "d6ff9457f5b196a8c245a7dc91e7fca0d28c5268",
"_links": {
"self": {
"href": "/users/54465ae80640fd0f60f6aa09",
"title": "user"
},
"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"
}
HATEOAS is enabled by default. In the previous resource, I was expecting a link to the user devices, to /users/54465ae80640fd0f60f6aa09/devices, because this endpoint exist, is defined in the code (user_devices), and works fine.
Who can I make pyhon-eve understand the relation between user and user-devices, to add this devices links to the user resource? Otherwise, the user 54465ae80640fd0f60f6aa09 will not know how to get the devices.
I am expecting something like:
{
"_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"
}
Where is "obvious" how to get the devices.
Thank you very much.