| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCLServices.EnvironmentProvider; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | | using System; |
| | 7 | | using System.Text; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Networking; |
| | 10 | | using Random = UnityEngine.Random; |
| | 11 | | using Collection = WearableCollectionsAPIData.Collection; |
| | 12 | |
|
| | 13 | | namespace DCL.Helpers |
| | 14 | | { |
| | 15 | | public static class WearablesFetchingHelper |
| | 16 | | { |
| | 17 | | // TODO: change fetching logic to allow for auto-pagination |
| | 18 | | // The https://nft-api.decentraland.org/v1/ endpoint doesn't fetch L1 wearables right now, if those need to be r |
| | 19 | | public const string BASE_FETCH_URL = "https://peer.decentraland.org/lambdas/collections"; |
| | 20 | | public const string COLLECTIONS_FETCH_PARAMS = "?sortBy=newest&first=1000"; |
| | 21 | | public const string WEARABLES_FETCH_PARAMS = "/wearables?"; |
| | 22 | | public const string BASE_WEARABLES_COLLECTION_ID = "urn:decentraland:off-chain:base-avatars"; |
| | 23 | | public const string THIRD_PARTY_COLLECTIONS_FETCH_URL = "third-party-integrations"; |
| | 24 | | public const string NFT_API_FETCH_URL = "https://nft-api.decentraland.org/v1/"; |
| | 25 | | public const string NFT_API_FETCH_PARAMS_ITEMS = "items?"; |
| | 26 | | private static Collection[] collections; |
| | 27 | |
|
| | 28 | | private static IEnumerator EnsureCollectionsData() |
| | 29 | | { |
| 0 | 30 | | if (collections?.Length > 0) |
| 0 | 31 | | yield break; |
| | 32 | |
|
| 0 | 33 | | yield return Environment.i.platform.webRequest.Get( |
| | 34 | | url: GetCollectionsFetchURL(), |
| | 35 | | downloadHandler: new DownloadHandlerBuffer(), |
| | 36 | | timeout: 60, |
| | 37 | | disposeOnCompleted: false, |
| | 38 | | OnFail: (webRequest) => |
| | 39 | | { |
| 0 | 40 | | Debug.LogWarning($"Request error! collections couldn't be fetched! -- {webRequest.webRequest.error}" |
| 0 | 41 | | }, |
| | 42 | | OnSuccess: (webRequest) => |
| | 43 | | { |
| 0 | 44 | | var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(webRequest.webRequest.down |
| 0 | 45 | | collections = collectionsApiData.data; |
| 0 | 46 | | }); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public static async UniTask<string> GetNFTItems(List<string> wearableUrns, IEnvironmentProviderService environme |
| | 50 | | { |
| 0 | 51 | | StringBuilder sb = new (GetItemsFetchURL(environmentProviderService)); |
| | 52 | |
|
| 0 | 53 | | int urnCount = wearableUrns.Count; |
| | 54 | |
|
| 0 | 55 | | for (int i = 0; i < urnCount; i++) |
| | 56 | | { |
| 0 | 57 | | sb.Append("urn="); |
| 0 | 58 | | sb.Append(wearableUrns[i]); |
| | 59 | |
|
| 0 | 60 | | if (i < urnCount - 1) |
| 0 | 61 | | sb.Append("&"); |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | var resultAsync = await Environment.i.platform.webRequest.GetAsync( |
| | 65 | | sb.ToString(), |
| | 66 | | isSigned: true); |
| | 67 | |
|
| 0 | 68 | | return resultAsync.downloadHandler.text;; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public static string GetItemsFetchURL(IEnvironmentProviderService environmentProviderService) |
| | 72 | | { |
| 0 | 73 | | string baseUrl = NFT_API_FETCH_URL; |
| 0 | 74 | | bool useZone = environmentProviderService.IsProd() == false; |
| | 75 | |
|
| 0 | 76 | | if (useZone) |
| 0 | 77 | | baseUrl = baseUrl.Replace("org", "zone"); |
| | 78 | |
|
| 0 | 79 | | return $"{baseUrl}{NFT_API_FETCH_PARAMS_ITEMS}"; |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | public static string GetCollectionsFetchURL() => $"{BASE_FETCH_URL}{COLLECTIONS_FETCH_PARAMS}"; |
| | 83 | |
|
| 0 | 84 | | public static string GetWearablesFetchURL() => $"{BASE_FETCH_URL}{WEARABLES_FETCH_PARAMS}"; |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Fetches base collection ids and adds them to the provided ids list |
| | 88 | | /// </summary> |
| | 89 | | /// <param name="finalCollectionIdsList">A strings list that will be filled with the base collection ids</param> |
| | 90 | | public static IEnumerator GetBaseCollections(List<string> finalCollectionIdsList) |
| | 91 | | { |
| 0 | 92 | | yield return EnsureCollectionsData(); |
| | 93 | |
|
| 0 | 94 | | finalCollectionIdsList.Add( BASE_WEARABLES_COLLECTION_ID ); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public static IEnumerator GetRandomCollections(int amount, List<string> finalCollectionIdsList) |
| | 98 | | { |
| 0 | 99 | | yield return EnsureCollectionsData(); |
| | 100 | |
|
| 0 | 101 | | List<int> randomizedIndices = new List<int>(); |
| | 102 | | int randomIndex; |
| | 103 | |
|
| 0 | 104 | | for (int i = 0; i < amount; i++) |
| | 105 | | { |
| 0 | 106 | | randomIndex = Random.Range(0, collections.Length); |
| | 107 | |
|
| 0 | 108 | | while (randomizedIndices.Contains(randomIndex)) |
| | 109 | | { |
| 0 | 110 | | randomIndex = Random.Range(0, collections.Length); |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | finalCollectionIdsList.Add(collections[randomIndex].urn); |
| 0 | 114 | | randomizedIndices.Add(randomIndex); |
| | 115 | | } |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Given a base url for fetching wearables, this method recursively downloads all the 'pages' responded by the |
| | 120 | | /// and populates the global Catalogue with those wearables. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="url">The API url to fetch the list of wearables</param> |
| | 123 | | /// <param name="finalWearableItemsList">A WearableItems list that will be filled with the fetched wearables</pa |
| | 124 | | public static IEnumerator GetWearableItems(string url, List<WearableItem> finalWearableItemsList) |
| | 125 | | { |
| 0 | 126 | | string nextPageParams = null; |
| | 127 | |
|
| 0 | 128 | | yield return Environment.i.platform.webRequest.Get( |
| | 129 | | url: url, |
| | 130 | | downloadHandler: new DownloadHandlerBuffer(), |
| | 131 | | timeout: 60, |
| | 132 | | disposeOnCompleted: false, |
| | 133 | | OnFail: (webRequest) => |
| | 134 | | { |
| 0 | 135 | | Debug.LogWarning($"Request error! wearables couldn't be fetched! -- {webRequest.webRequest.error}"); |
| 0 | 136 | | }, |
| | 137 | | OnSuccess: (webRequest) => |
| | 138 | | { |
| 0 | 139 | | var wearablesApiData = JsonConvert.DeserializeObject<WearablesAPIData>(webRequest.webRequest.downloa |
| 0 | 140 | | var wearableItemsList = wearablesApiData.GetWearableItems(); |
| 0 | 141 | | finalWearableItemsList.AddRange(wearableItemsList); |
| | 142 | |
|
| 0 | 143 | | nextPageParams = wearablesApiData.pagination.next; |
| 0 | 144 | | }); |
| | 145 | |
|
| 0 | 146 | | if (!string.IsNullOrEmpty(nextPageParams)) |
| | 147 | | { |
| | 148 | | // Since the wearables deployments response returns only a batch of elements, we need to fetch all the |
| | 149 | | // batches sequentially |
| 0 | 150 | | yield return GetWearableItems( |
| | 151 | | $"{GetWearablesFetchURL()}{nextPageParams}", |
| | 152 | | finalWearableItemsList); |
| | 153 | | } |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | [Obsolete("Deprecated. Use IWearablesCatalogService.GetAllThirdPartyCollectionsAsync instead")] |
| | 157 | | public static Promise<Collection[]> GetThirdPartyCollections() |
| | 158 | | { |
| 0 | 159 | | Promise<Collection[]> promiseResult = new Promise<Collection[]>(); |
| | 160 | |
|
| 0 | 161 | | Environment.i.platform.webRequest.Get( |
| | 162 | | url: $"{Environment.i.platform.serviceProviders.catalyst.lambdasUrl}/{THIRD_PARTY_COLLECTIONS_FETCH_URL} |
| | 163 | | downloadHandler: new DownloadHandlerBuffer(), |
| | 164 | | timeout: 60, |
| | 165 | | OnFail: (webRequest) => |
| | 166 | | { |
| 0 | 167 | | promiseResult.Reject($"Request error! third party collections couldn't be fetched! -- {webRequest.we |
| 0 | 168 | | }, |
| | 169 | | OnSuccess: (webRequest) => |
| | 170 | | { |
| 0 | 171 | | var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(webRequest.webRequest.down |
| 0 | 172 | | promiseResult.Resolve(collectionsApiData.data); |
| 0 | 173 | | }); |
| | 174 | |
|
| 0 | 175 | | return promiseResult; |
| | 176 | | } |
| | 177 | | } |
| | 178 | | } |