Home > Enterprise >  Best practises for consuming multiple third party APIs in existing API
Best practises for consuming multiple third party APIs in existing API

Time:10-27

I'm trying to find out the best approach for designing the following scenarios.

Let's assume, that I already have, a REST API implementation, which is going to fetch books, from different providers and serve them back, to my own client.

  1. Each provider offers a separate API for serving books to its consumers.
  2. Each provider, has a completely different structure on the response from the previous one.
  3. My client should always get exactly the same format, so the frontend-client application can handle the responses.

Some output examples:

Provider 1

[
  {
    "bookId": 32,
    "bookName": "foo",
    "author" : {
      "name" : "John",
      "surname": "Doe"
    },
    "publisher": {
      "name" : "Foo Books",
      "address": "New York"
    }
  },
  ...
]

Provider 2

[
  {
    "publisherCompany": {
      "name": "Foo Books",
      "position": "New York",
      "books": [
        {
          "id": 32,
          "title": "Foo",
          "author": {
            "name" : "John",
            "lastName": "Doe"
          } 
        },
        ...
      ]
    }
  },
 ...
]

Both of those 2 providers, are outputting the same values, but in different format. Moreover some keys are completely different.

What I'm looking for, is an Architectural Design or a Design Pattern, so i can map each different output, to my own format.

What i have tried in the past

  • I had created my own Entities
  • I had different services for each consumer and after fetching the data, i was instantiating my own entities and mapped the responses accordingly.
  • After having my objects (Entities) filled with data, i was passing them to a transformer function to transform them to the desired output.

My questions:

  • Is there any Design Pattern for this?
  • Is there any terminology for this scenario so i can do a research?
  • In which way you would approach this scenario?
  • Any resources for reading ?

Thanks ;)

CodePudding user response:

Not everything must follow a design pattern or have a name, just use your knowledge and common sense.

In this case, what you want is the following:

  • Have your own domain model and a Service layer that would get the data from all your Providers.
  • Have a specific model for Provider 1 model and a specific Service layer that would handle the call to Provider 1 and the mapping to your own domain model.
  • Have a specific model for Provider 2 model and a specific Service layer that would handle the call to Provider 2 and the mapping to your own domain model.

Both Services related to Provider 1 and 2 should implement the same Interface that specifies the contract for additional Provider Services that you might need. This would be the Interface that your domain Service layer would know and use.

This is more or less what you already have but if you want to give this a name, you can read about Hexagonal Architecture which has some similarities with the described solution. I suggest the following articles:

  • Related