Skip to content

Instantly share code, notes, and snippets.

@rkrzewski
Last active August 18, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkrzewski/46641359761eaa605fd62191b96b4416 to your computer and use it in GitHub Desktop.
Save rkrzewski/46641359761eaa605fd62191b96b4416 to your computer and use it in GitHub Desktop.
package example
import akka.actor.{ Actor, ActorLogging }
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers._
import akka.stream.{ ActorMaterializer, ActorMaterializerSettings }
import akka.util.ByteString
class Cookies extends Actor with ActorLogging {
import context.dispatcher
import akka.pattern.pipe
final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
val http = Http(context.system)
override def preStart() = {
http.singleRequest(HttpRequest(uri = "http://google.com"))
.pipeTo(self)
}
def receive = {
case resp @ HttpResponse(StatusCodes.Found, _, _, _) =>
resp.header[`Location`].foreach { location =>
http.singleRequest(HttpRequest(uri = location.uri))
.pipeTo(self)
}
case resp: HttpResponse =>
val cookies: Seq[HttpCookie] = resp.headers.collect {
case c: `Set-Cookie` =>
c.cookie
}
log.info("Got cookies: {}", cookies.mkString("\n"))
http.shutdownAllConnectionPools().andThen {
case _ =>
context.system.terminate()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment