dimanche 4 novembre 2018

Rails design pattern to consuming two different API's

Imagine I have two api's with different resources,

www.api-A.com/consumers

Returns: { consumers: ['mike', 'Anna', 'Danilo'] }

www.api-B.com/clients

Returns: { clients: ['Jack', 'Bruce', 'Mary'] }

I would like to consume these two listing the results in one Rails Controller

My doubt: Do I have to create a wrapper for every api like:

module ApiAWrapper
  #code here
end

module ApiBWrapper
  #code here
end

and inside my controller I call.

MyController
  def index
    @clients << ApiAWrapper.most_recent
    @clients << ApiBWrapper.most_recent
    @clients
  end
end

Now @clients will have

['mike', 'Anna', 'Danilo', 'Jack', 'Bruce', 'Mary']

Is this the right way to consume these different API's with similar responses?

Is there a design pattern that I can use or should read it about to guide me?

Because I want to treat them like if there were just one.

Aucun commentaire:

Enregistrer un commentaire