Skip to content

Commit

Permalink
chore: add json header by default (#156)
Browse files Browse the repository at this point in the history
* chore: add json header by default

* fix tests

* Update lib/ethereumex/http_client.ex

Co-authored-by: Alisina Bahadori <[email protected]>

---------

Co-authored-by: Alisina Bahadori <[email protected]>
  • Loading branch information
ayrat555 and alisinabh authored Dec 12, 2023
1 parent 8d0bf56 commit f051c4a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Config
config :ethereumex,
http_options: [pool_timeout: 5000, receive_timeout: 15_000],
http_pool_options: %{},
http_headers: [{"Content-Type", "application/json"}]
http_headers: []

# config :ethereumex, ipc_path: "/Library/Application Support/io.parity.ethereum/jsonrpc.ipc"
import_config "#{Mix.env()}.exs"
8 changes: 7 additions & 1 deletion lib/ethereumex/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Ethereumex.HttpClient do

@spec post_request(binary(), Keyword.t()) :: {:ok, any()} | http_client_error()
def post_request(payload, opts) do
headers = Keyword.get(opts, :http_headers) || Config.http_headers()
headers = headers(opts)
url = Keyword.get(opts, :url) || Config.rpc_url()

format_batch =
Expand All @@ -31,6 +31,12 @@ defmodule Ethereumex.HttpClient do
end
end

defp headers(opts) do
headers = Keyword.get(opts, :http_headers) || Config.http_headers()

[{"Content-Type", "application/json"} | headers]
end

@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
Expand Down
2 changes: 1 addition & 1 deletion test/ethereumex/http_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule Ethereumex.HttpClientTest do
end
end

@tag :eth
@tag :eth_chain_id
describe "HttpClient.eth_chain_id/1" do
test "returns chain id of the RPC serveer" do
result = HttpClient.eth_chain_id()
Expand Down
2 changes: 1 addition & 1 deletion test/ethereumex/ipc_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule Ethereumex.IpcClientTest do
end
end

@tag :eth
@tag :eth_chain_id
describe "IpcClient.eth_chain_id/1" do
test "returns chain id of the RPC serveer" do
result = IpcClient.eth_chain_id()
Expand Down
14 changes: 13 additions & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
ExUnit.configure(
exclude: [:skip, :web3, :net, :eth, :eth_mine, :eth_db, :shh, :eth_compile, :eth_sign, :batch]
exclude: [
:skip,
:web3,
:net,
:eth,
:eth_mine,
:eth_db,
:shh,
:eth_compile,
:eth_sign,
:batch,
:eth_chain_id
]
)

Application.ensure_all_started(:telemetry)
Expand Down

0 comments on commit f051c4a

Please sign in to comment.