0

The Akka HTTP client API allows passing a Source[ChunkStreamPart, Any] to a HttpEntity.Chunked, which makes it possible to push a stream of ByteStrings into a single HTTP request with backpressure handling:

val data: Source[ByteString, Future[ImportantInformation]]
val chunkedEntity = HttpEntity.Chunked(
    ContentTypes.`application/octet-stream`, 
    data.map(ChunkStreamPart(_)))
val request = HttpRequest(HttpMethods.POST,
    Uri("http://targethost/path"), entity = chunkedEntity)
val downstreamResp : Future[HttpResponse] = Http().singleRequest(request)

Now, the source is consumed far down in the transport layer, and I can't find a way to access the Future[ImportantInformation] materialized value from my Source. Is there a way to work around this problem, i.e. either a method that would let me access the materialized value, or even some kind of Sink in the library that sinks a stream of ByteStrings into a single HTTP request?

us2012
  • 16,083
  • 3
  • 46
  • 62

2 Answers2

2

You can use mapMaterializedValue on your source to access its materialized value.

val data: Source[ByteString, Future[ImportantInformation]]
val mappeddata = 
  data.mapMaterializedValue(future => processImportantInformation(future))
Pahomov Dmitry
  • 111
  • 1
  • 4
  • Thanks! This works for one of our cases. In the other one, we ended up running the source with a `Sink.asPublisher()` and then passing a `Source.fromPublisher()` into the `RequestEntity`, thereby getting access to the materialized value, too. – us2012 Feb 12 '18 at 17:27
1

If you don't need to specify ImportantInformation but just want to know when the Source receives a termination message then you can use Source.watchTermination. This will materialize a Future[Done].

There is a good example found here.

Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
  • Hi Ramon, unfortunately I *do* need to get the `ImportantInformation`. I had a look at the linked example, but it seems that neither `watchTermination` nor `Sink.actorRef` (as it only accepts a static termination message) will give me access to it. Do you have any ideas on how to circumvent this? – us2012 Feb 12 '18 at 08:19