外部のSOAPサービスにアクセスして前日のデータを取得する。
元はSimple Object Access Protocolの頭字語とされていたが、現在は「何かの頭字語ではない」とされている
Scalaは、XMLリテラルを持っている。
2.11から標準ライブラリから分離。
依存関係を追加するには、以下のようにする。
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.0.5"
Scala逆引きレシピにも「第7章 XML」の項目がある。
準備
/* @see http://scalaxb.org/ja/wsdl-support
* scalaxb/httpclients_dispatch_async.scala
* Copyright (c) 2010 e.e d3si9n
* Released under the MIT license
* https://github.com/eed3si9n/scalaxb/blob/release/1.3.0/LICENSE
*/
package scalaxb
import concurrent.Future
trait DispatchHttpClientsAsync extends HttpClientsAsync {
lazy val httpClient = new DispatchHttpClient {}
trait DispatchHttpClient extends HttpClient {
import dispatch._, Defaults._
val http = new Http()
def request(in: String, address: java.net.URI, headers: Map[String, String]): concurrent.Future[String] = {
val req = url(address.toString).setBodyEncoding("UTF-8") <:< headers << in
http(req > as.String)
}
}
}
App never shutdowns when using Dispatch #99
-> sys.exit
でないと終了しない。
setUseProxyProperties does nothing #122
-> プロキシサーバの設定をAsyncHttpClient
に伝えられない。
解決してそうなPRはmergeされる気配は無さそう。
Skinny FrameworkのHTTPクライアントモジュールを採用。
理由
Skinny Framework has a quite simple and handy HTTP client library. Of course, you can use it with non-Skinny apps.
ポイント
trait SkinnyHttpClientsAsync extends scalaxb.HttpClientsAsync {
lazy val httpClient = new SkinnyHttpClient {}
trait SkinnyHttpClient extends HttpClient {
// 外部からExecutionContextを渡すなら必要に応じて変える必要あり
import scala.concurrent.ExecutionContext.Implicits.global
import skinny.http._
def request(in: String, address: java.net.URI, headers: Map[String, String]): concurrent.Future[String] = {
val req = Request(address.toString)
headers.foreach { case (k, v) => req.header(k, v) }
val contentType = "text/xml; charset=utf-8"
req.body(in.getBytes(), contentType)
HTTP.asyncPost(req).map(_.asString)
}
}
}