Created
June 7, 2013 15:37
-
-
Save nmccready/5730187 to your computer and use it in GitHub Desktop.
HttpClient interface with multiple implementations Akka to use a TestActorRef, Spray and Ning
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
import akka.actor._ | |
import scala.concurrent.duration._ | |
import scala.concurrent.Future | |
import spray.http._ | |
import spray.can.client._ | |
trait SprayHttpClient extends IHttpClient[HttpResponse] { | |
val httpClient = DefaultHttpClient(system) | |
lazy val headers: List[spray.http.HttpHeader] = Nil | |
def system: ActorSystem | |
implicit lazy val factory = system | |
def get(uri: String) = | |
HttpDialog(httpClient, host, port).send { | |
HttpRequest(method = HttpMethods.GET, uri = uri, headers = headers) | |
}.end | |
def close() = httpClient.stop() | |
} | |
object AkkaHttpMessages { | |
case class Get(host: String, port: Int, uri: String, headers: List[spray.http.HttpHeader] = Nil) | |
case class Close() | |
} | |
import akka.pattern.ask | |
import akka.util.Timeout | |
import AkkaHttpMessages._ | |
import akka.testkit.TestActorRef | |
trait AkkaHttpClient extends IHttpClient[HttpResponse] { | |
implicit val timeout = Timeout(10 second) | |
def clientActorRef: ActorRef | |
def get(uri: String): Future[HttpResponse] = | |
(clientActorRef ? HttpRequest(method = HttpMethods.GET, uri = uri)).mapTo[HttpResponse] | |
def close() = clientActorRef ! Close() | |
} | |
trait TestMessagesHttpClient extends AkkaHttpClient { | |
override val host = "not applicable" | |
override val port = 0 | |
implicit val system: ActorSystem | |
lazy val clientActorRef = TestActorRef(new TestHTTPStatusEndpointsHandlerActor()) | |
} | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
object Now { | |
def apply[T](fut: Future[T]): T = fut.value.get.get | |
} | |
trait Wait { | |
def awaitTimeSeconds: Duration | |
def apply[T](fut: Future[T]): T = Await.result[T](fut, awaitTimeSeconds) | |
def wait[T](fut: Future[T]): T = apply(fut) | |
} | |
object Wait extends Wait { | |
val awaitTimeSeconds: Duration = 20 seconds | |
} | |
trait IHttpClient[T] { | |
def host: String | |
def port: Int | |
def get(uri: String): Future[T] | |
def close(): Unit | |
} | |
import com.ning.http.client.{AsyncHttpClient, AsyncCompletionHandler, Response} | |
trait NingHttpClient extends IHttpClient[Response] { | |
val client = new AsyncHttpClient() | |
def get(uri: String): Future[Response] = { | |
val promise = Promise[Response]() | |
client.prepareGet(s"http://$host:$port$uri").execute(new AsyncCompletionHandler[Response] { | |
def onCompleted(response: Response) = { | |
promise.success(response) | |
response | |
} | |
override def onThrowable(t: Throwable) { | |
promise.failure(t) | |
super.onThrowable(t) | |
} | |
}) | |
promise.future | |
} | |
def close() = client.close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment