| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Helpers.NFT.Markets.OpenSea_Internal |
| | 8 | | { |
| | 9 | | internal class BatchAssetsRequestHandler : IRequestHandler |
| | 10 | | { |
| | 11 | | const bool VERBOSE = false; |
| | 12 | | const float BATCH_OPEN_TIME = 2; |
| | 13 | | const float QUERY_MAX_LENGTH = 1085; // maxUrl(1279) - apiUrl(37) - longestPossibleRequest (78 tokenId + 42 cont |
| | 14 | |
|
| 0 | 15 | | public bool isOpen { private set; get; } |
| | 16 | |
|
| 0 | 17 | | SchedulableRequestHandler IRequestHandler.schedulableRequestHandler => schedulableHandler; |
| | 18 | |
|
| 0 | 19 | | private readonly List<RequestAssetInBatch> requests = new List<RequestAssetInBatch>(); |
| | 20 | | private readonly RequestController requestController; |
| 0 | 21 | | private readonly SchedulableRequestHandler schedulableHandler = new SchedulableRequestHandler(); |
| | 22 | |
|
| 0 | 23 | | private string query = string.Empty; |
| | 24 | | private int retryCount = 0; |
| | 25 | |
|
| 0 | 26 | | public BatchAssetsRequestHandler(RequestController requestController, bool autocloseBatch = true) |
| | 27 | | { |
| | 28 | | if (VERBOSE) |
| | 29 | | Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) created"); |
| | 30 | |
|
| 0 | 31 | | this.requestController = requestController; |
| 0 | 32 | | this.isOpen = true; |
| | 33 | |
|
| 0 | 34 | | if (autocloseBatch) |
| 0 | 35 | | CoroutineStarter.Start(BatchOpenTimeOut(BATCH_OPEN_TIME)); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void AddRequest(RequestAssetInBatch request) |
| | 39 | | { |
| 0 | 40 | | requests.Add(request); |
| 0 | 41 | | query += $"asset_contract_addresses={request.contractAddress}&token_ids={request.tokenId}&"; |
| | 42 | |
|
| | 43 | | if (VERBOSE) |
| | 44 | | Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) adding {request.contractAddress}/{request.token |
| | 45 | |
|
| 0 | 46 | | if (query.Length >= QUERY_MAX_LENGTH) |
| | 47 | | { |
| 0 | 48 | | CloseBatch(); |
| | 49 | | } |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | void CloseBatch() |
| | 53 | | { |
| | 54 | | if (VERBOSE) |
| | 55 | | Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) closed {query}"); |
| | 56 | |
|
| 0 | 57 | | isOpen = false; |
| | 58 | |
|
| 0 | 59 | | if (string.IsNullOrEmpty(query)) |
| 0 | 60 | | return; |
| | 61 | |
|
| 0 | 62 | | schedulableHandler.SetReadyToBeScheduled(this); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | IEnumerator BatchOpenTimeOut(float openTime) |
| | 66 | | { |
| 0 | 67 | | yield return WaitForSecondsCache.Get(openTime); |
| 0 | 68 | | CloseBatch(); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | void TryRetrySingleAsset() |
| | 72 | | { |
| | 73 | | if (VERBOSE) |
| | 74 | | Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) retry single asset {query}"); |
| | 75 | |
|
| 0 | 76 | | requestController.requestScheduler.EnqueueRequest(this); |
| 0 | 77 | | retryCount ++; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | void TryRetryBatchAssets() |
| | 81 | | { |
| | 82 | | if (VERBOSE) |
| | 83 | | Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) retry splitting batch {query}"); |
| | 84 | |
|
| | 85 | | // Split requests in new batches and retry |
| 0 | 86 | | int counter = 0; |
| 0 | 87 | | var batches = requests.GroupBy(x => counter++ % 2); |
| | 88 | |
|
| 0 | 89 | | foreach (var group in batches) |
| | 90 | | { |
| 0 | 91 | | RetryBatch(group.ToList()); |
| | 92 | | } |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | void RetryBatch(List<RequestAssetInBatch> batchedRequests) |
| | 96 | | { |
| 0 | 97 | | var newHandler = new BatchAssetsRequestHandler(requestController, false); |
| 0 | 98 | | for (int i = 0; i < batchedRequests.Count; i++) |
| | 99 | | { |
| 0 | 100 | | newHandler.AddRequest(batchedRequests[i]); |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | requestController.requestScheduler.EnqueueRequest(newHandler); |
| 0 | 104 | | newHandler.schedulableHandler.SetReadyToBeScheduled(newHandler); |
| 0 | 105 | | } |
| | 106 | |
|
| 0 | 107 | | string IRequestHandler.GetUrl() { return $"{Constants.MULTIPLE_ASSETS_URL}?{query}"; } |
| | 108 | |
|
| | 109 | | void IRequestHandler.SetApiResponse(string responseJson, Action onSuccess, Action<string> onError) |
| | 110 | | { |
| 0 | 111 | | AssetsResponse response = null; |
| | 112 | |
|
| | 113 | | try |
| | 114 | | { |
| 0 | 115 | | response = Utils.FromJsonWithNulls<AssetsResponse>(responseJson); |
| 0 | 116 | | } |
| 0 | 117 | | catch (Exception e) |
| | 118 | | { |
| 0 | 119 | | onError?.Invoke(e.Message); |
| 0 | 120 | | } |
| | 121 | |
|
| 0 | 122 | | if (response == null) |
| 0 | 123 | | return; |
| | 124 | |
|
| 0 | 125 | | RequestAssetInBatch request = null; |
| 0 | 126 | | AssetResponse asset = null; |
| | 127 | |
|
| 0 | 128 | | int batchCount = requests.Count; |
| 0 | 129 | | for (int i = batchCount - 1; i >= 0; i--) |
| | 130 | | { |
| 0 | 131 | | request = requests[i]; |
| 0 | 132 | | for (int j = 0; j < response.assets.Length; j++) |
| | 133 | | { |
| 0 | 134 | | asset = response.assets[j]; |
| 0 | 135 | | bool isMatch = asset.token_id == request.tokenId |
| | 136 | | && String.Equals(asset.asset_contract.address, request.contractAddress, StringCompari |
| | 137 | |
|
| 0 | 138 | | if (isMatch) |
| | 139 | | { |
| | 140 | | if (VERBOSE) |
| | 141 | | Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) resolved {request.contractAddress}/ |
| | 142 | |
|
| 0 | 143 | | request.Resolve(asset); |
| 0 | 144 | | requests.RemoveAt(i); |
| 0 | 145 | | break; |
| | 146 | | } |
| | 147 | | } |
| | 148 | | } |
| | 149 | |
|
| | 150 | | // Retry possible unresolved requests |
| 0 | 151 | | if (requests.Count > 0) |
| | 152 | | { |
| 0 | 153 | | RetryBatch(requests); |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | onSuccess?.Invoke(); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | bool IRequestHandler.CanRetry() |
| | 160 | | { |
| 0 | 161 | | if (requests.Count == 1) |
| 0 | 162 | | return retryCount < Constants.REQUESTS_RETRY_ATTEMPS; |
| 0 | 163 | | return true; |
| | 164 | | } |
| | 165 | |
|
| | 166 | | void IRequestHandler.Retry() |
| | 167 | | { |
| 0 | 168 | | if (requests.Count == 0) |
| 0 | 169 | | return; |
| | 170 | |
|
| 0 | 171 | | if (requests.Count == 1) |
| | 172 | | { |
| 0 | 173 | | TryRetrySingleAsset(); |
| 0 | 174 | | } |
| | 175 | | else |
| | 176 | | { |
| 0 | 177 | | TryRetryBatchAssets(); |
| | 178 | | } |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | void IRequestHandler.SetApiResponseError(string error) |
| | 182 | | { |
| | 183 | | if (VERBOSE) |
| | 184 | | Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) rejecting {error} {query}"); |
| | 185 | |
|
| 0 | 186 | | for (int i = 0; i < requests.Count; i++) |
| | 187 | | { |
| 0 | 188 | | requests[i].Reject(error); |
| | 189 | | } |
| 0 | 190 | | } |
| | 191 | | } |
| | 192 | | } |