restsocial-networkingrestful-urlsocial-network-friendship

RESTful URL design for a Friendship System


I am an API developer in my company. We are developing a social network. We are using a friendship system. We are using API-oriented system. The api I will be writing will be used by every platform we are developing.

I classically have a User resource and I am using a friendships table to track friendships:

+-----------+------------------+------+-----+---------------------+----------------+
| Field     | Type             | Null | Key | Default             | Extra          |
+-----------+------------------+------+-----+---------------------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| sourceId  | int(11)          | NO   | MUL | NULL                |                |
| targetId  | int(11)          | NO   |     | NULL                |                |
| status    | tinyint(1)       | NO   |     | 0                   |                |
| seen      | tinyint(1)       | NO   |     | 0                   |                |
| createdAt | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updatedAt | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| deletedAt | timestamp        | YES  |     | NULL                |                |
+-----------+------------------+------+-----+---------------------+----------------+

And now, I am trying to think about how I should implement the necessary URIs. But I couldn't get the idea of how I should implement it exactly. And I could only came up with this idea, and couldn't get it forward:

Let's say I have 2 users one with the username of john and the other with the username of jane.

jane wants to add john as a friend, and when to do so, she sends a POST request:

POST /users/john/friendships

john saw this, and want to deny or confirm it, to do so, I came up with 2 different ideas; 1st one is:

PUT /users/jane/friendships?action=(deny|confirm)

or 2nd one is:

PUT /users/jane/friendships/<friendshipId>?action=(deny|confirm)

Conventionally, the 2nd one seems more appropriate to me but then I think that I can just do

PUT /friendships/<friendshipId>?action=(deny|confirm)

So, I am stuck in here. All I think of this is these examples. I don't think these are the right ones.


Solution

  • John could add Jane as a friend (including a request body with the relevant information):

    POST /users/john/friends

    ...or, John could update some information about his friendship with Jane (again, with the request body)

    PUT /users/john/friends/jane

    ...or, John could 'unfriend' Jane.

    DELETE /users/john/friends/jane