Skip to content

Commit

Permalink
Http4sKubernetesClient constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
hnaderi committed Apr 29, 2023
1 parent 337993e commit 6d81598
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
6 changes: 1 addition & 5 deletions example/src/main/scala/Http4sExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ import io.circe.Json
import io.k8s.api.core.v1.ConfigMap
import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta
import org.http4s.circe._
import org.http4s.ember.client.EmberClientBuilder

//NOTE run `kubectl proxy` before running this example
object Http4sExample extends IOApp {

private val client =
EmberClientBuilder
.default[IO]
.build
.map(Http4sKubernetesClient[IO, Json]("http://localhost:8001", _))
Http4sKubernetesClient.fromUrl[IO, Json]("http://localhost:8001")

def watchNodes(cl: StreamingClient[fs2.Stream[IO, *]]) =
CoreV1.nodes
Expand Down
32 changes: 31 additions & 1 deletion modules/http4s/src/main/scala/Http4sKubernetesClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package dev.hnaderi.k8s.client

import cats.effect.Concurrent
import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import cats.implicits._
import dev.hnaderi.k8s.jawn
import dev.hnaderi.k8s.utils._
Expand All @@ -25,12 +27,13 @@ import org.http4s.Method._
import org.http4s._
import org.http4s.client.Client
import org.http4s.client.dsl.Http4sClientDsl
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.headers.`Content-Type`
import org.http4s.syntax.literals._
import org.typelevel.jawn.Facade
import org.typelevel.jawn.fs2._

final case class Http4sKubernetesClient[F[_], T](
final case class Http4sKubernetesClient[F[_], T] private (
baseUrl: String,
client: Client[F]
)(implicit
Expand Down Expand Up @@ -140,3 +143,30 @@ final case class Http4sKubernetesClient[F[_], T](
}
}
}

object Http4sKubernetesClient {
type KClient[F[_]] = HttpClient[F] with StreamingClient[Stream[F, *]]

def fromClient[F[_], T](
baseUrl: String,
client: Client[F]
)(implicit
F: Concurrent[F],
enc: EntityEncoder[F, T],
dec: EntityDecoder[F, T],
builder: Builder[T],
reader: Reader[T]
): KClient[F] =
Http4sKubernetesClient(baseUrl, client)

def fromUrl[F[_], T](
baseUrl: String
)(implicit
F: Async[F],
enc: EntityEncoder[F, T],
dec: EntityDecoder[F, T],
builder: Builder[T],
reader: Reader[T]
): Resource[F, KClient[F]] =
EmberClientBuilder.default[F].build.map(fromClient(baseUrl, _))
}

0 comments on commit 6d81598

Please sign in to comment.