Skip to content

Commit

Permalink
cleanup: move request function above private handle_response
Browse files Browse the repository at this point in the history
and reuse message received in the handler
  • Loading branch information
SpaceEEC committed Jul 16, 2018
1 parent efbee5c commit 06e8ce3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
61 changes: 37 additions & 24 deletions lib/rest/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/rest/handler/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule Crux.Rest.Handler do

@doc false
def handle_call(
{:queue, request_data},
{:queue, request_data} = message,
from,
%{
route: route,
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 06e8ce3

Please sign in to comment.