From a298d69ea9aac21453fe34f632d57166b33be1a1 Mon Sep 17 00:00:00 2001 From: Gary Button Date: Mon, 30 Jul 2018 15:15:25 -0700 Subject: [PATCH 1/2] Dyamic url --- README.md | 4 ++++ config/test.exs | 2 +- lib/ethereumex/client/macro.ex | 15 +++++++++------ lib/ethereumex/client/server.ex | 12 ++++++++---- lib/ethereumex/http_client.ex | 13 +++++++------ test/ethereumex/client/macro_test.exs | 25 +++++++++++++++++++++++++ test/test_helper.exs | 2 +- 7 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 test/ethereumex/client/macro_test.exs diff --git a/README.md b/README.md index 466a4ee..5d5fb93 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,10 @@ config :ethereumex, iex> Ethereumex.HttpClient.web3_client_version {:ok, "Parity//v1.7.2-beta-9f47909-20170918/x86_64-macos/rustc1.19.0"} +# Using the url option will overwrite the configuration +iex> Ethereumex.HttpClient.web3_client_version(url: "http://localhost:8545") +{:ok, "Parity//v1.7.2-beta-9f47909-20170918/x86_64-macos/rustc1.19.0"} + iex> Ethereumex.HttpClient.web3_sha3("wrong_param") {:error, %{"code" => -32602, "message" => "Invalid params: invalid format."}} diff --git a/config/test.exs b/config/test.exs index 0303066..6fcad06 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,3 +1,3 @@ use Mix.Config -config :ethereumex, url: "http://localhost:8545" +config :ethereumex, url: "http://localhost:9545" diff --git a/lib/ethereumex/client/macro.ex b/lib/ethereumex/client/macro.ex index 77496d0..431a7d1 100644 --- a/lib/ethereumex/client/macro.ex +++ b/lib/ethereumex/client/macro.ex @@ -419,14 +419,17 @@ defmodule Ethereumex.Client.Macro do @spec request(binary(), list(binary() | boolean | map), keyword()) :: {:ok, any() | [any()]} | error + def request(_name, _params, batch: true, url: _url), + do: raise("Cannot use batch and url options at the same time") + def request(name, params, batch: true) do name |> add_request_info(params) end - def request(name, params, _) do + def request(name, params, opts) do name |> add_request_info(params) - |> server_request + |> server_request(opts) end @spec batch_request([{atom(), list(binary())}]) :: {:ok, [any()]} | error @@ -441,8 +444,8 @@ defmodule Ethereumex.Client.Macro do |> server_request end - defp server_request(params) do - GenServer.call(__MODULE__, {:request, params}) + defp server_request(params, opts \\ []) do + GenServer.call(__MODULE__, {:request, params, opts}) end def start_link do @@ -453,11 +456,11 @@ defmodule Ethereumex.Client.Macro do GenServer.cast(__MODULE__, :reset_id) end - def single_request(params) do + def single_request(params, opts) do {:error, :not_implemented} end - defoverridable single_request: 1 + defoverridable single_request: 2 end end end diff --git a/lib/ethereumex/client/server.ex b/lib/ethereumex/client/server.ex index 024bc77..e5206d7 100644 --- a/lib/ethereumex/client/server.ex +++ b/lib/ethereumex/client/server.ex @@ -3,11 +3,15 @@ defmodule Ethereumex.Client.Server do @moduledoc false + def init(args) do + {:ok, args} + end + def start_link(module) when is_atom(module) do GenServer.start_link(__MODULE__, {module, 0}, name: module) end - def handle_call({:request, params}, _from, {module, id}) when is_list(params) do + def handle_call({:request, params, opts}, _from, {module, id}) when is_list(params) do params = params |> Enum.with_index() @@ -15,15 +19,15 @@ defmodule Ethereumex.Client.Server do Map.put(req_data, "id", index + id) end) - response = apply(module, :single_request, [params]) + response = apply(module, :single_request, [params, opts]) {:reply, response, {module, id + Enum.count(params)}} end - def handle_call({:request, params}, _from, {module, id}) do + def handle_call({:request, params, opts}, _from, {module, id}) do params = Map.put(params, "id", id) - response = apply(module, :single_request, [params]) + response = apply(module, :single_request, [params, opts]) {:reply, response, {module, id + 1}} end diff --git a/lib/ethereumex/http_client.ex b/lib/ethereumex/http_client.ex index 37c23af..b8fd423 100644 --- a/lib/ethereumex/http_client.ex +++ b/lib/ethereumex/http_client.ex @@ -3,11 +3,11 @@ defmodule Ethereumex.HttpClient do import Ethereumex.Config @moduledoc false - @spec single_request(map()) :: {:ok, any() | [any()]} | error - def single_request(payload) do + @spec single_request(map(), []) :: {:ok, any() | [any()]} | error + def single_request(payload, opts \\ []) do payload |> encode_payload - |> post_request + |> post_request(opts) end @spec encode_payload(map()) :: binary() @@ -15,12 +15,13 @@ defmodule Ethereumex.HttpClient do payload |> Poison.encode!() end - @spec post_request(binary()) :: {:ok | :error, any()} - defp post_request(payload) do + @spec post_request(binary(), []) :: {:ok | :error, any()} + defp post_request(payload, opts) do headers = [{"Content-Type", "application/json"}] options = Ethereumex.Config.http_options() + url = Keyword.get(opts, :url) || rpc_url() - with {:ok, response} <- HTTPoison.post(rpc_url(), payload, headers, options), + with {:ok, response} <- HTTPoison.post(url, payload, headers, options), %HTTPoison.Response{body: body, status_code: code} = response do decode_body(body, code) else diff --git a/test/ethereumex/client/macro_test.exs b/test/ethereumex/client/macro_test.exs new file mode 100644 index 0000000..5c1c359 --- /dev/null +++ b/test/ethereumex/client/macro_test.exs @@ -0,0 +1,25 @@ +defmodule Ethereumex.Client.MacroTest do + use ExUnit.Case + alias Ethereumex.HttpClient + + setup_all do + HttpClient.start_link() + + :ok + end + + @tag :macro + describe "Macro.request/3" do + test "throws and error when using the batch and node options" do + assert_raise(RuntimeError, fn -> HttpClient.request("web3_clientVersion", [], batch: true, url: "http://localhost:8545") end) + end + + test "using the application url" do + {:ok, _} = HttpClient.request("web3_clientVersion", [], []) + end + + test "supply a request url" do + {:error, :econnrefused} = HttpClient.request("web3_clientVersion", [], url: "http://localhost:1234") + end + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 12ae0b9..fbcc258 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,5 +1,5 @@ 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, :macro] ) ExUnit.start() From 652bd44adfd1ce6d31f85e7169bcec4a52b3adb3 Mon Sep 17 00:00:00 2001 From: Gary Button Date: Tue, 31 Jul 2018 09:28:58 -0700 Subject: [PATCH 2/2] Bump version --- CHANGELOG.md | 3 +++ README.md | 2 +- mix.exs | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e4984e..95920ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.3.3 +* Added dynamic url input(https://github.com/exthereum/ethereumex/pull/37) + # 0.3.2 * Fix eth_getLogs mathod params (https://github.com/exthereum/ethereumex/pull/20) diff --git a/README.md b/README.md index 5d5fb93..1f85284 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Add Ethereumex to your `mix.exs` dependencies: 1. Add `ethereumex` to your list of dependencies in `mix.exs`: ```elixir def deps do - [{:ethereumex, "~> 0.3.2"}] + [{:ethereumex, "~> 0.3.3"}] end ``` diff --git a/mix.exs b/mix.exs index a4fafc4..a594fcd 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Ethereumex.Mixfile do def project do [ app: :ethereumex, - version: "0.3.2", + version: "0.3.3", elixir: "~> 1.6", description: "Elixir JSON-RPC client for the Ethereum blockchain", package: [