PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/content/posts/gigahorse-030.ja.md

https://github.com/eed3si9n/eed3si9n.com
Markdown | 64 lines | 48 code | 16 blank | 0 comment | 0 complexity | 89260e6e40fa678bf337546cebe24dfa MD5 | raw file
  1. ---
  2. title: "Gigahorse 0.3.0"
  3. type: story
  4. date: 2017-04-27
  5. changed: 2021-09-13
  6. draft: false
  7. promote: true
  8. sticky: false
  9. url: /ja/gigahorse-030
  10. aliases: [ /node/224 ]
  11. ---
  12. [okhttp]: http://square.github.io/okhttp/
  13. Gigahorse 0.3.0 をリリースしたGigahorse が何かは[ドキュメンテーション](http://eed3si9n.com/gigahorse/ja/)をみてほしい。
  14. ### OkHttp サポート
  15. 0.3.0 [Square OkHttp][okhttp] サポートを追加する Gigahorse-OkHttp Scala 2.10, 2.11, 2.12 向けにクロスビルドされている
  16. JavaDoc によると`OkHttpClient` のインスタンスは close しなくてもいいらしい
  17. ```scala
  18. scala> import gigahorse._, support.okhttp.Gigahorse
  19. import gigahorse._
  20. import support.okhttp.Gigahorse
  21. scala> import scala.concurrent._, duration._
  22. import scala.concurrent._
  23. import duration._
  24. scala> val http = Gigahorse.http(Gigahorse.config) // don't have to close
  25. http: gigahorse.HttpClient = gigahorse.support.okhttp.OkhClient@23b48158
  26. ```
  27. <!--more-->
  28. ただしレスポンスオブジェクトのボディコンテンツを消費しない場合は**必ず** close する必要がある
  29. 通常はボディコンテンツは `Gigahorse.asString` などのメソッドで消費される
  30. そういう意味では Akka HTTP の設計に似ているかもしれない
  31. ```scala
  32. scala> val r = Gigahorse.url("http://api.duckduckgo.com").get.
  33. | addQueryString(
  34. | "q" -> "1 + 1",
  35. | "format" -> "json"
  36. | )
  37. r: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(q -> List(1 + 1), format -> List(json)), None, None, None, None, None, None)
  38. scala> val f = http.run(r, { res: FullResponse =>
  39. | res.close() // must close if you don't consume the body
  40. | 1
  41. | })
  42. f: scala.concurrent.Future[Int] = Future(<not completed>)
  43. scala> Await.result(f, 120.seconds)
  44. res0: Int = 1
  45. ```
  46. OkHttp はボディのストリームや Reactive stream のサポートが無いためそれが必要ならば Akka HTTP 版か AHC 版の Gigahorse を使ってほしい
  47. ### Shaded AHC 2.0
  48. Gigahorse AHC 2.0 をシェーディングするようようになった