Skip to content

Curl-based Scala Native client implementation for Smithy4s services

Notifications You must be signed in to change notification settings

neandertech/smithy4s-curl

Repository files navigation

smithy4s-curl

An experimental Smithy4s client backend for Scala Native, using Curl directly, without introducing a http4s/cats dependency.

The purpose of the library is to provide a synchronous client implementation for smithy4s services, suitable for usage in CLIs where introducing http4s/cats dependency just to interact with a smithy4s service is undesirable.

The library is currently only available for Scala 3, but we will welcome contributions cross-compiling it to 2.13 – it should be very easy. API surface is very minimal and designed for binary compatible evolution, so after initial round of testing and gathering community feedback, we plan to release 1.0.0 and start checking binary/Tasty compatibility for each release.

Additionally, the library is currently published for Scala Native 0.4, but only for as long as smithy4s core stays on SN 0.4 – once it's published for SN 0.5, this library will jump straight to that.

Installation

Latest version: smithy4s-curl Scala version support

  • SBT: libraryDependencies += "tech.neander" %%% "smithy4s-curl" % "<latest version>"
  • Scala CLI: //> using dep tech.neander::smithy4s-curl::<latest version>
  • Mill: ivy"tech.neander::smithy4s-curl::<latest version>"

Getting started

For example's sake, let's say we have a smithy4s service that models one of the endpoints from https://httpbin.org, defined using smithy4s-deriving (note we're using Scala CLI for this demo):

//> using dep "tech.neander::smithy4s-deriving::0.0.3"
//> using platform scala-native
//> using scala 3.4.2
//> using option -Wunused:all

import scala.annotation.experimental
import smithy4s.*, deriving.{given, *}, aliases.*
import scala.util.Try

case class Response(headers: Map[String, String], origin: String, url: String)
    derives Schema

@experimental
trait HttpbinService derives API:
  @readonly
  @httpGet("/get")
  def get(): Try[Response]

Note that we only need to use @experimental annotation because we are using smithy4s-deriving. If you're creating clients for services generated by standard Smithy4s codegen, just remove all @experimental annotations you see.

To create a Curl client for this service all we need to do is this:

import smithy4s_curl.*

@main @experimental
def helloWorld = 
  val service = 
    SimpleRestJsonCurlClient(
      API.service[HttpbinService],
      "https://httpbin.org",
      SyncCurlClient()
     ).make.unliftService

  println(service.get())

Note that you need to have Curl library enabled, and a -lcurl flag added to the Scala Native linking flags

Contributing

If you see something that can be improved in this library – please contribute!

This is a relatively standard Scala CLI project, even though the tests run a Scala version newer than the library itself (library is published for 3.3, tests are in 3.4, to make use of smithy4s-deriving).

Here are some useful commands:

  • make test – run tests
  • make check-docs – verify that snippets in README.md (this file) compile
  • make pre-ci – format the code so that it passes CI check
  • make run-example – run example from README against real https://httpbin.org