O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required