How can i build batch the request and understand the response with help ZStream (ZIO)?

186 Views Asked by At

I have api that gets such request:

case class UsersRequest(ids: List[Long])

and returns such response:

case class UsersInfoResponse(info: List[Info])
case class Info(userId: Long, info: String)

also, i have methods that send this request and create user:

def createUser(id: Long): IO[Throwable, User] = {
 getUserInfo(id)
   .map(info => User(id, info))
}

def getUserInfo(id:Long): IO[Throwable, String] = {
   here i call grpc service
   service.getUserInfo(UsersRequest(List(id)))
}

I want:

  1. write ZStream that create ids batch
  2. every 1 sec takes 10 ids and creates UsersRequest
  3. gets UsersInfoResponse
  4. using id understands what info must gets
  5. return info

So that I can do it i should create something similar:

def getUserInfo(id:Long): IO[Throwable, String] = {
   Stream
      .fromQueue()
      .groupedWithin(10, Duration.Zero)
      .????
      .runDrain
      .forkManaged

   AND

      p <- Promise.make[Throwable, String]
      interrupted <- Promise.make[Nothing, Unit]
      env <- ZIO.environment[R]
}

I don't how i can do it. How can build batch and send the request and after match by id get result?

0

There are 0 best solutions below