Created
January 22, 2015 14:08
-
-
Save takeouchida/2b6af61baa25e059a8d6 to your computer and use it in GitHub Desktop.
spray-client proxy example based on a code in spray repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package clientwithproxy | |
import akka.util.Timeout | |
import spray.can.Http.ClientConnectionType | |
import scala.util.{Success, Failure} | |
import scala.concurrent.duration._ | |
import akka.actor.ActorSystem | |
import akka.pattern.ask | |
import akka.event.Logging | |
import akka.io.IO | |
import spray.json.{JsonFormat, DefaultJsonProtocol} | |
import spray.can.Http | |
import spray.httpx.SprayJsonSupport | |
import spray.client.pipelining._ | |
import spray.util._ | |
case class Elevation(location: Location, elevation: Double) | |
case class Location(lat: Double, lng: Double) | |
case class GoogleApiResult[T](status: String, results: List[T]) | |
object ElevationJsonProtocol extends DefaultJsonProtocol { | |
implicit val locationFormat = jsonFormat2(Location) | |
implicit val elevationFormat = jsonFormat2(Elevation) | |
implicit def googleApiResultFormat[T :JsonFormat] = jsonFormat2(GoogleApiResult.apply[T]) | |
} | |
object Main extends App { | |
// we need an ActorSystem to host our application in | |
implicit val system = ActorSystem("simple-spray-client") | |
import system.dispatcher // execution context for futures below | |
val log = Logging(system, getClass) | |
log.info("Requesting the elevation of Mt. Everest from Googles Elevation API...") | |
import ElevationJsonProtocol._ | |
import SprayJsonSupport._ | |
val proxyHost = "192.168.1.7" | |
val proxyPort = 3128 | |
val proxySetup = Http.HostConnectorSetup(proxyHost, port = proxyPort, | |
connectionType = ClientConnectionType.Proxied(proxyHost, proxyPort)) | |
implicit val timeout = Timeout(5 seconds) | |
val pipeline = | |
for (Http.HostConnectorInfo(connector, _) <- IO(Http) ? proxySetup) | |
yield sendReceive(connector) ~> unmarshal[GoogleApiResult[Elevation]] | |
val responseFuture = pipeline.flatMap { f => | |
f(Get("http://maps.googleapis.com/maps/api/elevation/json?locations=27.988056,86.925278&sensor=false")) | |
} | |
responseFuture onComplete { | |
case Success(GoogleApiResult(_, Elevation(_, elevation) :: _)) => | |
log.info("The elevation of Mt. Everest is: {} m", elevation) | |
shutdown() | |
case Success(somethingUnexpected) => | |
log.warning("The Google API call was successful but returned something unexpected: '{}'.", somethingUnexpected) | |
shutdown() | |
case Failure(error) => | |
log.error(error, "Couldn't get elevation") | |
shutdown() | |
} | |
def shutdown(): Unit = { | |
IO(Http).ask(Http.CloseAll)(1.second).await | |
system.shutdown() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment