jeudi 25 décembre 2014

Exposing relationships in REST APIs (how does the user refer to objects?)

In my models I have Events and Users. Each Event has a leader attribute that points to a User, however there isn't always a host necessarily, in which case host is null.


In my API:


GET /users/ returns a list of all users


GET /users/3/ returns user 3 info, {"name": "John", "href": "/users/3/"}


GET /events/ returns a list of all events


GET /events/2/host/ should return the user who is a host for event 2. (say this is user 3)


Should I return {"href": "/users/3/"}, simply a link to the User? Or the representation of the user itself, {"name": "John", "href": "/users/3/"}?


Lets say I go with the first option. How do I change the user?


PUT /events/2/host/ json={"href": "/users/2/"} - does this make sense? My server would then have to parse the endpoint to figure out which user id is the new host, and then assign that in the database. Seems a bit inelegant, but I think Flask should have a way to parse the endpoint (since it does the same thing for routing itself).


Finally, in the GET /events/2/ resource, I was thinking about having an attribute "host":{"href": "/events/2/host/"} in the representation. Does this make sense? And if there's no host, there would be no href attribute at all, just an empty dictionary assigned to host.





Aucun commentaire:

Enregistrer un commentaire