Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume a HATEOAS REST API in Angular?

I'm working on an Angular 4 front-end for an API built by another team. The API follows HATEOAS and provides me with hypermedia links with every single response.

I know the shape of the API and I figure I can just hard-code the URLs into Angular Services with minimal fuss. However, a colleague (who is a backend developer) is trying to convince me that I should take full advantage of the hypermedia because it will mean less coupling between the frontend and backend (and potential breakage if the API changes).

However, I'm stumped on how I'd even go about implementing a simple HATEOAS pattern using Angular's built-in Http service. How would I store/share the hypermedia/URL information in a way that doesn't couple all the services together and make them hard-to-test? There seems to be no examples out there.

Would trying to create a HATEOAS-friendly HTTP client even be a good idea, or is it likely not worth the trouble?

like image 893
Jon Gunter Avatar asked Jul 25 '17 22:07

Jon Gunter


People also ask

What is HATEOAS how do you implement HATEOAS for a resource?

To implement HATEOAS, we would need to include related resources in the response. Instead of Student we use a return type of EntityModel<Student> . EntityModel is a simple class wrapping a domain object and allows adding links to it. We create a new resource.

What is HATEOAS spring?

The Spring HATEOAS project is a library of APIs that we can use to easily create REST representations that follow the principle of HATEOAS (Hypertext as the Engine of Application State).

What is HATEOAS stackoverflow?

HATEOAS stands for Hypertext As The Engine Of Application State. It means that hypertext should be used to find your way through the API.


1 Answers

Your colleague is right, you should use the meta information that the back-end provides. In this way you are not putting responsibility on the client that doesn't belong there. Why should the client know from where to fetch the entities? Storing the entities (in fact the data in general) is the responsibility of the back-end. The back-end owns the data, it decides where to put it, how to access it, when to change the location or the persistence type, anything related to storing the data.

How would I store/share the hypermedia/URL information in a way that doesn't couple all the services together and make them hard-to-test?

Why do you think using HATEOAS makes the testing harder? It does not, in fact not using it makes the testing harder as the URLs are static which makes the back-end non-stub-able.

You can extract the information from the back-end response and store it as meta-information in the angular model, on a _meta key or something like that.

like image 140
Constantin Galbenu Avatar answered Oct 06 '22 00:10

Constantin Galbenu