Rest – the advantage of API clients

apiapi-designrest

At my new job I am working on a basic existing CRUD web application in MVC + with an Angular front-end.

The MVC API has a Swagger spec and it's used to generate an API client with auto rest. That API client is consumed by the front-end.

Now I understand the technical implementation but I am scratching my on why this is done? What is the advantage of an API client instead of just calling the API directly? This just seems like baseless redirection to me.

Can anyone explain what are the basic design decision on why API client should be used?

Best Answer

Advantages:

  • For particularly complex APIs, this can simplify the implementation for Front-End devs. As an example, it could hide any authentication / session management, allowing Front-End to build an object with the authentication parameters and then use a getData() method which can create new sessions as needed.
  • It can allow your API to be more complicated. For example, you could implement something to encrypt/compress/obscure data in transit (although I admit there are probably better examples).
  • It encourages Separation of Concerns / tidy code on the Front-End. It's generally bad practice to mix up different types of code, as things can get very messy to follow as the project grows.
  • It allows the API to be replaced in the future. Perhaps someone will re-implement the API client to point at another service with similar data (eg, switching from Yahoo Stocks to Google Stocks API). Having the API Client code already separates will greatly help with this.

The alternative you mention ("call the API Directly") how did you do this? You probably wrote the code to call the API, which would probably have been a good start for the API Client. why not let something auto-generate this for you?

Related Topic