Akka http client retry with restart


I found in the official doc, that there is an option to retry the http single get request, via using the following api:

import akka.http.scaladsl.unmarshalling.Unmarshal

object Main extends App {
import system.dispatcher

  implicit val system = ActorSystem("app")
  implicit val mat = ActorMaterializer()
val restartSource = RestartSource.withBackoff(
  minBackoff = 3.seconds,
  maxBackoff = 30.seconds,
  randomFactor = 0.2 // adds 20% "noise" to vary the intervals slightly
) { () =>
  // Create a source from a future of a source
  Source.fromFutureSource {
    // Make a single request with akka-http
      uri = "http://example.com/eventstream"))
      // Unmarshall it as a source of server sent events
      .flatMap(Unmarshal(_).to[Source[ServerSentEvent, NotUsed]])

when I’m trying to run it as is in my intellij I get the following error:
Error:(49, 33) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.HttpResponse,akka.stream.scaladsl.Source[akka.http.scaladsl.model.sse.ServerSentEvent,akka.NotUsed]]
.flatMap(Unmarshal(_).to[Source[ServerSentEvent, NotUsed]])
Any idea?

It says you need an implicit umarsheller for source type, basically an implementation of Unmarshaller that knows how to work with your type.
Have a look at this example, seems close to what you may want.

Which example did you use as a starting point?

I see some similar code at https://doc.akka.io/docs/akka-http/current/sse-support.html#client-side-usage-unmarshalling. There the code snippet also imports the unmarshallers for ServerSentEvent, which you might be missing.

Could you try with import akka.http.scaladsl.unmarshalling.sse.EventStreamUnmarshalling._?

yes now it’s compiling…thanks!