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.

3

2 Answers

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() 
2

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 } } } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.