Matthew Hartz Matthew Hartz - 1 month ago 13
reST (reStructuredText) Question

Rest API Design Establishing Connection between the same resource

I have a resource called clients. We are now creating a system where we can link clients with other clients to create subclients. We are creating a table to represent this called clientconnections, which contains clientId and subclientId.

Now to establish this, we are doing it via an invite system (so we'll need an invite action api).

What is the best way to represent this in a restful way?

GET/PUT/POST
can be through a simple API such as:
/clients/{clientId}/connections


But establishing the connection via a rest API is hard. Here is my original idea... but i definitely think it's wrong.

POST
/clients/{clientid}/connections/{clientid2 doh}/invite


or this but this doesn't seem right either

POST
/clients/{clientid}/clients/{clientid2}/invite


Any suggestions would be great!

Thanks!

Answer

There's a bunch of different ways to think about this, I don't think there's one valid approach but I think I would do one of the following:

  1. Treat clients that have and have not accepted as the same thing, just with a different status.
  2. Treat 'invitations' as its own separate thing altogether. Only when the invitation has been accepted, a 'connection' is being created.

There is no real right or wrong here. My main advice would be is: keep it simple.