defmodule PortalApiWrapper do @moduledoc """ Documentation for PortalApiWrapper. """ require Logger defp get_url do Application.get_env(:portal_api_wrapper, :portal_api_url) end defp get_client do Application.get_env(:portal_api_wrapper, :http_client) end defp make_url(method, collection) do get_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 """ Call to api for sign_in Takes (email, password) returns - {:ok, token} - {:error, code, message} (non-200 response) - {:error, message} (no response) """ def sign_in(email, password) do url = get_url <> "/sign_in" headers = make_headers() body = %{email: email, password: password} |> Poison.encode! with {:ok, %{status_code: 200, body: body}} <- get_client.post(url, body, headers), {:ok, %{jwt: token}} <- Poison.decode(body, keys: :atoms) do {:ok, token} else {:ok, %{status_code: code, body: body}} -> Logger.error("Error: sign_in failed with code #{code}, params: #{inspect body}") {:error, code, body} err -> Logger.error("Error: sign_in no response, message: #{inspect err}") err end 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, message} 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}} <- get_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, body: body}} -> Logger.error("Error: index #{collection} returned code #{code}, query: #{inspect query}") {:error, code, body} 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, message} 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) params = Poison.encode!(params) with {:ok, %{body: body, status_code: 200}} <- get_client.post(url, params, headers), {:ok, body = %{_id: id}} <- Poison.decode(body, keys: :atoms) do {:ok, body} else {:ok, %{status_code: code, body: body}} -> Logger.error("Error: create #{collection} returned code #{code}, params: #{inspect params}") {:error, code, body} 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, message} 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}} <- get_client.get("#{url}/#{object_id}", headers), {:ok, body = %{_id: id}} <- Poison.decode(body, keys: :atoms) do {:ok, body} else {:ok, %{status_code: code, body: body}} -> Logger.error("Error: show #{collection}:#{object_id} returned code #{code}") {:error, code, body} 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, message} 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) params = Poison.encode!(params) with {:ok, %{body: body, status_code: 200}} <- get_client.patch("#{url}/#{object_id}", params, headers), {:ok, body = %{_id: id}} <- Poison.decode(body, keys: :atoms) do {:ok, body} else {:ok, %{status_code: code, body: body}} -> Logger.error("Error: update #{collection}:#{object_id} returned code #{code}") {:error, code, body} err -> Logger.error("Error: update #{collection}:#{object_id} no response, message: #{inspect err}") err end end end