Created
January 15, 2016 21:05
-
-
Save stevedomin/ec111edf47a9c7b1daf3 to your computer and use it in GitHub Desktop.
How to test an API client in Elixir?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyBank.Client do | |
defstruct client_id: nil, access_token: nil | |
def request(client, method, path, params \\ %{}, body \\ nil, headers \\ []) do | |
# prepare url, headers, etc. | |
case HTTPoison.request(method, url, body, headers) do | |
{:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, body} | |
# handle other cases ... | |
end | |
end | |
end | |
defmodule MyBank.Accounts do | |
def list(client) do | |
case MyBank.Client.request(client, :get, "/accounts") do | |
{:ok, body} -> Poison.decode!(body, as: %{"accounts" => [MyBank.Account]) | |
{: | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment