PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 0ms 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---
 2title:       "Gigahorse 0.3.0"
 3type:        story
 4date:        2017-04-27
 5changed:     2021-09-13
 6draft:       false
 7promote:     true
 8sticky:      false
 9url:         /gigahorse-030
10aliases:     [ /node/223 ]
11---
12
13  [okhttp]: http://square.github.io/okhttp/
14
15Gigahorse 0.3.0 is now released. See [documentation](http://eed3si9n.com/gigahorse/) on what it is.
16
17### OkHttp support
18
190.3.0 adds [Square OkHttp][okhttp] support. Gigahorse-OkHttp is availble for Scala 2.10, 2.11, and 2.12.
20
21According to the JavaDoc you actually don't have to close the `OkHttpClient` instance.
22
23```scala
24scala> import gigahorse._, support.okhttp.Gigahorse
25import gigahorse._
26import support.okhttp.Gigahorse
27
28scala> import scala.concurrent._, duration._
29import scala.concurrent._
30import duration._
31
32scala> val http = Gigahorse.http(Gigahorse.config) // don't have to close
33http: gigahorse.HttpClient = gigahorse.support.okhttp.OkhClient@23b48158
34```
35<!--more-->
36
37However, you **must** close the response object if you don't consume the body contents.
38Normally you'd consume the body content as `Gigahorse.asString` or something like that.
39In that sense, the design is similar to Akka HTTP.
40
41```scala
42scala> val r = Gigahorse.url("http://api.duckduckgo.com").get.
43     |           addQueryString(
44     |             "q" -> "1 + 1",
45     |             "format" -> "json"
46     |           )
47r: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(q -> List(1 + 1), format -> List(json)), None, None, None, None, None, None)
48
49scala> val f = http.run(r, { res: FullResponse =>
50     |   res.close() // must close if you don't consume the body
51     |   1
52     | })
53f: scala.concurrent.Future[Int] = Future(<not completed>)
54
55scala> Await.result(f, 120.seconds)
56res0: Int = 1
57```
58
59OkHttp also does not provide streaming body or Reactive stream support, so if you need that check out Akka HTTP-backed or AHC-backed Gigahorse.
60
61### Shaded AHC 2.0
62
63Gigahorse now shades AHC 2.0.