< 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:105
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%72800%
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        public const string WEARABLES_FETCH_URL = "https://peer.decentraland.org/lambdas/collections/wearables?";
 13        private const string COLLECTIONS_FETCH_URL = "https://peer.decentraland.org/lambdas/collections";
 14        private const string BASE_WEARABLES_COLLECTION_ID = "urn:decentraland:off-chain:base-avatars";
 15
 16        private static Collection[] collections;
 17        private static IEnumerator EnsureCollectionsData()
 18        {
 019            if (collections?.Length > 0)
 020                yield break;
 21
 022            yield return Environment.i.platform.webRequest.Get(
 23                url: COLLECTIONS_FETCH_URL,
 24                downloadHandler: new DownloadHandlerBuffer(),
 25                timeout: 5000,
 26                disposeOnCompleted: false,
 27                OnFail: (webRequest) =>
 28                {
 029                    Debug.LogWarning($"Request error! collections couldn't be fetched! -- {webRequest.error}");
 030                },
 31                OnSuccess: (webRequest) =>
 32                {
 033                    var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(webRequest.downloadHandler
 034                    collections = collectionsApiData.collections;
 035                });
 036        }
 37
 38        /// <summary>
 39        /// Fetches all the existent collection ids and triggers a randomized selection to be loaded
 40        /// </summary>
 41        /// <param name="finalCollectionIdsList">A strings list that will be filled with the randomized collection ids</
 42        public static IEnumerator GetRandomCollections(int amount, bool ensureBaseWearables, List<string> finalCollectio
 43        {
 044            yield return EnsureCollectionsData();
 45
 046            List<int> randomizedIndices = new List<int>();
 47            int randomIndex;
 048            bool addedBaseWearablesCollection = false;
 049            for (int i = 0; i < amount; i++)
 50            {
 051                randomIndex = Random.Range(0, collections.Length);
 052                while (randomizedIndices.Contains(randomIndex))
 53                {
 054                    randomIndex = Random.Range(0, collections.Length);
 55                }
 56
 057                if (ensureBaseWearables && collections[randomIndex].id == BASE_WEARABLES_COLLECTION_ID)
 058                    addedBaseWearablesCollection = true;
 59
 060                finalCollectionIdsList.Add(collections[randomIndex].id);
 061                randomizedIndices.Add(randomIndex);
 62            }
 63
 64            // We add the base wearables collection to make sure we have at least 1 of each avatar body-part
 065            if (!addedBaseWearablesCollection)
 066                finalCollectionIdsList[0] = BASE_WEARABLES_COLLECTION_ID;
 067        }
 68
 69        /// <summary>
 70        /// Given a base url for fetching wearables, this method recursively downloads all the 'pages' responded by the 
 71        /// and populates the global Catalogue with those wearables.
 72        /// </summary>
 73        /// <param name="url">The API url to fetch the list of wearables</param>
 74        /// <param name="finalWearableItemsList">A WearableItems list that will be filled with the fetched wearables</pa
 75        public static IEnumerator GetWearableItems(string url, List<WearableItem> finalWearableItemsList)
 76        {
 077            string nextPageParams = null;
 78
 079            yield return Environment.i.platform.webRequest.Get(
 80                url: url,
 81                downloadHandler: new DownloadHandlerBuffer(),
 82                timeout: 5000,
 83                disposeOnCompleted: false,
 84                OnFail: (webRequest) =>
 85                {
 086                    Debug.LogWarning($"Request error! wearables couldn't be fetched! -- {webRequest.error}");
 087                },
 88                OnSuccess: (webRequest) =>
 89                {
 090                    var wearablesApiData = JsonUtility.FromJson<WearablesAPIData>(webRequest.downloadHandler.text);
 091                    var wearableItemsList = wearablesApiData.GetWearableItems();
 092                    finalWearableItemsList.AddRange(wearableItemsList);
 93
 094                    nextPageParams = wearablesApiData.pagination.next;
 095                });
 96
 097            if (!string.IsNullOrEmpty(nextPageParams))
 98            {
 99                // Since the wearables deployments response returns only a batch of elements, we need to fetch all the
 100                // batches sequentially
 0101                yield return GetWearableItems(WEARABLES_FETCH_URL + nextPageParams, finalWearableItemsList);
 102            }
 0103        }
 104    }
 105}