'How to avoid 429 error and handle retry on batch get requests in Scala?

I looked around but there is no general answer.

Using the "sttp client3" library

I'm sending http requests on a web page to grab its content. The code loops through a list of strings to make the requests. The problem is that I get many 429 code. Putting thread.sleep doesn't seem to be a good way of handling this I read.

Here's my get method:

def getOnMasterNode(url: String): Either[String, String] = {
    val request = basicRequest.get(uri"$url")
    val response = request.send(backend)

    response.body
}

and here's the part where I send all the request:

urls.foreach(item => DataGet.getOnMasterNode(BASEURL+item) match {
    case Left(body) => DataGet.writeDataToFile(body,
      BASEPATH_MN + "-" + item.split("/").toList.last + day + ".html"
    )
    case Right(_) => LOGGER.error("Error while fetching the data page")
  })

What is a correct way of handling this kind of errors?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source