Handle Error elegantly with Scala sttp client

592 Views Asked by At

I'm using the scala sttp client and in particular, using the Monix backend and I have the following code:

def httpTask(href: String): Task[HttpBinaryResponse] = {
  val request = basicRequest
    .get(uri"$href")

  AsyncHttpClientMonixBackend
    .resource()
    .use { backend =>
      request.send(backend).map {
        response: Response[Either[String, String]] =>
          println(s"For URL: $href >> Got response code: ${response.code}")
          HttpBinaryResponse(
            href,
            response.code.isSuccess,
            response.headers.map(elem => (elem.name, elem.value)))
      }
    }
}

I have a set of URL's that I run through this method:

hrefs.toSet.flatten.filter(_.startsWith("http")).map(httpTask) // Set[Task[HttpBinaryResponse]] 

In my caller function, I then execute the task:

val taskSeq = appBindings.httpService.parse(parserFilter)
val chunks = taskSeq.sliding(30, 30).toSeq
val batchedTasks = chunks.map(chunk => Task.parSequence(chunk))
val allBatches = Task.sequence(batchedTasks).map(_.flatten)

I then pattern match on the materialized type and return, bit what I want here is not just a Success and a Failure, but I would like to have both the values at once:

allBatches.runToFuture.materialize.map {
          case Success(elem) =>
            Ok(
              Json
                .obj("status" -> "Ok", "URL size" -> s"${elem.size}")
            ).enableCors
          case Failure(err) =>
            Ok(
              Json
                .obj("status" -> "error", "message" -> s"${err.getMessage}")
            ).enableCors
        }

I need to get a List of URL's with a corresponding Success or a Failure message. With this approach of what I have, I get only either one of those, which is obvious as I pattern match. How can I collect both Success and Failure?

0

There are 0 best solutions below