All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.touk.nussknacker.engine.sttp.SttpJson.scala Maven / Gradle / Ivy

The newest version!
package pl.touk.nussknacker.engine.sttp

import io.circe.{Decoder, Error}
import sttp.client3.circe.deserializeJson
import sttp.client3.{HttpError, Response, ResponseAs, ResponseException, asString}
import sttp.model.StatusCode
import sttp.monad.MonadError

import scala.concurrent.Future
import scala.language.higherKinds

object SttpJson {

  def failureToError[F[_], T](
      response: Response[Either[ResponseException[String, Error], T]]
  )(implicit monadError: MonadError[F]): F[T] = response.body match {
    case Right(qr)   => monadError.unit(qr)
    case Left(error) => monadError.error(error)
  }

  def failureToFuture[T](response: Response[Either[ResponseException[String, Error], T]]): Future[T] =
    response.body match {
      case Right(qr)   => Future.successful(qr)
      case Left(error) => Future.failed(error)
    }

  // we want to handle 404 as None
  def asOptionalJson[Type: Decoder]: ResponseAs[Either[ResponseException[String, Error], Option[Type]], Any] =
    asString.mapWithMetadata[Either[ResponseException[String, Error], Option[Type]]] {
      case (Right(data), _) =>
        val deserialize = ResponseAs.deserializeWithError(deserializeJson[Option[Type]])
        deserialize(data)
      case (Left(_), meta) if meta.code == StatusCode.NotFound => Right(None)
      case (Left(s), meta)                                     => Left(HttpError(s, meta.code))
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy