Created
July 8, 2015 02:58
-
-
Save searler/d7b8580ec864b126558a to your computer and use it in GitHub Desktop.
Akka Future based HTTP client implementation of Scalaxb HttpClientsAsync
This file contains hidden or 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 scala.concurrent.Await | |
import scala.concurrent.Future | |
import scala.concurrent.duration.Duration | |
import scala.util.Try | |
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.model.ContentType | |
import akka.http.scaladsl.model.HttpCharsets | |
import akka.http.scaladsl.model.HttpEntity.Strict | |
import akka.http.scaladsl.model.HttpMethods | |
import akka.http.scaladsl.model.HttpRequest | |
import akka.http.scaladsl.model.HttpResponse | |
import akka.http.scaladsl.model.MediaTypes | |
import akka.http.scaladsl.model.Uri.apply | |
import akka.stream.ActorMaterializer | |
import akka.util.ByteString | |
object Simpler extends App { | |
import scala.concurrent.{ Future, Await } | |
import akka.stream.ActorMaterializer | |
import akka.http.scaladsl.model._ | |
import akka.http.scaladsl.Http | |
import scala.concurrent.duration._ | |
implicit val system = ActorSystem() | |
implicit val materializer = ActorMaterializer() | |
import system.dispatcher | |
trait FutureHttp extends scalaxb.HttpClientsAsync { | |
def httpClient: HttpClient = new HttpClient { | |
def request(in: String, address: java.net.URI, headers: Map[String, String]): concurrent.Future[String] = { | |
val action = ("""".*"""".r).findFirstIn(headers("Content-Type")).get | |
val ct = ContentType(MediaTypes.`application/soap+xml`.withParams(Map("action" -> action)), HttpCharsets.`UTF-8`) | |
val responseFuture: Future[HttpResponse] = | |
Http().singleRequest(HttpRequest(method = HttpMethods.POST, uri = address.toString, entity = new Strict(ct, ByteString(in)))) | |
return responseFuture.flatMap(_.entity.dataBytes.runFold(ByteString.empty)(_ ++ _).map(_.utf8String)) | |
} | |
} | |
} | |
val service = (new generated.StockQuoteSoap12Bindings with scalaxb.SoapClientsAsync with FutureHttp {}).service; | |
val fresponse = service.getQuote(Some("GOOG")) | |
val response = Try(Await.result(fresponse, Duration(5, "seconds"))) | |
response.recover { case _@ x => x.toString }.foreach(println) | |
Http().shutdownAllConnectionPools().onComplete(_ => system.shutdown()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment