Transformation: Convert Response

We’ll need a JSON library to convert the response into a data structure. Searching hex.pm, I found the poison library (no relation to HTTPoison), so let’s add its dependency to our mix.exs file.[23]

 defp​ deps ​do
  [
  { ​:httpoison​, ​"​​~> 1.0.0"​ },
  { ​:poison​, ​"​​~> 3.1"​ },
  ]
 end

Run mix deps.get, and you’ll end up with poison installed.

To convert the body from a string, we call the Poison.Parser.parse! function when we return the message from the GitHub API:

 def​ handle_response({ _, %{​status_code:​ status_code, ​body:​ body}}) ​do
  {
  status_code |> check_for_error(),
  body |> Poison.Parser.parse!()
  }
 end
 

Get Programming Elixir ≥ 1.6 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.