Yevhenii Popadiuk
Yevhenii Popadiuk

Reputation: 1054

How to properly pass many dependencies (external APIs) to a class in Scala?

How to properly pass many dependencies (external APIs) to a class in Scala?

I'm working on the application that uses many APIs to collect data. For API's I have trait like below.

trait api {
  def url: Foo
  def parse: Bar
}

Also, there are about 10 implementations of the api trait (one for each API). Inside a parent actor, I want to create a child actor for every external API. I created new trait and implementation,

trait ExternalApis {
  val apiList: List[api]
}

object MyApis extends ExternalApis {
  val apiList = List(new ApiImpl1, ..., new ApiImpl10)
}

so now I can pass MyApis object (or any other implementation of ExternalApis) to parent actor and map over apiList for creating such child actors.

It seems to me I'm missing something. Are there more proper ways to do it?

Upvotes: 2

Views: 55

Answers (1)

Sohum Sachdev
Sohum Sachdev

Reputation: 1397

The implementation that you have made looks nearly ready. Just something I would like to add are:

  1. Passing an API List may not be the most ideal way to do such a thing. Anytime you would like to add/remove something to the API list, you would have to change different areas of the code. A suggestion would be to read this from a config folder, where the config folders would contain things such as url, username, password etc.

If you could provide more insight on the usage of these API's, it would help my answer a lot.

Hope this helped!

Upvotes: 1

Related Questions