Skip to content

Instantly share code, notes, and snippets.

@nmccready
Created June 7, 2013 13:48
Show Gist options
  • Save nmccready/5729393 to your computer and use it in GitHub Desktop.
Save nmccready/5729393 to your computer and use it in GitHub Desktop.
Convert Ning AsyncHandler to Scala Future with AsyncHttpClient
trait IHttpClient[T] {
def host: String
def port: Int
def get(uri: String): Future[T]
}
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment