feat: Add /api/v2/proxy/metadata/addresses endpoint (#10585)
* Init * feat: Add /api/v2/proxy/metadata/addresses endpoint * Process review commentspull/10603/head
parent
6caf8148f3
commit
c9ddc19b22
@ -0,0 +1,30 @@ |
||||
defmodule BlockScoutWeb.API.V2.Proxy.MetadataController do |
||||
@moduledoc """ |
||||
Controller for the metadata service |
||||
""" |
||||
use BlockScoutWeb, :controller |
||||
|
||||
alias Explorer.MicroserviceInterfaces.Metadata |
||||
|
||||
action_fallback(BlockScoutWeb.API.V2.FallbackController) |
||||
|
||||
@doc """ |
||||
Function to handle GET requests to `/api/v2/proxy/metadata/addresses` endpoint. |
||||
Proxies request to the metadata service. Preloads additional info to the addresses in response. |
||||
If some addresses are not found, they'll be discarded. |
||||
""" |
||||
def addresses(conn, params) do |
||||
with {code, body} <- Metadata.get_addresses(params) do |
||||
case code do |
||||
200 -> |
||||
conn |
||||
|> render(:addresses, %{result: body}) |
||||
|
||||
status_code -> |
||||
conn |
||||
|> put_status(status_code) |
||||
|> json(body) |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,13 @@ |
||||
defmodule BlockScoutWeb.API.V2.Proxy.MetadataView do |
||||
use BlockScoutWeb, :view |
||||
|
||||
alias BlockScoutWeb.API.V2.AddressView |
||||
|
||||
def render("addresses.json", %{result: {:ok, %{"addresses" => addresses} = body}}) do |
||||
Map.put(body, "addresses", Enum.map(addresses, &AddressView.prepare_address/1)) |
||||
end |
||||
|
||||
def render("addresses.json", %{result: :error}) do |
||||
%{error: "Decoding error"} |
||||
end |
||||
end |
Loading…
Reference in new issue