< Summary

Class:NFTInfoRetriever
Assembly:DCL.Components.NFT
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTAsset/NFTInfoLoadHelper.cs
Covered lines:9
Uncovered lines:33
Coverable lines:42
Total lines:101
Line coverage:21.4% (9 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Dispose()0%4.074083.33%
FetchNFTInfo(...)0%2.062075%
FetchNFTInfo()0%1101000%
FetchNFTInfoCoroutine()0%4.123050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTAsset/NFTInfoLoadHelper.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Text.RegularExpressions;
 4using System.Threading;
 5using Cysharp.Threading.Tasks;
 6using DCL.Helpers.NFT;
 7using UnityEngine;
 8
 9public interface INFTInfoRetriever : IDisposable
 10{
 11    public event Action<NFTInfo> OnFetchInfoSuccess;
 12    public event Action OnFetchInfoFail;
 13    void FetchNFTInfo(string address, string id);
 14    UniTask<NFTInfo> FetchNFTInfo(string src);
 15}
 16
 17public class NFTInfoRetriever : INFTInfoRetriever
 18{
 19    internal const string COULD_NOT_FETCH_DAR_URL = "Couldn't fetch DAR url '{0}' for NFTShape.";
 20    internal const string ACCEPTED_URL_FORMAT = "The accepted format is 'ethereum://ContractAddress/TokenID'.";
 21    internal const string SUPPORTED_PROTOCOL = "The only protocol currently supported is 'ethereum'.";
 22
 23    public event Action<NFTInfo> OnFetchInfoSuccess;
 24    public event Action OnFetchInfoFail;
 25    internal Coroutine fetchCoroutine;
 26    private CancellationTokenSource tokenSource;
 27
 28    public void Dispose()
 29    {
 30        // Note: When we delete de old component NFTShape, we should remove the coroutine part
 331        if (fetchCoroutine != null)
 232            CoroutineStarter.Stop(fetchCoroutine);
 33
 334        fetchCoroutine = null;
 335        tokenSource?.Cancel();
 336        tokenSource?.Dispose();
 037    }
 38
 39    public void FetchNFTInfo(string address, string id)
 40    {
 241        if (fetchCoroutine != null)
 042            CoroutineStarter.Stop(fetchCoroutine);
 43
 244        fetchCoroutine = CoroutineStarter.Start(FetchNFTInfoCoroutine(address, id));
 245    }
 46
 47    public async UniTask<NFTInfo> FetchNFTInfo(string src)
 48    {
 049        tokenSource = new CancellationTokenSource();
 050        tokenSource.Token.ThrowIfCancellationRequested();
 51        // Check the src follows the needed format e.g.: 'ethereum://0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/558536'
 052        var regexMatches = Regex.Matches(src, "(?<protocol>[^:]+)://(?<registry>0x([A-Fa-f0-9])+)(?:/(?<asset>.+))?");
 053        if (regexMatches.Count == 0 || regexMatches[0].Groups["protocol"] == null || regexMatches[0].Groups["registry"] 
 54        {
 055            string errorMessage = string.Format(COULD_NOT_FETCH_DAR_URL + " " + ACCEPTED_URL_FORMAT, src);
 056            Debug.Log(errorMessage);
 057            OnFetchInfoFail?.Invoke();
 058            return null;
 59        }
 60
 061        Match match = regexMatches[0];
 062        string darURLProtocol = match.Groups["protocol"].ToString();
 063        if (darURLProtocol != "ethereum")
 64        {
 065            string errorMessage = string.Format(COULD_NOT_FETCH_DAR_URL + " " + SUPPORTED_PROTOCOL + " " + ACCEPTED_URL_
 066            Debug.Log(errorMessage);
 067            OnFetchInfoFail?.Invoke();
 068            return null;
 69        }
 70
 071        string darURLRegistry = match.Groups["registry"].ToString();
 072        string darURLAsset = match.Groups["asset"].ToString();
 73
 074        NFTInfo nftInformation = null;
 75
 076        var rutine = NFTUtils.FetchNFTInfo(darURLRegistry, darURLAsset,
 77            (info) =>
 78            {
 079                nftInformation = info;
 080            },
 81            (error) =>
 82            {
 083                Debug.LogError($"Couldn't fetch NFT: '{darURLRegistry}/{darURLAsset}' {error}");
 084            });
 85
 086        await rutine.WithCancellation(tokenSource.Token);
 87
 088        return nftInformation;
 089    }
 90
 91    private IEnumerator FetchNFTInfoCoroutine(string address, string id)
 92    {
 293        yield return NFTUtils.FetchNFTInfo(address, id,
 094            (info) => { OnFetchInfoSuccess?.Invoke(info); },
 95            (error) =>
 96            {
 097                Debug.LogError($"Couldn't fetch NFT: '{address}/{id}' {error}");
 098                OnFetchInfoFail?.Invoke();
 099            });
 0100    }
 101}