6

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?

  • Is your question why it is auto-generated, or merely why one would want an additional layer of indirection? – Robert Harvey Jul 19 '16 at 15:13
5

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?

0
  • You can use API clients to abstract away some of the HTTP details from consumers (resource URIs, handling response errors, etc.) It is usually simpler to call a simple method on an object and get a result back than building an HTTP request and managing the response.

    This might be seen as spoon-feeding the consumer devs, but can be suitable for less experienced teams or devs that can't be bothered to use a REST API (which is admittedly a debatable view).

  • Another advantage is that it gives you more control over access to your API if you have multiple consumers. If you bundle the client with a package management system, you get all the benefits in terms of version policy and deployability. You can also track more accurately which consumer uses which version, force consumers to specify stuff when using the client so that you have statistics, etc.

  • Why would this need to be auto-generated? – Robert Harvey Jul 19 '16 at 14:52
  • It doesn't need to (and that's not the question, I think). – guillaume31 Jul 19 '16 at 15:05
-2

Can anyone explain what are the basic design decision on why API client should be used. What are really nice use cases for such a pattern etc.

If you have 3 customers, with different implementations, how will you separate your code? Will you write 3 different web sites, and merge your changes? Will you write optional parameters? I hope not.

A reason to use the API route, is separation of responsibilities. The url, http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-1, explains rather well how the cloud will assist with your REST API. I hope this helps!

  • 1
    Doesn't answer the question of why you would need a generator to make the API client code. – RemcoGerlich Jul 19 '16 at 13:20
  • you make stack overflow annoying. Can anyone explain what are the basic design decision on why API client should be used? – StackOverFowl Jul 19 '16 at 15:54
  • He is asking about the use of API clients auto generated from a swagger spec. You are just explaining why APIs are useful in general, he understands that. – RemcoGerlich Jul 19 '16 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.