parent
d30d49bdd8
commit
35f2df7c46
@ -0,0 +1,63 @@ |
||||
defmodule BlockScoutWeb.API.EthRPC.EthController do |
||||
use BlockScoutWeb, :controller |
||||
|
||||
alias BlockScoutWeb.AccessHelpers |
||||
alias BlockScoutWeb.API.EthRPC.View, as: EthRPCView |
||||
alias Explorer.EthRPC |
||||
|
||||
def eth_request(%{body_params: %{"_json" => requests}} = conn, _) when is_list(requests) do |
||||
case AccessHelpers.check_rate_limit(conn) do |
||||
:ok -> |
||||
responses = EthRPC.responses(requests) |
||||
|
||||
conn |
||||
|> put_status(200) |
||||
|> put_view(EthRPCView) |
||||
|> render("responses.json", %{responses: responses}) |
||||
|
||||
:rate_limit_reached -> |
||||
AccessHelpers.handle_rate_limit_deny(conn) |
||||
end |
||||
end |
||||
|
||||
def eth_request(%{body_params: %{"_json" => request}} = conn, _) do |
||||
case AccessHelpers.check_rate_limit(conn) do |
||||
:ok -> |
||||
[response] = EthRPC.responses([request]) |
||||
|
||||
conn |
||||
|> put_status(200) |
||||
|> put_view(EthRPCView) |
||||
|> render("response.json", %{response: response}) |
||||
|
||||
:rate_limit_reached -> |
||||
AccessHelpers.handle_rate_limit_deny(conn) |
||||
end |
||||
end |
||||
|
||||
def eth_request(conn, request) do |
||||
case AccessHelpers.check_rate_limit(conn) do |
||||
:ok -> |
||||
# In the case that the JSON body is sent up w/o a json content type, |
||||
# Phoenix encodes it as a single key value pair, with the value being |
||||
# nil and the body being the key (as in a CURL request w/ no content type header) |
||||
decoded_request = |
||||
with [{single_key, nil}] <- Map.to_list(request), |
||||
{:ok, decoded} <- Jason.decode(single_key) do |
||||
decoded |
||||
else |
||||
_ -> request |
||||
end |
||||
|
||||
[response] = EthRPC.responses([decoded_request]) |
||||
|
||||
conn |
||||
|> put_status(200) |
||||
|> put_view(EthRPCView) |
||||
|> render("response.json", %{response: response}) |
||||
|
||||
:rate_limit_reached -> |
||||
AccessHelpers.handle_rate_limit_deny(conn) |
||||
end |
||||
end |
||||
end |
@ -1,40 +0,0 @@ |
||||
defmodule BlockScoutWeb.API.RPC.EthController do |
||||
use BlockScoutWeb, :controller |
||||
|
||||
alias Explorer.EthRPC |
||||
|
||||
def eth_request(%{body_params: %{"_json" => requests}} = conn, _) when is_list(requests) do |
||||
responses = EthRPC.responses(requests) |
||||
|
||||
conn |
||||
|> put_status(200) |
||||
|> render("responses.json", %{responses: responses}) |
||||
end |
||||
|
||||
def eth_request(%{body_params: %{"_json" => request}} = conn, _) do |
||||
[response] = EthRPC.responses([request]) |
||||
|
||||
conn |
||||
|> put_status(200) |
||||
|> render("response.json", %{response: response}) |
||||
end |
||||
|
||||
def eth_request(conn, request) do |
||||
# In the case that the JSON body is sent up w/o a json content type, |
||||
# Phoenix encodes it as a single key value pair, with the value being |
||||
# nil and the body being the key (as in a CURL request w/ no content type header) |
||||
decoded_request = |
||||
with [{single_key, nil}] <- Map.to_list(request), |
||||
{:ok, decoded} <- Jason.decode(single_key) do |
||||
decoded |
||||
else |
||||
_ -> request |
||||
end |
||||
|
||||
[response] = EthRPC.responses([decoded_request]) |
||||
|
||||
conn |
||||
|> put_status(200) |
||||
|> render("response.json", %{response: response}) |
||||
end |
||||
end |
@ -1,13 +0,0 @@ |
||||
defmodule BlockScoutWeb.API.RPC.EthView do |
||||
use BlockScoutWeb, :view |
||||
|
||||
alias BlockScoutWeb.API.RPC.EthRPCView |
||||
|
||||
def render("responses.json", %{responses: responses}) do |
||||
EthRPCView.render("responses.json", %{responses: responses}) |
||||
end |
||||
|
||||
def render("response.json", %{response: response}) do |
||||
EthRPCView.render("response.json", %{response: response}) |
||||
end |
||||
end |
Loading…
Reference in new issue