by shigemk2

当面は技術的なことしか書かない

Skinnyのhttpクライアント

HTTP Client - Skinny Framework

GET PUT DELETE POSTすべての命令に対応したクライアント。

wgetとかcurl的なことがskinny framework上で出来る。なお、case class を直接定義する必要なくって、headerとかasBytesとかはナチュラルに使える。

import skinny.http._

// GET
val response: Response = HTTP.get("http://example.com/api?foo=bar")

case class Response(
  status: Int,
  headers: Map[String, String],
  headerFields: Map[String, Seq[String]],
  rawCookies: Map[String, String],
  charset: Option[String],
  body: Array[Byte]) {

  def header(name: String): Option[String] 
  def headerField(name: String): Seq[String]
  def asBytes: Array[Byte]
  def textBody: String
  def asString: String 
}

// GET with QueryParams

val response = HTTP.get("http://example.com/", "foo" -> "bar")
val response = HTTP.get(Request("http://example.com/").queryParams("foo" -> "bar"))
val response = HTTP.get(Request("http://example.com/").queryParam("foo" -> "bar").queryParam("bar" -> "baz"))

// GET with charset
val response = HTTP.get("http://example.com/?foo=bar", "UTF-8")

// Async GET

val response: scala.concurrent.Future[Response] = HTTP.asyncGet("http://example.com/?foo=bar")
val response = HTTP.asyncGet("http://example.com/", "foo" -> "bar")
val response = HTTP.asyncGet(Request("http://example.com/").queryParams("foo" -> "bar"))
val response = HTTP.asyncGet("http://example.com/?foo=bar", "UTF-8")

// POST

val response = HTTP.post("http://example.com/", "foo=bar")
val response = HTTP.post("http://example.com/", "foo" -> "bar")
val response = HTTP.postMultipart("http://example.com/", FormData("toResponse", TextInput("bar")))

val file = new java.io.File("http-client/src/test/resources/sample.txt")
val response = HTTP.postMultipart("http://example.com/", FormData("toResponse", FileInput(file, "text/plain")))

val response = HTTP.asyncPost("http://example.com/", "foo=bar")
val response = HTTP.asyncPost("http://example.com/", "foo" -> "bar")
val response = HTTP.asyncPostMultipart("http://example.com/", FormData("toResponse", TextInput("bar")))

// PUT

val response = HTTP.put("http://example.com/", "foo=bar")
val response = HTTP.put("http://example.com/", "foo" -> "bar")
val response = HTTP.asyncPut("http://example.com/", "foo=bar")
val response = HTTP.asyncPut("http://example.com/", "foo" -> "bar")

// DELETE

val response = HTTP.delete("http://example.com/resource")
val response = HTTP.asyncDelete("http://example.com/resource")

// TRACE

val response = HTTP.trace("http://example.com/resource")
val response = HTTP.asyncTrace("http://example.com/resource")

// OPTIONS

val response = HTTP.options("http://example.com/resource")
val response = HTTP.asyncOptions("http://example.com/resource")

unchecked since it is eliminated by erasure

Scalaは、Javaと同様の消去モデル(erasure model)のジェネリックプログラミングを使っており、実行時に型引数の情報を管理しない。

これは型消去と呼ばれ、コップ本の270-272あたりに書いてある。配列だけが型消去の例外と言われるが、具体的な回避方法はよくわからない。すくなくともこのパターンにおいては。

val a = Some(List(1, 2, 3))
val b = Some("hoge")

def printmatch[A](a: Option[A]) = a match {
  // warning: non-variable type argument List[Int] in type pattern Option[List[Int]] is unchecked since it is eliminated by erasure
  case a: Option[List[Int]] => println("option list int")
  case _ => println("i have no idea")
}

printmatch(a) // option list int
printmatch(b) // option list int // 期待してたのと違う

d.hatena.ne.jp

Scalaスケーラブルプログラミング第2版

Scalaスケーラブルプログラミング第2版

  • 作者: Martin Odersky,Lex Spoon,Bill Venners,羽生田栄一,水島宏太,長尾高弘
  • 出版社/メーカー: インプレスジャパン
  • 発売日: 2011/09/27
  • メディア: 単行本(ソフトカバー)
  • 購入: 12人 クリック: 235回
  • この商品を含むブログ (46件) を見る