Reputation: 12197
I am struggling to see how to structure a REST API where multiple owners may use the same resource. For example if there are users
and organizations
and each of them has something like images
or locations
. An Image and a location is a database field, which is the same for both users and organizations... I can see two basic ways to go about it
POST /users/:userID/documents
GET /users/:userID/documents/:id
POST /org/:orgID/documents
GET /org/:orgID/documents/:id
pros of separate endpoints: The logic is clean and everything is very obvious
cons of separate endpoints: There is a LOT of code copying between the endpoints.
Shared endpoints:
POST /documents
GET /documents/:id
pros of separate endpoints: There are fewer overall endpoints, less code copying.
cons of separate endpoints: The logic gets messy, IDK the best way to go about sending the orgID
or userID
with the request (without in effect creating a new endpoint within the /documents
resource. I could use a query string, but that just feels wrong... (IDK if it is wrong)
Any advice?
Upvotes: 0
Views: 436
Reputation: 5770
It all depends on the use case scenarios for your API.
There's nothing wrong with separate endpoints, if the flow would generally be to request users/organizations and their respective documents. It also doesn't have to mean code duplication - you can simply have some sort of common DocumentRepository in the backend, consumed in multiple places.
However, if the requirements are to simply search/display documents, a query string might be your best bet:
GET /documents?userId={userId}&organizationId={organizationId}
Upvotes: 1