2

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Umut Sirin
  • 2,462
  • 1
  • 21
  • 31

1 Answers1

5

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

tuespetre
  • 1,827
  • 2
  • 18
  • 30
  • Thanks for the response :) But is this the way you would do, or is it the fully RESTful approach? – Umut Sirin Jul 04 '13 at 19:43
  • Well, no "REST API" is truly "fully RESTful" because they all require some knowledge of the API beforehand, where REST describes an application that can be fully navigated and used starting from the root URI. But the options I answered with respect the HTTP verbs and their underlying concepts, which is what these APIs really typically strive for. – tuespetre Jul 04 '13 at 20:34
  • Yes, after some thinking, it came right to me too, using `/friends`instead of `/friendships`. Certainly, i need to make more research before i say _i know REST_. Anyway thank you so much for your input. – Umut Sirin Jul 04 '13 at 20:37
  • 1
    As an aside, a friendship can be considered confirmed when both users have POSTed each other to their friends. – tuespetre Jul 04 '13 at 20:39