Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pain-free APIs with Smithy4s

Pain-free APIs with Smithy4s

Most back-end projects eventually reach a point when they have to define an API. We implement their routes, document them, sometimes we write a client. We do essentially the same work multiple times - each view of an endpoint contains the same path, and yet we don't have a "single source of truth".

In this talk, I'll show how Smithy (https://smithy.io) and smithy4s enable defining an API in a single place, and how they make it easy to build its server, client, and even a CLI - in a Scala-friendly, tooling-compatible way.

Jakub Kozłowski

March 23, 2023
Tweet

More Decks by Jakub Kozłowski

Other Decks in Technology

Transcript

  1. Pain-free APIs Pain-free APIs with Smithy4s with Smithy4s Jakub Kozłowski

    | Scalar 2023 | Jakub Kozłowski | Scalar 2023 | linktr.ee/kubukoz linktr.ee/kubukoz
  2. Interface Interface definition definition language language (IDL) (IDL) operation GetWeather

    { input := { @required city: String } output := { @required weather: Weather } } structure Weather { @required degrees: Integer details: String }
  3. // not like this (http4s) HttpRoutes.of[IO] { case GET ->

    Root / "weather" / city :? params => ??? } // more like this (tapir) endpoint.in( "weather" / path[String]("city") / query[String]("temp") )
  4. HTTP example HTTP example namespace hello use alloy#simpleRestJson @simpleRestJson service

    WeatherService { operations: [GetWeather] } @http( method: "GET", uri: "/weather/{city}" ) @readonly operation GetWeather { ... }
  5. What do we get? What do we get? // give

    me business logic, I'll give you routes def server(impl: WeatherService[IO]): HttpRoutes[IO] // give me a HTTP client, I'll give you a high-level interface def client(c: Client[IO]): WeatherService[IO]
  6. Server (routes) Server (routes) val impl: WeatherService[IO] = ... def

    run: IO[Unit] = SimpleRestJsonBuilder .routes(impl) .resource .flatMap { routes => // normal http4s stuff from now on EmberServerBuilder .default[IO] .withHttpApp(routes.orNotFound) .build } .useForever
  7. Links Links Slides: see – linktr.ee/kubukoz Contact/YouTube/blog: see above ;)

    – – smithy.io – disneystreaming.github.io/smithy4s – Scaling APIs with Smithy – Revisiting visitors: a talk about generalising serialisation – smithy4s fullstack (blogpost series)