読者です 読者をやめる 読者になる 読者になる

by shigemk2

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

Skinnyのhttpクライアント

Scala

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")