From 06e8ce3062e10ba1f9c451060cd61180314c0d52 Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Mon, 16 Jul 2018 12:29:16 +0200 Subject: [PATCH] cleanup: move request function above private handle_response and reuse message received in the handler --- lib/rest/base.ex | 61 ++++++++++++++++++++++--------------- lib/rest/handler/handler.ex | 4 +-- 2 files changed, 39 insertions(+), 26 deletions(-) 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 =