Skip to content

Instantly share code, notes, and snippets.

@stevedomin
Created January 15, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevedomin/ec111edf47a9c7b1daf3 to your computer and use it in GitHub Desktop.
Save stevedomin/ec111edf47a9c7b1daf3 to your computer and use it in GitHub Desktop.
How to test an API client in Elixir?
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