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

io.github.felixbr.finagle.http.effect.client.FinagleHttpClientBuilder.scala Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package io.github.felixbr.finagle.http.effect.client

import cats.effect._
import com.twitter.finagle.{Http, Name, Service}
import com.twitter.finagle.Http.Client
import com.twitter.finagle.http.{Request, Response}
import io.github.felixbr.finagle.core.effect.WrappedService

import scala.concurrent.duration._

object FinagleHttpClientBuilder {

  def apply[F[_]: Sync: Async: ContextShift]: FinagleHttpClientBuilder[F] =
    new FinagleHttpClientBuilder[F]
}

final case class FinagleHttpClientBuilder[F[_]: Sync: Async: ContextShift](
  private val underlyingBuilder: Client = Http.client,
  private val closeTimeout: FiniteDuration = 1.second
) {

  def withUpdatedConfig(f: Client => Client): FinagleHttpClientBuilder[F] =
    copy(underlyingBuilder = f(underlyingBuilder))

  def withCloseTimeout(duration: FiniteDuration): FinagleHttpClientBuilder[F] =
    copy(closeTimeout = duration)

  def serviceResource(
    dest: String,
  ): Resource[F, WrappedService[F, Request, Response]] =
    makeServiceResource(underlyingBuilder.newService(dest), closeTimeout)

  def serviceResource(
    dest: String,
    label: String,
  ): Resource[F, WrappedService[F, Request, Response]] =
    makeServiceResource(underlyingBuilder.newService(dest, label), closeTimeout)

  def serviceResource(
    dest: Name,
    label: String,
  ): Resource[F, WrappedService[F, Request, Response]] =
    makeServiceResource(underlyingBuilder.newService(dest, label), closeTimeout)

  private def makeServiceResource(
    newService: Service[Request, Response],
    closeTimeout: FiniteDuration
  ): Resource[F, WrappedService[F, Request, Response]] =
    Resource.make {
      Sync[F].delay {
        new WrappedService[F, Request, Response](newService)
      }
    }(service => service.close(closeTimeout))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy