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

sup.modules.sttp.scala Maven / Gradle / Ivy

There is a newer version: 0.9.0-M7
Show newest version
package sup.modules

import cats.Functor
import cats.effect.Sync
import com.softwaremill.sttp.{Id, Request, SttpBackend}
import sup.{HealthCheck, HealthResult}
import cats.implicits._

object sttp {

  /**
    * Checks if a request made with a client has a successful status (1xx-3xx).
    * */
  def statusCodeHealthCheck[F[_]: Functor, T](call: Request[T, Nothing])(
    implicit backend: SttpBackend[F, Nothing]): HealthCheck[F, Id] = {
    HealthCheck.liftFBoolean {
      backend.send(call).map { response =>
        !response.isClientError && !response.isServerError
      }
    }
  }

  /**
    * Fetches the result of a remote health check.
    * */
  def remoteHealthCheck[F[_]: Sync, H[_]](call: Request[HealthResult[H], Nothing])(
    implicit backend: SttpBackend[F, Nothing]): HealthCheck[F, H] = {
    HealthCheck.liftF {
      backend.send(call).flatMap(response => Sync[F].delay(response.unsafeBody))
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy