Note that there are some explanatory texts on larger screens.

plurals
  1. PORESTful API design: best way to CRUD lightweight connections?
    primarykey
    data
    text
    <p>(Pardon the question title; hard to summarize this question.)</p> <p>On Facebook, you <code>like</code> things. On Twitter, you <code>follow</code> people. On GitHub, you also <code>follow</code> people and <code>star</code> repos and gists.</p> <p>All of these cases are pretty similar: those connections are lightweight, not really "resources" themselves. E.g. none of those three APIs expose public IDs for such connections.</p> <p>This raises the question: <strong>what's the "best" (in terms of REST) way to expose an API for creating/querying/deleting those connections?</strong></p> <hr> <p><strong>Facebook</strong> does [<a href="https://developers.facebook.com/docs/reference/api/" rel="nofollow">1</a>]:</p> <ul> <li><p><code>GET /:id/likes</code> to query an object's likes (more precisely, the users that like that object)</p></li> <li><p><code>POST /:id/likes</code> to like something (on behalf of the auth'ed user; no req body needed)</p></li> <li><p><code>DELETE /:id/likes</code> to unlike something (on behalf of the auth'ed user)</p></li> </ul> <p>The querying and creating make sense, but the <code>DELETE</code> is a bit "unRESTful", because you're not actually deleting the <code>/:id/likes</code> resource (an array of users that like that object).</p> <p>This discrepancy shows itself in another case [<a href="https://developers.facebook.com/blog/post/540/" rel="nofollow">2</a>]:</p> <ul> <li><code>GET /me/likes/:id</code> to query whether you like something</li> </ul> <p>So querying your connection is querying a totally different resource than creating or deleting it.</p> <hr> <p><strong>GitHub</strong> leans to the <code>/me/likes/:id</code> style for following users and starring <em>repos</em> [<a href="http://developer.github.com/v3/activity/starring/" rel="nofollow">3</a>]:</p> <p>(Note that GitHub's <code>/user</code> represents the auth'ed user, like Facebook's <code>/me</code>.)</p> <ul> <li><p><code>GET /user/starred/:owner/:repo</code> for querying whether you have a repo starred (returns 204 or 404, no body either way)</p></li> <li><p><code>PUT /user/starred/:owner/:repo</code> for starring a repo (no body needed in request)</p></li> <li><p><code>DELETE /user/starred/:owner/:repo</code> for unstarring a repo</p></li> </ul> <p>This is much more consistent, but unfortunately this separates individual "stars" from the group:</p> <ul> <li><code>GET /repos/:owner/:repo/stargazers</code> to query the users who've starred a repo</li> </ul> <hr> <p><strong>GitHub</strong>, interestingly, uses a different style for starring <em>gists</em> [<a href="http://developer.github.com/v3/gists/" rel="nofollow">4</a>]:</p> <ul> <li><p><code>GET /gists/:id/star</code> for querying whether you have a gist starred</p></li> <li><p><code>PUT /gists/:id/star</code> for starring a gist</p></li> <li><p><code>DELETE /gists/:id/star</code> for unstarring a gist</p></li> </ul> <p>This keeps the starring action with the gist resource, like Facebook, rather than the user resource.</p> <p>GitHub doesn't publicly expose <em>gists'</em> stargazers, but presumably it'd be e.g.:</p> <ul> <li><code>GET /gists/:id/stargazers</code> to query the users who've starred a gist</li> </ul> <p>While "stargazers" is indeed a different resource/name than "star", the names are similar and clearly related, and they're both on the same resource.</p> <p>The only downside I can think of to this is naming the resource. Something like <code>star</code> works, but actions like <code>follow</code> or <code>like</code> are trickier.</p> <hr> <p>(Not bothering to include the Twitter API as an example since it's hardly RESTful.)</p> <p>There's clearly no perfectly RESTful API for creating/querying/deleting things that aren't proper resources, but are there other pros/cons I'm not seeing, or other styles to consider?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload