-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnfoldAsyncTest.scala
37 lines (30 loc) · 1.23 KB
/
UnfoldAsyncTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.l7r7.lab.client
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import scala.concurrent.{ ExecutionContextExecutor, Future }
object UnfoldAsyncTest {
implicit val actorSystem: ActorSystem = ActorSystem()
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val executionContext: ExecutionContextExecutor = actorSystem.dispatcher
def read(initialRequest: HttpRequest): Source[HttpResponse, _] =
Source.unfoldAsync[HttpRequest, HttpResponse](initialRequest)(crawl)
def createRequest() = HttpRequest(uri = "http://localhost:8080/content")
def main(args: Array[String]): Unit = {
val initialRequest: HttpRequest = createRequest()
read(initialRequest)
.take(1000)
.mapAsync(1)(response => Unmarshal(response).to[Multipart.General])
.flatMapConcat(_.parts)
.map(_.entity.contentType)
.zipWithIndex
.runForeach(println)
}
def crawl(request: HttpRequest): Future[Option[(HttpRequest, HttpResponse)]] = {
Http().singleRequest(request)
.map(response => Some(request, response))
}
}