Search code examples
scalaziosttp

How do I make 10 http requests simultaneously?


I have 10 requests

  val reqTemplate = quickRequest.post(uri"http://localhost:8280/delay/1")
  val requests = for {i <- 1 to 10} yield {
    reqTemplate.body(s"{test:$i}")
  }

How do I send these requests simultaneously?

I tried


  val responseTasks = reqs.map(SttpClient.send)
  val allResponsesTask: Task[List[Response[String]]] = ZIO.collectAllPar(responseTasks)

  val appLogic: Task[Unit] = for {
    resps <- allResponsesTask
    resp <- resps
    _ <- putStrLn(resp.toString())
    _ <- putStrLn("Done")
  } yield ()

  Runtime.default.unsafeRunAsync(appLogic)

But that is giving me all sorts of fun compile errors.

Error:(18, 74) type mismatch;
 found   : scala.collection.immutable.IndexedSeq[zio.ZIO[sttp.client.asynchttpclient.zio.SttpClient,Throwable,sttp.client.Response[String]]]
    (which expands to)  scala.collection.immutable.IndexedSeq[zio.ZIO[zio.Has[sttp.client.SttpBackend[zio.Task,Nothing,sttp.client.asynchttpclient.WebSocketHandler]],Throwable,sttp.client.Response[String]]]
 required: Iterable[zio.ZIO[Any,Throwable,sttp.client.Response[String]]]
  val allResponsesTask: Task[List[Response[String]]] = ZIO.collectAllPar(responseTasks)
Error:(23, 7) type mismatch;
 found   : zio.ZIO[zio.console.Console,Nothing,Unit]
    (which expands to)  zio.ZIO[zio.Has[zio.console.Console.Service],Nothing,Unit]
 required: scala.collection.GenTraversableOnce[?]
    _ <- putStrLn(response.toString())
Error:(22, 14) type mismatch;
 found   : List[Nothing]
 required: zio.ZIO[Any,Throwable,Unit]
    response <- responses

Solution

  • The problem is that you have declared the an Sttp dependency but you haven't provided it.

    If you annotate the responsesTask you'll see it is actually of type Seq[ZIO[SttpClient, Throwable, Response[String]]] meaning that you need to provide a client in order to satisfy the dependency.

    I would change it to something like:

      val allResponsesTask: Task[List[Response[String]]] = 
        ZIO.collectAllPar(responseTasks)
          .provideLayer(AsyncHttpClientZioBackend.layer())
    

    Note that I added the layer here for simplicity sake, but generally you want to add all layers at the edge of your application, since creating a layer is not free.