Skip to content

Instantly share code, notes, and snippets.

@aryairani
Last active August 29, 2015 14:12
Show Gist options
  • Save aryairani/d1b6c3bce6470c1584a4 to your computer and use it in GitHub Desktop.
Save aryairani/d1b6c3bce6470c1584a4 to your computer and use it in GitHub Desktop.
Rewritten AyncHttpClient example for Tim
/**
* Created by arya on 1/2/15.
*/
/*
scalaVersion := "2.11.4"
libraryDependencies += "com.ning" % "async-http-client" % "1.9.3"
libraryDependencies += "org.scalaz" %% "scalaz-concurrent" % "7.1.0"
*/
package example
import com.ning.http.client._
import scalaz._
import scalaz.concurrent.Task
trait Version1 {
val asyncHttp: AsyncHttpClient
asyncHttp.prepareGet("http://google.com").execute(new AsyncCompletionHandler[Unit] {
override def onCompleted(r: Response): Unit = ???
override def onThrowable(t: Throwable): Unit = ???
})
}
trait Version2 {
val asyncHttp: AsyncHttpClient
def get(s: String): Task[Response] =
Task.async[Response](k => asyncHttp.prepareGet(s).execute(toHandler(k)))
trait MyResults
def processData: Response => Task[MyResults]
def toHandler(k: (Throwable \/ Response) => Unit) = new AsyncCompletionHandler[Unit] {
override def onCompleted(r: Response): Unit = k(\/-(r))
override def onThrowable(t: Throwable): Unit = k(-\/(t))
}
val myTask: Task[MyResults] = get("http://google.com").flatMap(processData)//...etc
}
@bchazalet
Copy link

That was useful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment