Current section
Files
Jump to
Current section
Files
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
Logger.debug("request url: " <>@endpoint <> path)
@endpoint <> path
end
# Static headers for all requests.
defp process_request_headers(headers) do
h = Enum.into(headers, [{"Content-Type", "application/json"}])
Logger.debug("request headers: " <> inspect(h))
h
end
# Encode the request as JSON.
defp process_request_body(body) do
b = Poison.encode!(body)
Logger.debug("request body: " <> b)
b
end
# Decode the response body as JSON.
defp process_response_body(body) do
Logger.debug("response body: " <> 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("response error: " <> inspect(error))
{:error, error}
end
end