Skip to content

Instantly share code, notes, and snippets.

@zetashift
Last active September 30, 2022 19:12
Show Gist options
  • Save zetashift/0463e98fe8e678e18d83fb3dae27bfae to your computer and use it in GitHub Desktop.
Save zetashift/0463e98fe8e678e18d83fb3dae27bfae to your computer and use it in GitHub Desktop.
HackerNews GET with scala-cli and CrossPlatformIOApp
//> using scala "3.1"
//> using lib "org.http4s::http4s-dsl:0.23.12"
//> using lib "org.http4s::http4s-ember-client:0.23.16"
//> using lib "org.http4s::http4s-circe:0.23.16"
//> using lib "io.circe::circe-generic:0.14.3"
//> using lib "io.circe::circe-literal:0.14.3"
//> using lib "io.chrisdavenport::crossplatformioapp:0.1.0"
//> using platform "scala-native"
import io.chrisdavenport.crossplatformioapp.CrossPlatformIOApp
import cats.effect.{ExitCode, IO}
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.*
import org.http4s.client.*
import io.circe.literal.*
import io.circe.*
import org.http4s.circe.CirceEntityCodec._
import org.http4s.syntax.literals.uri
object HNApp extends CrossPlatformIOApp:
val hnRequest =
Request[IO](
Method.GET,
uri"https://hn.algolia.com/api/v1/items/1"
)
def run(args: List[String]): IO[ExitCode] =
EmberClientBuilder
.default[IO]
.build
.use { client =>
client.expect[Json](hnRequest).flatMap(json => IO.print(json))
}
.as(ExitCode.Success)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment