Skip to content

Instantly share code, notes, and snippets.

@bchazalet
Forked from aryairani/AsyncHttpTask.scala
Last active August 29, 2015 14:20
Show Gist options
  • Save bchazalet/d07a836cdd5d79ffbdcb to your computer and use it in GitHub Desktop.
Save bchazalet/d07a836cdd5d79ffbdcb to your computer and use it in GitHub Desktop.
/**
* 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment