Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed fs2 jawn #339

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ lazy val http4s = module("http4s") {
.settings(
description := "http4s based client for kubernetes",
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-client" % "0.23.27",
"org.typelevel" %%% "jawn-fs2" % "2.4.0"
"org.http4s" %%% "http4s-client" % "0.23.27"
)
)
.dependsOn(client, jawn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ private[http4s] abstract class PlatformCompanion[F[_]: Async: Files: Env]
"Cannot find where/how to connect using the provided config!"
).raiseError
)
case Some((cluster, server, auth)) =>
case Some((clusterData, server, auth)) =>
ssl(
caFile = cluster.`certificate-authority`,
caData = cluster.`certificate-authority-data`,
caFile = clusterData.`certificate-authority`,
caData = clusterData.`certificate-authority-data`,
clientCert = auth.`client-certificate-data`,
clientCertFile = auth.`client-certificate`,
clientKey = auth.`client-key-data`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ private[http4s] abstract class PlatformCompanion[F[_]: Async: Files: Env]
"Cannot find where/how to connect using the provided config!"
).raiseError
)
case Some((cluster, server, auth)) =>
case Some((clusterData, server, auth)) =>
client(
caFile = cluster.`certificate-authority`,
caData = cluster.`certificate-authority-data`,
caFile = clusterData.`certificate-authority`,
caData = clusterData.`certificate-authority-data`,
clientCert = auth.`client-certificate-data`,
clientCertFile = auth.`client-certificate`,
clientKey = auth.`client-key-data`,
Expand Down
4 changes: 2 additions & 2 deletions modules/http4s/.jvm/src/main/scala/JVMPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ private[http4s] abstract class JVMPlatform[F[_]](implicit
"Cannot find where/how to connect using the provided config!"
).raiseError
)
case Some((cluster, server, auth)) =>
val sslContext = F.blocking(SSLContexts.from(cluster, auth))
case Some((clusterData, server, auth)) =>
val sslContext = F.blocking(SSLContexts.from(clusterData, auth))

Resource
.eval(sslContext)
Expand Down
29 changes: 24 additions & 5 deletions modules/http4s/src/main/scala/Http4sBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ final class Http4sBackend[F[_], T] private (client: Client[F])(implicit
)
)

private def parseJsonStream: fs2.Pipe[F, Byte, T] = {
import dev.hnaderi.k8s.jawn
import org.typelevel.jawn._
import fs2.{Pull, Chunk}

implicit val jawnFacade: Facade.SimpleFacade[T] = jawn.jawnFacade[T]
def go(
parser: AsyncParser[T]
)(s: Stream[F, Chunk[Byte]]): Pull[F, T, Unit] = {
def handle(attempt: Either[ParseException, collection.Seq[T]]) =
attempt.fold(Pull.raiseError[F], js => Pull.output(Chunk.from(js)))

s.pull.uncons1.flatMap {
case Some((a, stream)) =>
handle(parser.absorb(a.toByteBuffer)) >> go(parser)(stream)
case None =>
handle(parser.finish()) >> Pull.done
}
}

src => go(AsyncParser[T](AsyncParser.ValueStream))(src.chunks).stream
}

override def connect[O: Decoder](
url: String,
verb: APIVerb,
Expand All @@ -115,15 +138,11 @@ final class Http4sBackend[F[_], T] private (client: Client[F])(implicit
cookies: Seq[(String, String)]
): Stream[F, O] = {
import Stream._
import org.typelevel.jawn.fs2._
import dev.hnaderi.k8s.jawn
import org.typelevel.jawn.Facade
implicit val jawnFacade: Facade.SimpleFacade[T] = jawn.jawnFacade[T]

eval(urlFrom(url, params))
.map(methodFor(verb)(_, Headers(headers) ++ cookiesFor(cookies)))
.flatMap(client.stream(_))
.flatMap(_.body.chunks.parseJsonStream[T])
.flatMap(_.body.through(parseJsonStream))
.flatMap { s =>
s.decodeTo[O]
.fold(err => raiseError[F](new Exception(s"$err\n$s")), emit(_))
Expand Down
8 changes: 4 additions & 4 deletions modules/sttp/.jvm/src/main/scala/SttpJVM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ private[client] trait SttpJVM[F[_]] {
throw new IllegalArgumentException(
"Cannot find where/how to connect using the provided config!"
)
case Some((cluster, server, auth)) =>
val ssl = SSLContexts.from(cluster, auth)
case Some((clusterData, server, auth)) =>
val ssl = SSLContexts.from(clusterData, auth)
HttpClient[SttpF[F, *]](
server,
SttpKBackend[F, T](buildWithSSLContext(ssl)),
Expand Down Expand Up @@ -124,8 +124,8 @@ private[client] trait SttpJVM[F[_]] {
val str = readFile(config)
manifest.parse[Config](str) match {
case Left(error) => throw error
case Right(config) =>
fromConfig(config, context = context, cluster = cluster)
case Right(configData) =>
fromConfig(configData, context = context, cluster = cluster)
}
}

Expand Down
Loading