Current section

Files

Jump to
beanstream lib beanstream.ex
Raw

lib/beanstream.ex

defmodule Beanstream do
@moduledoc """
A HTTP client for Beanstream.
"""
use HTTPoison.Base # based on HTTPoison
require Logger
@endpoint "https://www.beanstream.com/api/v1/"
# Creates the URL for our endpoint.
def process_url(path) do
@endpoint <> path
end
# Static headers for all requests.
defp process_request_headers(headers) do
Enum.into(headers, [{"Content-Type", "application/json"}])
end
# Encode the request as JSON.
defp process_request_body(body) do
Poison.encode!(body)
end
# Decode the response body as JSON.
defp process_response_body(body) do
Logger.debug(body)
case Poison.decode(body) do
{:ok, body} -> body
{:error, _error} -> body
end
end
# Process OK response (performed manually, not a Poison callback)
def process_response({:ok, resp}) do
case resp.status_code do
200 -> {:ok, resp.body}
_ -> Beanstream.Error.new(resp)
end
end
def process_response({:error, error}) do
Logger.error(inspect(error))
{:error, error}
end
end