PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/content/posts/gigahorse-030.md

https://github.com/eed3si9n/eed3si9n.com
Markdown | 63 lines | 48 code | 15 blank | 0 comment | 0 complexity | 8c43e14469f623170a998f9db894970b 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: /gigahorse-030
  10. aliases: [ /node/223 ]
  11. ---
  12. [okhttp]: http://square.github.io/okhttp/
  13. Gigahorse 0.3.0 is now released. See [documentation](http://eed3si9n.com/gigahorse/) on what it is.
  14. ### OkHttp support
  15. 0.3.0 adds [Square OkHttp][okhttp] support. Gigahorse-OkHttp is availble for Scala 2.10, 2.11, and 2.12.
  16. According to the JavaDoc you actually don't have to close the `OkHttpClient` instance.
  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. However, you **must** close the response object if you don't consume the body contents.
  29. Normally you'd consume the body content as `Gigahorse.asString` or something like that.
  30. In that sense, the design is similar to 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 also does not provide streaming body or Reactive stream support, so if you need that check out Akka HTTP-backed or AHC-backed Gigahorse.
  47. ### Shaded AHC 2.0
  48. Gigahorse now shades AHC 2.0.