< 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:35
Coverable lines:35
Total lines:112
Line coverage:0% (0 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EnsureCollectionsData()0%72800%
GetRandomCollections()0%56700%
GetWearableItems()0%42600%

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 UnityEngine;
 4using UnityEngine.Networking;
 5using Random = UnityEngine.Random;
 6using Collection = WearableCollectionsAPIData.Collection;
 7
 8namespace DCL.Helpers
 9{
 10    public static class WearablesFetchingHelper
 11    {
 12        // TODO: dinamically use ICatalyst.contentUrl, content server is not a const
 13        public const string WEARABLES_FETCH_URL = "https://peer-lb.decentraland.org/lambdas/collections/wearables?";
 14        public const string BASE_WEARABLES_COLLECTION_ID = "urn:decentraland:off-chain:base-avatars";
 15
 16        // TODO: change fetching logic to allow for auto-pagination
 17        // The https://nft-api.decentraland.org/v1/ endpoint doesn't fetch L1 wearables right now, if those need to be r
 18        // public const string COLLECTIONS_FETCH_URL = "https://peer-lb.decentraland.org/lambdas/collections";
 19        public const string COLLECTIONS_FETCH_URL = "https://nft-api.decentraland.org/v1/collections?sortBy=newest&first
 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: COLLECTIONS_FETCH_URL,
 29                downloadHandler: new DownloadHandlerBuffer(),
 30                timeout: 5000,
 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        /// <summary>
 44        /// Fetches all the existent collection ids and triggers a randomized selection to be loaded
 45        /// </summary>
 46        /// <param name="finalCollectionIdsList">A strings list that will be filled with the randomized collection ids</
 47        public static IEnumerator GetRandomCollections(int amount, bool ensureBaseWearables, List<string> finalCollectio
 48        {
 049            yield return EnsureCollectionsData();
 50
 051            List<int> randomizedIndices = new List<int>();
 52            int randomIndex;
 053            bool addedBaseWearablesCollection = false;
 54
 055            for (int i = 0; i < amount; i++)
 56            {
 057                randomIndex = Random.Range(0, collections.Length);
 58
 059                while (randomizedIndices.Contains(randomIndex))
 60                {
 061                    randomIndex = Random.Range(0, collections.Length);
 62                }
 63
 064                if (collections[randomIndex].urn == BASE_WEARABLES_COLLECTION_ID)
 065                    addedBaseWearablesCollection = true;
 66
 067                finalCollectionIdsList.Add(collections[randomIndex].urn);
 068                randomizedIndices.Add(randomIndex);
 69            }
 70
 71            // We add the base wearables collection to make sure we have at least 1 of each avatar body-part
 072            if (!addedBaseWearablesCollection && ensureBaseWearables)
 073                finalCollectionIdsList.Add( BASE_WEARABLES_COLLECTION_ID );
 074        }
 75
 76        /// <summary>
 77        /// Given a base url for fetching wearables, this method recursively downloads all the 'pages' responded by the 
 78        /// and populates the global Catalogue with those wearables.
 79        /// </summary>
 80        /// <param name="url">The API url to fetch the list of wearables</param>
 81        /// <param name="finalWearableItemsList">A WearableItems list that will be filled with the fetched wearables</pa
 82        public static IEnumerator GetWearableItems(string url, List<WearableItem> finalWearableItemsList)
 83        {
 084            string nextPageParams = null;
 85
 086            yield return Environment.i.platform.webRequest.Get(
 87                url: url,
 88                downloadHandler: new DownloadHandlerBuffer(),
 89                timeout: 5000,
 90                disposeOnCompleted: false,
 91                OnFail: (webRequest) =>
 92                {
 093                    Debug.LogWarning($"Request error! wearables couldn't be fetched! -- {webRequest.webRequest.error}");
 094                },
 95                OnSuccess: (webRequest) =>
 96                {
 097                    var wearablesApiData = JsonUtility.FromJson<WearablesAPIData>(webRequest.webRequest.downloadHandler.
 098                    var wearableItemsList = wearablesApiData.GetWearableItems();
 099                    finalWearableItemsList.AddRange(wearableItemsList);
 100
 0101                    nextPageParams = wearablesApiData.pagination.next;
 0102                });
 103
 0104            if (!string.IsNullOrEmpty(nextPageParams))
 105            {
 106                // Since the wearables deployments response returns only a batch of elements, we need to fetch all the
 107                // batches sequentially
 0108                yield return GetWearableItems(WEARABLES_FETCH_URL + nextPageParams, finalWearableItemsList);
 109            }
 0110        }
 111    }
 112}