Zerion API proxy (#9896)
* Zerion API proxy * Add envs to common-blockscout.env * Change proxy endpoint pathpull/9905/head
parent
58c1915959
commit
ac2ff4638e
@ -0,0 +1,24 @@ |
||||
defmodule BlockScoutWeb.API.V2.Proxy.ZerionController do |
||||
use BlockScoutWeb, :controller |
||||
|
||||
alias BlockScoutWeb.API.V2.AddressController |
||||
|
||||
alias Explorer.ThirdPartyIntegrations.Zerion |
||||
|
||||
action_fallback(BlockScoutWeb.API.V2.FallbackController) |
||||
|
||||
@doc """ |
||||
Function to handle GET requests to `/api/v2/proxy/zerion/wallet-portfolio/:address_hash_param` endpoint. |
||||
""" |
||||
@spec wallet_portfolio(Plug.Conn.t(), map()) :: Plug.Conn.t() | {atom(), any()} |
||||
def wallet_portfolio(conn, %{"address_hash_param" => address_hash_string} = params) do |
||||
with {:ok, _address_hash, _address} <- AddressController.validate_address(address_hash_string, params), |
||||
url = Zerion.wallet_portfolio_url(address_hash_string), |
||||
{response, status} <- Zerion.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,45 @@ |
||||
defmodule Explorer.ThirdPartyIntegrations.Zerion do |
||||
@moduledoc """ |
||||
Module for Zerion API integration https://developers.zerion.io/reference |
||||
""" |
||||
|
||||
alias Explorer.Helper |
||||
alias Explorer.Utility.Microservice |
||||
|
||||
@recv_timeout 60_000 |
||||
|
||||
@doc """ |
||||
Proxy request to Zerion API endpoints |
||||
""" |
||||
@spec api_request(String.t(), Plug.Conn.t(), :get | :post_transactions) :: {any(), integer()} |
||||
def api_request(url, conn, method \\ :get) |
||||
|
||||
def api_request(url, _conn, :get) do |
||||
auth_token = Base.encode64("#{api_key()}:") |
||||
headers = [{"Authorization", "Basic #{auth_token}"}] |
||||
|
||||
case HTTPoison.get(url, headers, recv_timeout: @recv_timeout) do |
||||
{:ok, %HTTPoison.Response{status_code: status, body: body}} -> |
||||
{Helper.decode_json(body), status} |
||||
|
||||
_ -> |
||||
{nil, 500} |
||||
end |
||||
end |
||||
|
||||
@doc """ |
||||
Zerion /wallets/{accountAddress}/portfolio endpoint |
||||
""" |
||||
@spec wallet_portfolio_url(String.t()) :: String.t() |
||||
def wallet_portfolio_url(address_hash_string) do |
||||
"#{base_url()}/wallets/#{address_hash_string}/portfolio" |
||||
end |
||||
|
||||
defp base_url do |
||||
Microservice.base_url(__MODULE__) |
||||
end |
||||
|
||||
defp api_key do |
||||
Application.get_env(:explorer, __MODULE__)[:api_key] |
||||
end |
||||
end |
Loading…
Reference in new issue