/docs/03-processing.md

https://github.com/eed3si9n/gigahorse · Markdown · 91 lines · 67 code · 24 blank · 0 comment · 0 complexity · b6b3c082636d4ca8e1641ac79aad04ec MD5 · raw file

  1. ---
  2. out:processing.html
  3. ---
  4. [concepts]: concepts.html
  5. Processing the FullResponse
  6. ---------------------------
  7. Once you build a `Request` value, you can pass it to `HttpClient`
  8. to execute the request using `run`, `download`, `processFull`, `runStream` methods.
  9. ### http.run(r, f)
  10. There are many methods on `HttpClient`, but probably the most useful one is
  11. `http.run(r, f)` method. As we saw in [Basic Concepts][concepts] page
  12. this take a `Request` value, and a function `FullResponse => A`.
  13. Gigahorse provides `Gigahorse.asString` function to return `Future[String]`,
  14. but we can imagine this could be expanded to do more.
  15. Another thing to note is that `run` method will only accept HTTP 2XX statuses,
  16. and fail the future value otherwise. (By default 3XX redirects are handled automatically)
  17. ### Post-processing a Future
  18. In addition to passing in a function, a `Future` can easily be post-processed
  19. by mapping inside it.
  20. ```scala
  21. import gigahorse._, support.okhttp.Gigahorse
  22. import scala.concurrent._, duration._
  23. import ExecutionContext.Implicits._
  24. val http = Gigahorse.http(Gigahorse.config)
  25. val r = Gigahorse.url("https://api.duckduckgo.com").get.
  26. addQueryString(
  27. "q" -> "1 + 1"
  28. )
  29. val f0: Future[FullResponse] = http.run(r, identity)
  30. val f: Future[String] = f0 map { Gigahorse.asString andThen (_.take(60)) }
  31. Await.result(f, 120.seconds)
  32. ```
  33. Whenever an operation is done on a `Future`, an implicit execution context must be available
  34. -- this declares which thread pool the callback to the future should run in.
  35. For convenience there's an overload of `run` that takes only the `Request` parameter.
  36. ### Lifting the FullResponse to Either
  37. One of the common processing when dealing with a Future that can fail is to
  38. lift the inner `A` value to `Either[Throwable, A]`.
  39. There's a convenient website called <http://getstatuscode.com/>
  40. that can emulate HTTP statuses. Here's what happens when we await on a failed Future.
  41. ```scala
  42. val r = Gigahorse.url("http://getstatuscode.com/500")
  43. val f = http.run(r, Gigahorse.asString)
  44. Await.result(f, 120.seconds)
  45. ```
  46. Gigahorse provides a mechanism called `Gigahorse.asEither` to
  47. lift the inner `A` value to `Either[Throwable, A]` as follows:
  48. ```scala
  49. val r = Gigahorse.url("http://getstatuscode.com/500")
  50. val f = http.run(r, Gigahorse.asEither)
  51. Await.result(f, 120.seconds)
  52. ```
  53. `asEither` can be mapped over as a right-biased `Either`.
  54. ```scala
  55. val r = Gigahorse.url("http://getstatuscode.com/200")
  56. val f = http.run(r, Gigahorse.asEither map {
  57. Gigahorse.asString andThen (_.take(60)) })
  58. Await.result(f, 120.seconds)
  59. ```
  60. ### http.processFull(r, f)
  61. If you do not wish to throw an error on non-2XX responses, and for example
  62. read the body text of a 500 response, use `processFull` method.
  63. ```scala
  64. val r = Gigahorse.url("http://getstatuscode.com/500")
  65. val f = http.processFull(r, Gigahorse.asString andThen (_.take(60)))
  66. Await.result(f, 120.seconds)
  67. ```