Skip to content

Commit

Permalink
add option to disable format_batch for batch responses (#131)
Browse files Browse the repository at this point in the history
* add option to disable format_batch for batch responses

* improve

* fix

* bump version
  • Loading branch information
ayrat555 authored Jul 29, 2022
1 parent c8b7a93 commit edc7269
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.13.1-otp-24
erlang 24.1.4
elixir 1.13.4-otp-24
erlang 24.1.7
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.10.1 - 2022-07-29
* Add option to not format batch requests (https://github.com/mana-ethereum/ethereumex/pull/131)

## 0.10.0 - 2022-05-10
* Add EIP1559 support for eth_maxPriorityFeePerGas and eth_feeHistory (https://github.com/mana-ethereum/ethereumex/pull/127)
* Update finch to 0.12.0 (https://github.com/mana-ethereum/ethereumex/pull/126)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add `:ethereumex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ethereumex, "~> 0.9"}
{:ethereumex, "~> 0.10"}
]
end
```
Expand Down
5 changes: 5 additions & 0 deletions lib/ethereumex/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ defmodule Ethereumex.Config do
Application.get_env(:ethereumex, :client_type, :http)
end

@spec format_batch() :: boolean()
def format_batch() do
Application.get_env(:ethereumex, :format_batch, true)
end

@spec poolboy_config() :: keyword()
defp poolboy_config() do
[
Expand Down
33 changes: 26 additions & 7 deletions lib/ethereumex/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Ethereumex.HttpClient do
@moduledoc false

use Ethereumex.Client.BaseClient

alias Ethereumex.Config

@type opt :: {:url, String.t()}
Expand All @@ -13,26 +14,40 @@ defmodule Ethereumex.HttpClient do
def post_request(payload, opts) do
headers = Keyword.get(opts, :http_headers) || Config.http_headers()
url = Keyword.get(opts, :url) || Config.rpc_url()

format_batch =
case Keyword.get(opts, :format_batch) do
nil -> Config.format_batch()
value -> value
end

request = Finch.build(:post, url, headers, payload)

case Finch.request(request, Ethereumex.Finch, Config.http_options()) do
{:ok, %Finch.Response{body: body, status: code}} ->
decode_body(body, code)
decode_body(body, code, format_batch)

{:error, error} ->
{:error, error}
end
end

@spec decode_body(binary(), non_neg_integer()) :: {:ok, any()} | http_client_error()
defp decode_body(body, code) do
@spec decode_body(binary(), non_neg_integer(), boolean()) :: {:ok, any()} | http_client_error()
defp decode_body(body, code, format_batch) do
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}
{200, %{"error" => error}} ->
{:error, error}

{200, result = [%{} | _]} ->
{:ok, maybe_format_batch(result, format_batch)}

{200, %{"result" => result}} ->
{:ok, result}

_ ->
{:error, decoded_body}
end

{:error, %Jason.DecodeError{data: ""}} ->
Expand All @@ -42,4 +57,8 @@ defmodule Ethereumex.HttpClient do
{:error, {:invalid_json, error}}
end
end

defp maybe_format_batch(responses, true), do: format_batch(responses)

defp maybe_format_batch(responses, _), do: responses
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Ethereumex.Mixfile do
use Mix.Project

@source_url "https://github.com/exthereum/ethereumex"
@version "0.10.0"
@version "0.10.1"

def project do
[
Expand Down

0 comments on commit edc7269

Please sign in to comment.