rkm
rkm

Reputation: 3131

HATEOAS and discovery for existing resources

What is HATEOAS way of discovering existing resources? Let's say I have a collection of resources and normally client first gets the list of resources and then it can navigate to each resource from the list, because server provides information how to get each resource. But what if client needs to get particular resource directly without fetching list first?

An example could be a web application that has a page with list of items (for example users) /api/users and user can navigate to user page where client fetch single user /api/users/1234. Frontend app has its own routing and user page can have following URL https://example.com/users/1234/details). Now imagine that client opens user page directly, how it can know which endpoint to use to get user data from the server?

Upvotes: 0

Views: 120

Answers (1)

IMSoP
IMSoP

Reputation: 97783

I think you are conflating two different levels of interaction:

  • The user interacting with a front-end application
  • The front-end application interacting with an API

The front-end application might provide a page per user, a summary page for a group of users, or embed the user information in a widget on every page's header.

The API, on the other hand, might provide an end-point to list groups, one to list users in a group, one to get details of a particular user, etc. HATEOAS is talking about how an API client would discover these.

So in your scenario, it might look like this:

  • The user requests https://example.com/users/1234/details
  • The front-end application on https://example.com will have an internal function to fetch the user details for ID 1234
  • This will require multiple calls to the API, some of which may be loaded from a cache, e.g.:
    1. GET /users/ or GET /users/?filter=id:1234 - retrieve a list of zero or more user records
    2. If there are no results, or the results don't include the selected user, abort with an error
    3. Parse the user record for user ID 1234 and extract a link to the related user details resource
    4. GET the resource indicated by that link
  • The parsed result of that final API call is what will be returned by the internal function, and ultimately displayed to the user

Upvotes: 1

Related Questions