How to create WSClient in Scala ?

2.8k views Asked by At

Hello I'm writing scala code to pull the data from API. Data is paginated, so I'm pulling a data sequentially. Now, I'm looking a solution to pulling multiple page parallel and stuck to create WSClient programatically instead of Inject.

Anyone have a solution to create WSClient ?

I found a AhcWSClient(), but it required to implicitly import actor system.

2

There are 2 answers

0
Lasf On

No need to reinvent the wheel here. And I'm not sure why you say you can't inject a WSClient. If you can inject a WSClient, then you could do something like this to run the requests in parallel:

class MyClient @Inject() (wsClient: WSClient)(implicit ec: ExecutionContext) {

  def getSomething(urls: Vector[String]): Future[Something] = {
    val futures = urls.par.map { url =>
      wsClient.url(url).get()
    }
    Future.sequence(futures).map { responses =>
      //process responses here. You might want to fold them together
    }
  }

}
2
Jethro On

When you cannot Inject one as suggested in the other answer, you can create a Standalone WS client using:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws._
import play.api.libs.ws.ahc.StandaloneAhcWSClient

implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val ws = StandaloneAhcWSClient()