Scala Play WS client making parallel REST calls dynamically

335 Views Asked by At

In my scala play framework application, I have to dynamically call multiple REST calls based on urls from a List.

I came across following code for multiple calls:

val futureResponse = for {
  responseOne <- WS.url(url1).get()
  responseTwo <- WS.url(url1).get()
  responseThree <- WS.url(url1).get()
} yield processCalls(responseOne, responseTwo, responseThree)

But how can I make it dynamic so that urls are picked from the List and responses are also stored in a list for further processing.

1

There are 1 best solutions below

0
Simon On

Something like this:

val urls: Seq[String] = ...

val results: Future[Seq[WSResponse]] = 
    Future.seq(
        urls.map(url => wsClient.url(url).get)
    )