diff --git a/lib/rest/base.ex b/lib/rest/base.ex index 2fd1fcf..59dbe20 100644 --- a/lib/rest/base.ex +++ b/lib/rest/base.ex @@ -26,6 +26,43 @@ defmodule Crux.Rest.Base do |> Keyword.put_new(:"user-agent", @user_agent) end + @spec request( + method :: String.t(), + route :: String.t(), + body :: term(), + headers :: [{String.t() | atom(), String.t()}], + Keyword.t() + ) :: :ok | {:ok, term()} | {:error, term()} + def request(method, route, body \\ "", headers \\ [], options \\ []) do + [headers, body] = + case body do + %{reason: reason} -> + headers = [{"x-audit-log-reason", URI.encode(reason)} | headers] + body = Map.delete(body, :reason) + + [headers, body] + + _ -> + [headers, body] + end + + super(method, @api_base <> route, body, headers, options) + end + + @spec queue( + method :: String.t(), + route :: String.t(), + body :: term(), + headers :: [{String.t() | atom(), String.t()}], + Keyword.t() + ) :: :ok | {:ok, term()} | {:error, term()} + def queue(method, route, body \\ "", headers \\ [], options \\ []) do + {route, [method, route, body, headers, options]} + |> Crux.Rest.Handler.queue() + |> handle_response(method) + end + + defp handle_response({:error, _} = error, _method), do: error defp handle_response({:ok, %HTTPoison.Response{status_code: 204}}, _method), do: :ok defp handle_response({:ok, %HTTPoison.Response{status_code: 200, body: body}}, _method) do @@ -54,28 +91,4 @@ defmodule Crux.Rest.Base do {:error, {:decoding, body}} end end - - defp handle_response({:error, _} = error, _method), do: error - - def queue(method, route, body \\ "", headers \\ [], options \\ []) do - {route, [method, route, body, headers, options]} - |> Handler.queue() - |> handle_response(method) - end - - def request(method, route, body \\ "", headers \\ [], options \\ []) do - [headers, body] = - case body do - %{reason: reason} -> - headers = [{"x-audit-log-reason", URI.encode(reason)} | headers] - body = Map.delete(body, :reason) - - [headers, body] - - _ -> - [headers, body] - end - - super(method, @api_base <> route, body, headers, options) - end end diff --git a/lib/rest/handler/handler.ex b/lib/rest/handler/handler.ex index 623deb8..252c263 100644 --- a/lib/rest/handler/handler.ex +++ b/lib/rest/handler/handler.ex @@ -91,7 +91,7 @@ defmodule Crux.Rest.Handler do @doc false def handle_call( - {:queue, request_data}, + {:queue, request_data} = message, from, %{ route: route, @@ -125,7 +125,7 @@ defmodule Crux.Rest.Handler do Logger.debug("[Crux][Rest][Handler][#{route}]: Waiting #{res}ms") :timer.sleep(res) - handle_call({:queue, request_data}, from, state) + handle_call(message, from, state) true -> reset_time =