2015-05-11 23:05:18 +02:00
|
|
|
/*
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-05-11 23:05:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.http.scaladsl
|
|
|
|
|
|
2015-09-30 12:07:03 +02:00
|
|
|
import akka.actor.{ ActorLogging, ActorSystem }
|
2016-01-13 13:32:42 +01:00
|
|
|
import akka.stream.{ ActorMaterializerSettings }
|
2015-09-30 12:07:03 +02:00
|
|
|
import akka.util.ByteString
|
2015-05-11 23:05:18 +02:00
|
|
|
import org.scalatest.{ Matchers, WordSpec }
|
|
|
|
|
|
|
|
|
|
class HttpClientExampleSpec extends WordSpec with Matchers {
|
|
|
|
|
|
|
|
|
|
"outgoing-connection-example" in {
|
|
|
|
|
pending // compile-time only test
|
|
|
|
|
//#outgoing-connection-example
|
2015-09-30 12:07:03 +02:00
|
|
|
import akka.http.scaladsl.Http
|
|
|
|
|
import akka.http.scaladsl.model._
|
2015-06-23 18:28:53 +02:00
|
|
|
import akka.stream.ActorMaterializer
|
2015-05-11 23:05:18 +02:00
|
|
|
import akka.stream.scaladsl._
|
2015-09-30 12:07:03 +02:00
|
|
|
|
|
|
|
|
import scala.concurrent.Future
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
implicit val system = ActorSystem()
|
2015-06-23 18:28:53 +02:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
val connectionFlow: Flow[HttpRequest, HttpResponse, Future[Http.OutgoingConnection]] =
|
2015-06-18 16:30:03 +02:00
|
|
|
Http().outgoingConnection("akka.io")
|
2015-05-11 23:05:18 +02:00
|
|
|
val responseFuture: Future[HttpResponse] =
|
|
|
|
|
Source.single(HttpRequest(uri = "/"))
|
|
|
|
|
.via(connectionFlow)
|
|
|
|
|
.runWith(Sink.head)
|
|
|
|
|
//#outgoing-connection-example
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"host-level-example" in {
|
|
|
|
|
pending // compile-time only test
|
|
|
|
|
//#host-level-example
|
2015-09-30 12:07:03 +02:00
|
|
|
import akka.http.scaladsl.Http
|
|
|
|
|
import akka.http.scaladsl.model._
|
2015-06-23 18:28:53 +02:00
|
|
|
import akka.stream.ActorMaterializer
|
2015-05-11 23:05:18 +02:00
|
|
|
import akka.stream.scaladsl._
|
2015-09-30 12:07:03 +02:00
|
|
|
|
|
|
|
|
import scala.concurrent.Future
|
|
|
|
|
import scala.util.Try
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
implicit val system = ActorSystem()
|
2015-06-23 18:28:53 +02:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-05-11 23:05:18 +02:00
|
|
|
// construct a pool client flow with context type `Int`
|
2015-06-18 16:30:03 +02:00
|
|
|
val poolClientFlow = Http().cachedHostConnectionPool[Int]("akka.io")
|
2015-05-11 23:05:18 +02:00
|
|
|
val responseFuture: Future[(Try[HttpResponse], Int)] =
|
|
|
|
|
Source.single(HttpRequest(uri = "/") -> 42)
|
|
|
|
|
.via(poolClientFlow)
|
|
|
|
|
.runWith(Sink.head)
|
|
|
|
|
//#host-level-example
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"single-request-example" in {
|
|
|
|
|
pending // compile-time only test
|
|
|
|
|
//#single-request-example
|
|
|
|
|
import akka.http.scaladsl.Http
|
2015-09-30 12:07:03 +02:00
|
|
|
import akka.http.scaladsl.model._
|
|
|
|
|
import akka.stream.ActorMaterializer
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.Future
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
implicit val system = ActorSystem()
|
2015-06-23 18:28:53 +02:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
val responseFuture: Future[HttpResponse] =
|
|
|
|
|
Http().singleRequest(HttpRequest(uri = "http://akka.io"))
|
|
|
|
|
//#single-request-example
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-30 12:07:03 +02:00
|
|
|
"single-request-in-actor-example" in {
|
|
|
|
|
pending // compile-time only test
|
|
|
|
|
//#single-request-in-actor-example
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.http.scaladsl.Http
|
|
|
|
|
import akka.http.scaladsl.model._
|
2016-01-13 13:32:42 +01:00
|
|
|
import akka.stream.ActorMaterializer
|
|
|
|
|
import akka.stream.ActorMaterializerSettings
|
2015-09-30 12:07:03 +02:00
|
|
|
|
|
|
|
|
class Myself extends Actor
|
|
|
|
|
with ActorLogging {
|
|
|
|
|
|
|
|
|
|
import akka.pattern.pipe
|
|
|
|
|
import context.dispatcher
|
|
|
|
|
|
2016-01-13 13:32:42 +01:00
|
|
|
final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
|
|
|
|
|
|
2015-09-30 12:07:03 +02:00
|
|
|
val http = Http(context.system)
|
|
|
|
|
|
|
|
|
|
override def preStart() = {
|
|
|
|
|
http.singleRequest(HttpRequest(uri = "http://akka.io"))
|
|
|
|
|
.pipeTo(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
|
|
|
|
|
log.info("Got response, body: " + entity.dataBytes.runFold(ByteString(""))(_ ++ _))
|
|
|
|
|
case HttpResponse(code, _, _, _) =>
|
|
|
|
|
log.info("Request failed, response code: " + code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#single-request-in-actor-example
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 23:05:18 +02:00
|
|
|
}
|