Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Created July 26, 2024 13:43
Show Gist options
  • Save kubukoz/c635c3db0770bb2e7b07040deb4fb3fc to your computer and use it in GitHub Desktop.
Save kubukoz/c635c3db0770bb2e7b07040deb4fb3fc to your computer and use it in GitHub Desktop.
Sagemaker invocation mock with http4s and records4s
//> using dep org.http4s::http4s-ember-server:0.23.27
//> using dep org.http4s::http4s-dsl:0.23.27
//> using dep org.http4s::http4s-circe:0.23.27
//> using dep com.github.tarao::record4s-circe:0.13.0
//> using option -Wunused:all
import cats.effect.IO
import cats.effect.IOApp
import com.comcast.ip4s.*
import com.github.tarao.record4s.%
import com.github.tarao.record4s.select
import com.github.tarao.record4s.circe.Codec.given
import org.http4s.HttpRoutes
import org.http4s.circe.CirceEntityCodec.*
import org.http4s.dsl.io.*
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.implicits.*
import scala.concurrent.duration.Duration
import scala.util.Random
object SagemakerMock extends IOApp.Simple {
val app = HttpRoutes
.of[IO] {
case req @ POST -> Root / "endpoints" / endpointName / "invocations" =>
req.as[% { val inputs: String }].flatMap { case select.inputs(input) =>
IO.println("input was: " + input) *>
Ok(
List.fill(768)(Random.nextFloat())
)
}
}
.orNotFound
def run: IO[Unit] = EmberServerBuilder
.default[IO]
.withHost(host"0.0.0.0")
.withPort(port"5050")
.withHttpApp(
app
)
.withShutdownTimeout(Duration.Zero)
.build
.useForever
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment