< Summary

Class:DCL.Helpers.WearablesFetchingHelper
Assembly:WearablesFetching
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/Wearables/WearablesFetchingHelper.cs
Covered lines:0
Uncovered lines:44
Coverable lines:44
Total lines:148
Line coverage:0% (0 of 44)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EnsureCollectionsData()0%72800%
GetCollectionsFetchURL()0%2100%
GetWearablesFetchURL()0%2100%
GetBaseCollections()0%12300%
GetRandomCollections()0%30500%
GetWearableItems()0%42600%
GetThirdPartyCollections()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/Wearables/WearablesFetchingHelper.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using Newtonsoft.Json;
 4using UnityEngine;
 5using UnityEngine.Networking;
 6using Random = UnityEngine.Random;
 7using Collection = WearableCollectionsAPIData.Collection;
 8
 9namespace DCL.Helpers
 10{
 11    public static class WearablesFetchingHelper
 12    {
 13        // TODO: change fetching logic to allow for auto-pagination
 14        // The https://nft-api.decentraland.org/v1/ endpoint doesn't fetch L1 wearables right now, if those need to be r
 15        public const string BASE_FETCH_URL = "https://peer.decentraland.org/lambdas/collections";
 16        public const string COLLECTIONS_FETCH_PARAMS = "?sortBy=newest&first=1000";
 17        public const string WEARABLES_FETCH_PARAMS = "/wearables?";
 18        public const string BASE_WEARABLES_COLLECTION_ID = "urn:decentraland:off-chain:base-avatars";
 19        public const string THIRD_PARTY_COLLECTIONS_FETCH_URL = "third-party-integrations";
 20        private static Collection[] collections;
 21
 22        private static IEnumerator EnsureCollectionsData()
 23        {
 024            if (collections?.Length > 0)
 025                yield break;
 26
 027            yield return Environment.i.platform.webRequest.Get(
 28                url: GetCollectionsFetchURL(),
 29                downloadHandler: new DownloadHandlerBuffer(),
 30                timeout: 60,
 31                disposeOnCompleted: false,
 32                OnFail: (webRequest) =>
 33                {
 034                    Debug.LogWarning($"Request error! collections couldn't be fetched! -- {webRequest.webRequest.error}"
 035                },
 36                OnSuccess: (webRequest) =>
 37                {
 038                    var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(webRequest.webRequest.down
 039                    collections = collectionsApiData.data;
 040                });
 041        }
 42
 43        public static string GetCollectionsFetchURL()
 44        {
 045            return $"{BASE_FETCH_URL}{COLLECTIONS_FETCH_PARAMS}";
 46        }
 47
 48        public static string GetWearablesFetchURL()
 49        {
 050            return $"{BASE_FETCH_URL}{WEARABLES_FETCH_PARAMS}";
 51        }
 52
 53        /// <summary>
 54        /// Fetches base collection ids and adds them to the provided ids list
 55        /// </summary>
 56        /// <param name="finalCollectionIdsList">A strings list that will be filled with the base collection ids</param>
 57        public static IEnumerator GetBaseCollections(List<string> finalCollectionIdsList)
 58        {
 059            yield return EnsureCollectionsData();
 60
 061            finalCollectionIdsList.Add( BASE_WEARABLES_COLLECTION_ID );
 062        }
 63
 64        public static IEnumerator GetRandomCollections(int amount, List<string> finalCollectionIdsList)
 65        {
 066            yield return EnsureCollectionsData();
 67
 068            List<int> randomizedIndices = new List<int>();
 69            int randomIndex;
 70            bool addedBaseWearablesCollection = false;
 71
 072            for (int i = 0; i < amount; i++)
 73            {
 074                randomIndex = Random.Range(0, collections.Length);
 75
 076                while (randomizedIndices.Contains(randomIndex))
 77                {
 078                    randomIndex = Random.Range(0, collections.Length);
 79                }
 80
 081                if (collections[randomIndex].urn == BASE_WEARABLES_COLLECTION_ID)
 82                    addedBaseWearablesCollection = true;
 83
 084                finalCollectionIdsList.Add(collections[randomIndex].urn);
 085                randomizedIndices.Add(randomIndex);
 86            }
 087        }
 88
 89        /// <summary>
 90        /// Given a base url for fetching wearables, this method recursively downloads all the 'pages' responded by the 
 91        /// and populates the global Catalogue with those wearables.
 92        /// </summary>
 93        /// <param name="url">The API url to fetch the list of wearables</param>
 94        /// <param name="finalWearableItemsList">A WearableItems list that will be filled with the fetched wearables</pa
 95        public static IEnumerator GetWearableItems(string url, List<WearableItem> finalWearableItemsList)
 96        {
 097            string nextPageParams = null;
 98
 099            yield return Environment.i.platform.webRequest.Get(
 100                url: url,
 101                downloadHandler: new DownloadHandlerBuffer(),
 102                timeout: 60,
 103                disposeOnCompleted: false,
 104                OnFail: (webRequest) =>
 105                {
 0106                    Debug.LogWarning($"Request error! wearables couldn't be fetched! -- {webRequest.webRequest.error}");
 0107                },
 108                OnSuccess: (webRequest) =>
 109                {
 0110                    var wearablesApiData = JsonConvert.DeserializeObject<WearablesAPIData>(webRequest.webRequest.downloa
 0111                    var wearableItemsList = wearablesApiData.GetWearableItems();
 0112                    finalWearableItemsList.AddRange(wearableItemsList);
 113
 0114                    nextPageParams = wearablesApiData.pagination.next;
 0115                });
 116
 0117            if (!string.IsNullOrEmpty(nextPageParams))
 118            {
 119                // Since the wearables deployments response returns only a batch of elements, we need to fetch all the
 120                // batches sequentially
 0121                yield return GetWearableItems(
 122                    GetWearablesFetchURL() + $"{nextPageParams}",
 123                    finalWearableItemsList);
 124            }
 0125        }
 126
 127        public static Promise<Collection[]> GetThirdPartyCollections()
 128        {
 0129            Promise<Collection[]> promiseResult = new Promise<Collection[]>();
 130
 0131            Environment.i.platform.webRequest.Get(
 132                url: $"{Environment.i.platform.serviceProviders.catalyst.lambdasUrl}/{THIRD_PARTY_COLLECTIONS_FETCH_URL}
 133                downloadHandler: new DownloadHandlerBuffer(),
 134                timeout: 60,
 135                OnFail: (webRequest) =>
 136                {
 0137                    promiseResult.Reject($"Request error! third party collections couldn't be fetched! -- {webRequest.we
 0138                },
 139                OnSuccess: (webRequest) =>
 140                {
 0141                    var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(webRequest.webRequest.down
 0142                    promiseResult.Resolve(collectionsApiData.data);
 0143                });
 144
 0145            return promiseResult;
 146        }
 147    }
 148}