Last active
August 31, 2015 01:51
-
-
Save jiang7462582/6d42f8393e6c9676aee7 to your computer and use it in GitHub Desktop.
Spray-client Rest
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
import spray.json.DefaultJsonProtocol | |
import spray.client.pipelining._ | |
import spray.http._ | |
import scala.concurrent._ | |
import akka.actor.{ActorSystem} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.util._ | |
case class User(id: Option[Int], username: Option[String]) | |
object Tasks extends DefaultJsonProtocol{ | |
implicit val orderConfirmationFormat = jsonFormat2(User) | |
implicit val system = ActorSystem() | |
def getRequest(): Unit = { | |
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive | |
val response: Future[HttpResponse] = pipeline(Get("http://www.baidu.com")) | |
response.onComplete { | |
case Failure(t) => | |
println(s"error $t") | |
case Success(r) => | |
println(s"success ${r.status}") | |
println(r.entity.asString) | |
} | |
} | |
def postRequest()={ | |
import spray.httpx.SprayJsonSupport._ | |
val pipeline: HttpRequest => Future[User] = sendReceive ~> unmarshal[User] | |
val response: Future[User] = pipeline { Post("http://localhost:8088/users", User(Option(100),"jiang"))} | |
response.onComplete{ | |
case Failure(t) => | |
println(s"error $t") | |
case Success(r) => | |
println(s"success ${r}") | |
println(r) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment