Current section
Files
Jump to
Current section
Files
lib/portal_api_wrapper.ex
defmodule PortalApiWrapper do
@moduledoc """
Documentation for PortalApiWrapper.
"""
require Logger
@portal_api_url Application.get_env(:portal_api_wrapper, :portal_api_url)
@http_client Application.get_env(:portal_api_wrapper, :http_client)
defp make_url(method, collection) do
@portal_api_url <> "/" <> Atom.to_string(collection)
end
defp make_headers("Bearer " <> token), do: make_headers(token)
defp make_headers(token) when is_binary(token) do
[
{"content-type", "application/json"},
{"authorization", "Bearer " <> token}
]
end
defp make_headers do
[
{"Content-Type", "application/json"}
]
end
defp make_query(search, paging) do
search
|> Kernel.++(paging)
|> URI.encode_query
end
@doc """
Make an index call against against an api collection
- `collection` is the atom name of the collection, e.g., :campaigns, :order_lines, ets
- `search_params` is a keyword list of search params
- `paging_params` is a keyword list of paging params
- `token` is the auth token to include in the header
returns one of:
- {:ok, results, paging} where results is a list and paging is a map
- {:error, code: code} response had non 200 code
- {:error, _} request failed, no response
"""
def index(collection, search_params, paging_params, token) do
url = make_url(:index, collection)
query = "?" <> make_query(search_params, paging_params)
url = url <> query
headers = make_headers(token)
with {:ok, %{body: body, status_code: 200}} <- @http_client.get(url, headers),
{:ok, %{results: results, page_data: page_data}} <- Poison.decode(body, keys: :atoms) do
{:ok, results, page_data}
else
{:ok, %{status_code: code}} ->
Logger.error("Error: index #{collection} returned code #{code}, query: #{inspect query}")
{:error, code: code}
err ->
Logger.error("Error: index #{collection} no response, message: #{inspect err}")
err
end
end
@doc """
Make a create call against against an api collection
- `collection` is the atom name of the collection, e.g., :campaigns, :order_lines, ets
- `params` is the request body
- `token` is the auth token to include in the header
returns one of:
- {:ok, object} where object is teh created object
- {:error, code: code} response had non 200 code
- {:error, _} request failed, no response
"""
def create(collection, params, token) do
url = make_url(:create, collection)
headers = make_headers(token)
with {:ok, %{body: body, status_code: 200}} <- @http_client.post(url, params, headers),
{:ok, body = %{_id: id}} <- Poison.decode(body, keys: :atoms) do
{:ok, body}
else
{:ok, %{status_code: code}} ->
Logger.error("Error: create #{collection} returned code #{code}, params: #{inspect params}")
{:error, code: code}
err ->
Logger.error("Error: create #{collection} no response, message: #{inspect err}")
err
end
end
@doc """
Make a show call against against an object in an api collection
- `collection` is the atom name of the collection, e.g., :campaigns, :order_lines, ets
- `object_id` is the id of the object in question
- `token` is the auth token to include in the header
returns one of:
- {:ok, object} where object is teh created object
- {:error, code: code} response had non 200 code
- {:error, _} request failed, no response
"""
def show(collection, object_id, token) do
url = make_url(:show, collection)
headers = make_headers(token)
with {:ok, %{body: body, status_code: 200}} <- @http_client.get("#{url}/#{object_id}", headers),
{:ok, body = %{_id: id}} <- Poison.decode(body, keys: :atoms) do
{:ok, body}
else
{:ok, %{status_code: code}} ->
Logger.error("Error: show #{collection}:#{object_id} returned code #{code}")
{:error, code: code}
err ->
Logger.error("Error: show #{collection}:#{object_id} no response, message: #{inspect err}")
err
end
end
@doc """
Make an update call against against an object in an api collection
- `collection` is the atom name of the collection, e.g., :campaigns, :order_lines, ets
- `object_id` is the id of the object in question
- `params` is the map of updates to apply
- `token` is the auth token to include in the header
returns one of:
- {:ok, object} where object is teh created object
- {:error, code: code} response had non 200 code
- {:error, _} request failed, no response
"""
def update(collection, object_id, params, token) do
url = make_url(:update, collection)
headers = make_headers(token)
with {:ok, %{body: body, status_code: 200}} <- @http_client.patch("#{url}/#{object_id}", params, headers),
{:ok, body = %{_id: id}} <- Poison.decode(body, keys: :atoms) do
{:ok, body}
else
{:ok, %{status_code: code}} ->
Logger.error("Error: update #{collection}:#{object_id} returned code #{code}")
{:error, code: code}
err ->
Logger.error("Error: update #{collection}:#{object_id} no response, message: #{inspect err}")
err
end
end
end