Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple endpoints to expose different views of the same resource

I'm struggling to find an answer to this (perhaps because I'm not asking the question properly) ...

I'm building API to expose resources for a basic social networking service I'm creating. My understanding is that the structure of URLs in an API is essentially a hierarchy, directory like structure. I think that means I can have multiple endpoints to reach the same resources or collections of resource links. For example:

I have an endpoint

www.domain.api.org/users/{:uid}/posts

Which will return all posts sent by the user or that the user is tagged in. Seems ok, but what if I have an endpoint such as:

www.domain.api.org/posts

Which when hit with a http GET will return all public posts (i.e. all users' posts plus his friends' and public posts).

The difference is that the first URL points to user owned resources whereas the second to public ones (of which the users posts are included of course) Are these okay or am I doing it the wrong / less sensible way?

To reiterate, can I have multiple endpoints which point to different contexts/views of the same resource?

like image 418
apostrophedottilde Avatar asked Dec 09 '15 02:12

apostrophedottilde


People also ask

Can one API have multiple endpoints?

Yes , API endpoints are URL's and one API could have several of them especially due to versioning. The endpoints indicate how you access the resource, while the method indicates the allowed interactions (such as GET, POST, or DELETE) with the resource.

Can two different URIs return the same resource?

A URI can point to at most one resource, but many URIs can point to the same resource. A many-to-one relationship between URIs and resources, if you will.

What is a resource endpoint?

The term endpoint is focused on the URL that is used to make a request. The term resource is focused on the data set that is returned by a request. Now, the same resource can often be accessed by multiple different endpoints. Also the same endpoint can return different resources, depending on a query string.


2 Answers

Basically multiple endpoints for the same resources should be avoided. However in this particular case it does make sense.

What you can do is to introduce optional query param userId to the following endpoint:

www.domain.api.org/posts/?userId=<userId>

If this substitutes the first endpoint you mentioned that's the way to go.

like image 199
Opal Avatar answered Oct 20 '22 00:10

Opal


I would like to add ontop of @Opal's answer.

Are these okay or am I doing it the wrong / less sensible way?

Ideally, like Opal mentioned, you would use queryParams in your url. For many applications I have build, I don't know the uids returned from the api beforehand, so selecting an item and passing it inside my url as a query parameter makes sense. But it also has the added benefit of having your key inside your url, allowing you to bookmark it, pass the url to another user and they will automatically see the same data you want them to see.

To iterate: Is your current implementation wrong? No, but ideally you would use a combination of both route parameters are query parameters to achieve this

like image 33
Maartenw Avatar answered Oct 19 '22 23:10

Maartenw