How to build REST clients?

Short answer: Make them hypermedia-driven

In order to create a stateless API, driving process in the client application, hypermedia or links are used. This means that URLs are merely resources on a web server, obtained from other resources. Again, this implies that links should be read out of the API resources by looking for the relation rel="...". The links can, and will, change (non-breaking change), but the relations will not (breaking change).

An example: Let's say you want to do a search for ads. Your first impulse might very well be to look up the URL and just hard-code it.

https://cache.api.finn.no/iad/search/realestate-homes?orgId=1234567..

It looks pretty straightforward right? The problem is that the location of the search result (the URL above) might at some point in time change. If it does, your client will break ugly, and you will probably not notice until your customer(s) bother to give you a call or send an email.

Instead, you should:

  1. Get the Service Document
  2. Iterate over the elements in the Searches Workspace until you find the collection with the correct title. In this case: realestate-homes
  3. Get the href from that collection and store it in a variable. If you use multiple URLs, store them in a map with key=title and value=URL
  4. Add your query-parameters to the URL, preferably by using an URIBuilder in your programming language
  5. Do the search by calling the API with your URL

If you restart your application regularly you typically want to do points 1 - 4 at startup time. If not, repeat daily or so.

This approach applies to all links in the API.

Expect data to be missing

Even though a field always seem to be present, it most likely will not at some time.

Hence you should always check for its presence before you try to extract the value

© 1996 - 2022 FINN.no AS