Skip to content

Commit

Permalink
adapt to credo
Browse files Browse the repository at this point in the history
  • Loading branch information
Ino Murko committed Apr 15, 2020
1 parent 2799cf8 commit dd9f373
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
1 change: 1 addition & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
excluded: []
},
checks: [
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, [parens: true]},
{Credo.Check.Refactor.LongQuoteBlocks, false}
]
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ethereumex.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Ethereumex do
use Application
alias Ethereumex.Counter
alias Ethereumex.Config
alias Ethereumex.Counter
@moduledoc File.read!("#{__DIR__}/../README.md")
import Supervisor.Spec, warn: false

Expand Down
10 changes: 5 additions & 5 deletions lib/ethereumex/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Ethereumex.Config do
@moduledoc false
alias Ethereumex.IpcServer

def setup_children do
def setup_children() do
setup_children(client_type())
end

Expand Down Expand Up @@ -34,7 +34,7 @@ defmodule Ethereumex.Config do
end

@spec rpc_url() :: binary()
def rpc_url do
def rpc_url() do
case Application.get_env(:ethereumex, :url) do
url when is_binary(url) and url != "" ->
url
Expand All @@ -49,17 +49,17 @@ defmodule Ethereumex.Config do
end

@spec http_options() :: keyword()
def http_options do
def http_options() do
Application.get_env(:ethereumex, :http_options, [])
end

@spec client_type() :: atom()
def client_type do
def client_type() do
Application.get_env(:ethereumex, :client_type, :http)
end

@spec ipc_path() :: binary()
def ipc_path do
def ipc_path() do
case Application.get_env(:ethereumex, :ipc_path, "") do
path when is_binary(path) and path != "" ->
path
Expand Down
40 changes: 24 additions & 16 deletions lib/ethereumex/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@ defmodule Ethereumex.HttpClient do
options = [hackney: [pool: :default]] ++ Config.http_options()
url = Keyword.get(opts, :url) || Config.rpc_url()

with {:ok, response} <- HTTPoison.post(url, payload, headers, options),
%HTTPoison.Response{body: body, status_code: code} = response do
decode_body(body, code)
else
{:error, %HTTPoison.Error{reason: reason}} -> {:error, reason}
e -> {:error, e}
case HTTPoison.post(url, payload, headers, options) do
{:ok, response} ->
%HTTPoison.Response{body: body, status_code: code} = response
decode_body(body, code)

{:error, %HTTPoison.Error{reason: reason}} ->
{:error, reason}

e ->
{:error, e}
end
end

@spec decode_body(binary(), integer()) :: {:ok | :error, any()}
defp decode_body(body, code) do
with {:ok, decoded_body} <- Jason.decode(body) do
case {code, decoded_body} do
{200, %{"error" => error}} -> {:error, error}
{200, result = [%{} | _]} -> {:ok, format_batch(result)}
{200, %{"result" => result}} -> {:ok, result}
_ -> {:error, decoded_body}
end
else
{:error, %Jason.DecodeError{data: ""}} -> {:error, :empty_response}
{:error, error} -> {:error, {:invalid_json, error}}
case Jason.decode(body) do
{:ok, decoded_body} ->
case {code, decoded_body} do
{200, %{"error" => error}} -> {:error, error}
{200, result = [%{} | _]} -> {:ok, format_batch(result)}
{200, %{"result" => result}} -> {:ok, result}
_ -> {:error, decoded_body}
end

{:error, %Jason.DecodeError{data: ""}} ->
{:error, :empty_response}

{:error, error} ->
{:error, {:invalid_json, error}}
end
end
end
18 changes: 8 additions & 10 deletions lib/ethereumex/ipc_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ defmodule Ethereumex.IpcClient do
@timeout 60_000
@spec post_request(binary(), []) :: {:ok | :error, any()}
def post_request(payload, _opts) do
with {:ok, response} <- call_ipc(payload) do
with {:ok, decoded_body} <- Jason.decode(response) do
case decoded_body do
%{"error" => error} -> {:error, error}
result = [%{} | _] -> {:ok, format_batch(result)}
result -> {:ok, Map.get(result, "result")}
end
else
{:error, %Jason.DecodeError{data: ""}} -> {:error, :empty_response}
{:error, error} -> {:error, {:invalid_json, error}}
with {:ok, response} <- call_ipc(payload),
{:ok, decoded_body} <- Jason.decode(response) do
case decoded_body do
%{"error" => error} -> {:error, error}
result = [%{} | _] -> {:ok, format_batch(result)}
result -> {:ok, Map.get(result, "result")}
end
else
{:error, %Jason.DecodeError{data: ""}} -> {:error, :empty_response}
{:error, %Jason.DecodeError{} = error} -> {:error, {:invalid_json, error}}
{:error, error} -> {:error, error}
end
end
Expand Down

0 comments on commit dd9f373

Please sign in to comment.