Created
June 7, 2013 13:48
-
-
Save nmccready/5729393 to your computer and use it in GitHub Desktop.
Convert Ning AsyncHandler to Scala Future with AsyncHttpClient
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
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