I have a service class, and the service have one method getSomethingFromApi
, now , I want to have play Configuration instance so I can pull stuff from the application.conf, and a play WSClient so I can perform http calls.
this is how I want my service to look:
class MyApiService {
def getSomethingFromApi(whichApi: String): Future[ApiRes] = {
wsClient.url(configuration.getString(whichApi)).withHttpHeaders(("Content-Type", "application/json")).get.map { res =>
response.status match {
case Status.OK => // do something
case _ => throw new Exception
}
}
}
}
and this is the ServicesModule that is wiring my services:
import com.softwaremill.macwire._
trait ServicesModule {
lazy val myService: MyApiService = wire[MyApiService]
}
my question now is what is the right way of using wiring play Configuration and WSClient instances..? cause currently i need those instances in my service but i dont have them, how should i do this the right way? thanks
For the configuration I suggest using something like PureConfig and load the configuration as follows
This then can be passed on to any component of your app using macwire.
As of Play 2.6.X one should use
AhcWSComponents
that are provided by thews
dependency as follows:In your build.sbt file add the ws dependency to your project
In your module trait mix-in the
AhcWSComponents
trait and wire theWSClient
In your
MyApiServic
e add theWSClient
as a param. to the constructorAnd now you're done. This general rule applies to all provided dependencies.