Last active
August 29, 2015 14:12
-
-
Save aryairani/d1b6c3bce6470c1584a4 to your computer and use it in GitHub Desktop.
Rewritten AyncHttpClient example for Tim
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
/** | |
* 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
That was useful, thanks!