0

I'm considering using stac4s as a STAC client in our Scala project but boy, is it giving me a hard time. The use of cats, monads and all that fancy stuff is very daunting and there's no documentation to speak of.

I'm stuck right at the start: instantiating the thing.

import com.azavea.stac4s.api.client.SttpStacClient
import org.junit.Test
import sttp.client3._

class Stac4sTest {

  @Test
  def testDrive(): Unit = {
    val stacClient = SttpStacClient(client = HttpURLConnectionBackend(), baseUri = uri"") // <-- error here
  }
}

This gives me this compile error:

error: could not find implicit value for evidence parameter of type cats.MonadThrow[sttp.client3.Identity]

I'm assuming it's something easy like a missing import but I haven't been able to find it.

This is stac4s version 0.8.1.

Jan Van den bosch
  • 3,542
  • 3
  • 26
  • 38

2 Answers2

1

I haven't ever used the stac4s library, but as far as instantiating an sttp+cats-effect based client, you typically end up with an IO value, which describes the whole program, with all of its side effects (here, allocating the client etc.). So you need to evaluate it after the description is created. For example, following sttp's docs:

val program = HttpClientCatsBackend.resource[IO]().use { backend =>
  val stacClient = SttpStacClient(client = backend, baseUri = uri"")
  // other operations
}

import cats.effect.unsafe.implicits.global
program.unsafeRunSync()

The unsafeRunSync executes the program, as it is described, using the given thread pool for any forks.

Note that you weren't able to use HttpURLConnectionBackend(), as it is a synchronous client, while stac seems to require a backend which uses cats-effect.

Daenyth
  • 35,856
  • 13
  • 85
  • 124
adamw
  • 8,038
  • 4
  • 28
  • 32
  • This is the right approach, though it's far better to use `Dispatcher` to do the `unsafeRunSync` step instead of the `IORuntime`. That's the intended use for `Dispatcher` - implement imperative synchronous code using an `IO` value – Daenyth May 25 '23 at 16:04
-1

Not answering this specific case but some guidance ...

When dealing with such kind of errors, I often recommend to look at the tests of the library and you'll somehow find out how to instantiate things or what import is missing.

For instance, you can start by looking at https://github.com/azavea/stac4s/blob/master/modules/client/jvm/src/test/scala/com/azavea/stac4s/api/client/SttpStacClientSpec.scala which will guide you to other classes and ultimately find what's missing.

Your IDE can help as well to suggest what's missing.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • That's exactly what I tried but in this case, both the backend and the implicit are provided by the tests themselves so I still don't know what implicit I should provide in production code. – Jan Van den bosch May 17 '23 at 12:43
  • This isn't an answer, it should be a comment – Daenyth May 25 '23 at 16:02