4

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?

Vlad
  • 41
  • 1
  • 1. I think you mean to write def createUser(id: Long): IO[Throwable, User] = { getUserInfo(id) .map(info => Info(id, info)) } not User, you didnt define such a class 2.Also why are you using a List inside UserRequest if you only wish to hold one long? 3.where is the source of the Id's? – YisraelU Nov 05 '21 at 13:02
  • This is just example, i want to be able to create batch from single request uses promise and Stream.fromQueue(), as it do kafka producer may be. But i do not want use callback. – Vlad Nov 08 '21 at 09:50
  • I want to hoard batch from single requests and when i have certain amount then i will send batch request, but i should understand response and do mapping with single request – Vlad Nov 08 '21 at 09:56

0 Answers0