Reputation: 1054
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
Reputation: 1397
The implementation that you have made looks nearly ready. Just something I would like to add are:
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