| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | | using UnityEditor; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Networking; |
| | 9 | |
|
| | 10 | | namespace DCL.ABConverter |
| | 11 | | { |
| | 12 | | public static class WearablesCollectionClient |
| | 13 | | { |
| | 14 | | private static WearableCollectionsAPIData.Collection[] wearableCollections; |
| | 15 | | private static double wearablesCollectionDumpStartTime; |
| | 16 | |
|
| | 17 | | public static string BuildWearableCollectionFetchingURL(string targetCollectionId) => |
| 0 | 18 | | WearablesFetchingHelper.GetWearablesFetchURL() + "collectionId=" + targetCollectionId; |
| | 19 | |
|
| | 20 | | public static List<WearableItem> GetBaseWearableCollections() => |
| 0 | 21 | | GetWearableItems(BuildWearableCollectionFetchingURL(WearablesFetchingHelper.BASE_WEARABLES_COLLECTION_ID)); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Given a base-url to fetch wearables collections, returns a list of all the WearableItems |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="url">base-url to fetch the wearables collections</param> |
| | 27 | | /// <returns>A list of all the WearableItems found</returns> |
| | 28 | | private static List<WearableItem> GetWearableItems(string url) |
| | 29 | | { |
| 0 | 30 | | UnityWebRequest w = UnityWebRequest.Get(url); |
| 0 | 31 | | w.SendWebRequest(); |
| | 32 | |
|
| 0 | 33 | | while (!w.isDone) { } |
| | 34 | |
|
| 0 | 35 | | if (!w.WebRequestSucceded()) |
| | 36 | | { |
| 0 | 37 | | Debug.LogError($"Request error! Wearable at '{url}' couldn't be fetched! -- {w.error}"); |
| 0 | 38 | | return null; |
| | 39 | | } |
| | 40 | |
|
| 0 | 41 | | var wearablesApiData = JsonConvert.DeserializeObject<WearablesAPIData>(w.downloadHandler.text); |
| 0 | 42 | | var resultList = wearablesApiData.GetWearableItems(); |
| | 43 | |
|
| | 44 | | // Since the wearables deployments response returns only a batch of elements, we need to fetch all the |
| | 45 | | // batches sequentially |
| 0 | 46 | | if (!string.IsNullOrEmpty(wearablesApiData.pagination.next)) |
| | 47 | | { |
| 0 | 48 | | var nextPageResults = GetWearableItems(WearablesFetchingHelper.GetWearablesFetchURL() + wearablesApiData |
| 0 | 49 | | resultList.AddRange(nextPageResults); |
| | 50 | | } |
| | 51 | |
|
| 0 | 52 | | return resultList; |
| | 53 | | } |
| | 54 | | } |
| | 55 | | } |