Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding links to a python-eve API resource implementing HATEOAS

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": "[email protected]"
}

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": "[email protected]"
}

Where is "obvious" how to get the devices.

Thank you very much.

like image 256
MTG Avatar asked Jul 31 '26 10:07

MTG


1 Answers

You must add a link between a user => device too

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
      }
    }
like image 183
HOT_BBQ Avatar answered Aug 03 '26 00:08

HOT_BBQ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!