< 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:109
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 COLLECTIONS_FETCH_URL = "https://peer-lb.decentraland.org/lambdas/collections"; // TODO: Cha
 15        public const string BASE_WEARABLES_COLLECTION_ID = "urn:decentraland:off-chain:base-avatars";
 16
 17        private static Collection[] collections;
 18
 19        private static IEnumerator EnsureCollectionsData()
 20        {
 021            if (collections?.Length > 0)
 022                yield break;
 23
 024            yield return Environment.i.platform.webRequest.Get(
 25                url: COLLECTIONS_FETCH_URL,
 26                downloadHandler: new DownloadHandlerBuffer(),
 27                timeout: 5000,
 28                disposeOnCompleted: false,
 29                OnFail: (webRequest) =>
 30                {
 031                    Debug.LogWarning($"Request error! collections couldn't be fetched! -- {webRequest.error}");
 032                },
 33                OnSuccess: (webRequest) =>
 34                {
 035                    var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(webRequest.downloadHandler
 036                    collections = collectionsApiData.collections;
 037                });
 038        }
 39
 40        /// <summary>
 41        /// Fetches all the existent collection ids and triggers a randomized selection to be loaded
 42        /// </summary>
 43        /// <param name="finalCollectionIdsList">A strings list that will be filled with the randomized collection ids</
 44        public static IEnumerator GetRandomCollections(int amount, bool ensureBaseWearables, List<string> finalCollectio
 45        {
 046            yield return EnsureCollectionsData();
 47
 048            List<int> randomizedIndices = new List<int>();
 49            int randomIndex;
 050            bool addedBaseWearablesCollection = false;
 51
 052            for (int i = 0; i < amount; i++)
 53            {
 054                randomIndex = Random.Range(0, collections.Length);
 55
 056                while (randomizedIndices.Contains(randomIndex))
 57                {
 058                    randomIndex = Random.Range(0, collections.Length);
 59                }
 60
 061                if (collections[randomIndex].id == BASE_WEARABLES_COLLECTION_ID)
 062                    addedBaseWearablesCollection = true;
 63
 064                finalCollectionIdsList.Add(collections[randomIndex].id);
 065                randomizedIndices.Add(randomIndex);
 66            }
 67
 68            // We add the base wearables collection to make sure we have at least 1 of each avatar body-part
 069            if (!addedBaseWearablesCollection && ensureBaseWearables)
 070                finalCollectionIdsList.Add( BASE_WEARABLES_COLLECTION_ID );
 071        }
 72
 73        /// <summary>
 74        /// Given a base url for fetching wearables, this method recursively downloads all the 'pages' responded by the 
 75        /// and populates the global Catalogue with those wearables.
 76        /// </summary>
 77        /// <param name="url">The API url to fetch the list of wearables</param>
 78        /// <param name="finalWearableItemsList">A WearableItems list that will be filled with the fetched wearables</pa
 79        public static IEnumerator GetWearableItems(string url, List<WearableItem> finalWearableItemsList)
 80        {
 081            string nextPageParams = null;
 82
 083            yield return Environment.i.platform.webRequest.Get(
 84                url: url,
 85                downloadHandler: new DownloadHandlerBuffer(),
 86                timeout: 5000,
 87                disposeOnCompleted: false,
 88                OnFail: (webRequest) =>
 89                {
 090                    Debug.LogWarning($"Request error! wearables couldn't be fetched! -- {webRequest.error}");
 091                },
 92                OnSuccess: (webRequest) =>
 93                {
 094                    var wearablesApiData = JsonUtility.FromJson<WearablesAPIData>(webRequest.downloadHandler.text);
 095                    var wearableItemsList = wearablesApiData.GetWearableItems();
 096                    finalWearableItemsList.AddRange(wearableItemsList);
 97
 098                    nextPageParams = wearablesApiData.pagination.next;
 099                });
 100
 0101            if (!string.IsNullOrEmpty(nextPageParams))
 102            {
 103                // Since the wearables deployments response returns only a batch of elements, we need to fetch all the
 104                // batches sequentially
 0105                yield return GetWearableItems(WEARABLES_FETCH_URL + nextPageParams, finalWearableItemsList);
 106            }
 0107        }
 108    }
 109}