parent
48bfc78b81
commit
9825192aba
@ -0,0 +1,61 @@ |
||||
defmodule BlockScoutWeb.API.V2.Proxy.NovesFiController do |
||||
use BlockScoutWeb, :controller |
||||
|
||||
alias BlockScoutWeb.API.V2.{AddressController, TransactionController} |
||||
alias Explorer.ThirdPartyIntegrations.NovesFi |
||||
|
||||
action_fallback(BlockScoutWeb.API.V2.FallbackController) |
||||
|
||||
@doc """ |
||||
Function to handle GET requests to `/api/v2/proxy/noves-fi/transactions/:transaction_hash_param` endpoint. |
||||
""" |
||||
@spec transaction(Plug.Conn.t(), map()) :: Plug.Conn.t() | {atom(), any()} |
||||
def transaction(conn, %{"transaction_hash_param" => transaction_hash_string} = params) do |
||||
with {:ok, _transaction, _transaction_hash} <- |
||||
TransactionController.validate_transaction(transaction_hash_string, params, |
||||
necessity_by_association: %{}, |
||||
api?: true |
||||
), |
||||
url = NovesFi.tx_url(transaction_hash_string), |
||||
{response, status} <- NovesFi.noves_fi_api_request(url, conn), |
||||
{:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do |
||||
conn |
||||
|> put_status(status) |
||||
|> json(response) |
||||
end |
||||
end |
||||
|
||||
@doc """ |
||||
Function to handle GET requests to `/api/v2/proxy/noves-fi/transactions/:transaction_hash_param/describe` endpoint. |
||||
""" |
||||
@spec describe_transaction(Plug.Conn.t(), map()) :: Plug.Conn.t() | {atom(), any()} |
||||
def describe_transaction(conn, %{"transaction_hash_param" => transaction_hash_string} = params) do |
||||
with {:ok, _transaction, _transaction_hash} <- |
||||
TransactionController.validate_transaction(transaction_hash_string, params, |
||||
necessity_by_association: %{}, |
||||
api?: true |
||||
), |
||||
url = NovesFi.describe_tx_url(transaction_hash_string), |
||||
{response, status} <- NovesFi.noves_fi_api_request(url, conn), |
||||
{:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do |
||||
conn |
||||
|> put_status(status) |
||||
|> json(response) |
||||
end |
||||
end |
||||
|
||||
@doc """ |
||||
Function to handle GET requests to `/api/v2/proxy/noves-fi/transactions/:transaction_hash_param/describe` endpoint. |
||||
""" |
||||
@spec address_transactions(Plug.Conn.t(), map()) :: Plug.Conn.t() | {atom(), any()} |
||||
def address_transactions(conn, %{"address_hash_param" => address_hash_string} = params) do |
||||
with {:ok, _address_hash, _address} <- AddressController.validate_address(address_hash_string, params), |
||||
url = NovesFi.address_txs_url(address_hash_string), |
||||
{response, status} <- NovesFi.noves_fi_api_request(url, conn), |
||||
{:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do |
||||
conn |
||||
|> put_status(status) |
||||
|> json(response) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,66 @@ |
||||
defmodule Explorer.ThirdPartyIntegrations.NovesFi do |
||||
@moduledoc """ |
||||
Module for Noves.Fi API integration https://blockscout.noves.fi/swagger/index.html |
||||
""" |
||||
|
||||
alias Explorer.Helper |
||||
|
||||
@recv_timeout 60_000 |
||||
|
||||
@doc """ |
||||
Proxy request to noves.fi API endpoints |
||||
""" |
||||
@spec noves_fi_api_request(String.t(), Plug.Conn.t()) :: any() |
||||
def noves_fi_api_request(url, conn) do |
||||
headers = [{"apiKey", api_key()}] |
||||
url_with_params = url <> "?" <> conn.query_string |
||||
|
||||
case HTTPoison.get(url_with_params, headers, recv_timeout: @recv_timeout) do |
||||
{:ok, %HTTPoison.Response{status_code: status, body: body}} -> |
||||
{Helper.decode_json(body), status} |
||||
|
||||
_ -> |
||||
nil |
||||
end |
||||
end |
||||
|
||||
@doc """ |
||||
Noves.fi /evm/{chain}/tx/{txHash} endpoint |
||||
""" |
||||
@spec tx_url(String.t()) :: String.t() |
||||
def tx_url(transaction_hash_string) do |
||||
"#{base_url()}/evm/#{chain_name()}/tx/#{transaction_hash_string}" |
||||
end |
||||
|
||||
@doc """ |
||||
Noves.fi /evm/{chain}/describeTx/{txHash} endpoint |
||||
""" |
||||
@spec describe_tx_url(String.t()) :: String.t() |
||||
def describe_tx_url(transaction_hash_string) do |
||||
"#{base_url()}/evm/#{chain_name()}/describeTx/#{transaction_hash_string}" |
||||
end |
||||
|
||||
@doc """ |
||||
Noves.fi /evm/{chain}/txs/{accountAddress} endpoint |
||||
""" |
||||
@spec address_txs_url(String.t()) :: String.t() |
||||
def address_txs_url(address_hash_string) do |
||||
"#{base_url()}/evm/#{chain_name()}/txs/#{address_hash_string}" |
||||
end |
||||
|
||||
defp base_url do |
||||
api_base_url() || "https://blockscout.noves.fi" |
||||
end |
||||
|
||||
defp api_base_url do |
||||
Application.get_env(:explorer, __MODULE__)[:api_base_url] |
||||
end |
||||
|
||||
defp chain_name do |
||||
Application.get_env(:explorer, __MODULE__)[:chain_name] |
||||
end |
||||
|
||||
defp api_key do |
||||
Application.get_env(:explorer, __MODULE__)[:api_key] |
||||
end |
||||
end |
Loading…
Reference in new issue