Transformation: Convert Response
We’ll need a JSON library to convert the response into a data structure. Searching hex.pm, I found the Erlang library jsx,[22] so let’s add its dependency to our mix.exs file.
project/2/issues/mix.exs | |
| defp deps do |
| [ |
| { :httpoison, "~> 0.4" }, |
| { :jsx, "~> 2.0" } |
| ] |
| end |
Run mix deps.get, and you’ll end up with jsx installed.
To convert the body from a string, we call the jsx decode function when we return the message from the GitHub API:
project/3/issues/lib/issues/github_issues.ex | |
| def handle_response(%{status_code: 200, body: body}) do |
* | { :ok, :jsx.decode(body) } |
| end |
| |
| def handle_response(%{status_code: ___, body: body}) do |
* | { :error, :jsx.decode(body) } |
| end |
We also have to deal ...
Get Programming Elixir now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.