From 57abb0047ccace1935b8f30184260250fa32364d Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 4 Oct 2019 18:58:52 +0300 Subject: [PATCH] handle metadata in token uri --- .../token/instance_metadata_retriever.ex | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex b/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex index f1472c7a38..8b530934aa 100644 --- a/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex +++ b/apps/explorer/lib/explorer/token/instance_metadata_retriever.ex @@ -50,7 +50,30 @@ defmodule Explorer.Token.InstanceMetadataRetriever do {:error, :no_uri} end - defp fetch_json(%{"tokenURI" => {:ok, [token_uri]}}) do + defp fetch_json(%{"tokenURI" => {:ok, ["http://" <> _ = token_uri]}}) do + fetch_metadata(token_uri) + end + + defp fetch_json(%{"tokenURI" => {:ok, ["https://" <> _ = token_uri]}}) do + fetch_metadata(token_uri) + end + + defp fetch_json(%{"tokenURI" => {:ok, [json]}}) do + Jason.decode(json) + rescue + e -> + Logger.error(fn -> ["Unknown metadata format #{inspect(json)}. error #{inspect(e)}"] end) + + {:error, json} + end + + defp fetch_json(result) do + Logger.error(fn -> ["Unknown metadata format #{result}."] end) + + {:error, result} + end + + defp fetch_metadata(token_uri) do case HTTPoison.get(token_uri) do {:ok, %Response{body: body, status_code: 200}} -> Jason.decode(body) @@ -67,8 +90,4 @@ defmodule Explorer.Token.InstanceMetadataRetriever do {:error, :request_error} end - - defp fetch_json(result) do - {:error, result} - end end