我有一个相当简单的用例。我有两个 Web 服务调用,一个获取产品,另一个获取关系。我想运行 fetchProducts() 首先从产品集中提取一个字段,然后将输出传递给 fetchRelationships(ids: Seq[String]) 以便我可以在产品上重新设置关系。这是代码:
def fetchProducts(): Stream[IO, Seq[Product]]= {
//webservice call
}
def fetchRelationship(ids: Seq[Product]): Stream[IO, Seq[Relationship]] = {
//webservice call
}
//Pseudocode. How can I do this with fs2 Streams?
def process = {
val prods = fetchProducts() //run this first
val prodIds = prods.flatMap(i => i.productId)
val rels = fetchRelationships(prodIds) //run after all all products are fetched
prods.forEach(p => p.setRelation(rels.get(p.id))
}
}
case class Product(productId: Option[String],
name: Option[String],
description: Option[String],
brandName: Option[String])
我受到外部 Api 的限制,无法批量获取结果。所以我不确定如何使用 fs2 来表达这一点,或者我是否应该使用它。