免责声明:我是sttp和莫尼克斯的新手,这是我想了解更多关于这些库的尝试。我的目标是通过HTTP请求、->解析JSON响应->从给定的API获取数据(客户端),将这些信息写入数据库。我的问题只涉及第一部分。我的目标是以异步(希望是快速)的方式运行get请求,同时具有避免或处理速率限制的方法。
下面是我已经尝试过的内容片段,似乎只适用于一个请求:
package com.github.client
import io.circe.{Decoder, HCursor}
import sttp.client._
import sttp.client.circe._
import sttp.client.asynchttpclient.monix._
import monix.eval.Task
object SO extends App {
case class Bla(paging: Int)
implicit val dataDecoder: Decoder[Bla] = (hCursor: HCursor) => {
for {
next_page <- hCursor.downField("foo").downArray.downField("bar").as[Int]
} yield Bla(next_page)
}
val postTask = AsyncHttpClientMonixBackend().flatMap { implicit backend =>
val r = basicRequest
.get(uri"https://foo.bar.io/v1/baz")
.header("accept", "application/json")
.header("Authorization", "hushh!")
.response(asJson[Bla])
r.send() // How can I instead of operating on a single request, operate on multiple
.flatMap { response =>
Task(response.body)
}
.guarantee(backend.close())
}
import monix.execution.Scheduler.Implicits.global
postTask.runSyncUnsafe() match {
case Left(error) => println(s"Error when executing request: $error")
case Right(data) => println(data)
}
}我的问题:
另外,我在使用另一个后端方面也很灵活,如果这将支持限制利率的目标的话。
发布于 2020-08-05 21:26:35
您可以像这样使用monix.reactive.Observable
Observable.repeatEval(postTask) // we generate infinite observable of the task
.throttle(1.second, 3) // set throttling
.mapParallelOrderedF(2)(_.runToFuture) // set execution parallelism and execute tasks
.subscribe() // start the pipline
while (true) {}https://stackoverflow.com/questions/63254339
复制相似问题