| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | |
|
| | 6 | | namespace DCL.Helpers.NFT.Markets.OpenSea_Internal |
| | 7 | | { |
| | 8 | | public class RequestController : IDisposable |
| | 9 | | { |
| | 10 | | private const bool VERBOSE = false; |
| | 11 | |
|
| | 12 | | private const string EDITOR_USER_AGENT = |
| | 13 | | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 |
| | 14 | |
|
| | 15 | | private const string EDITOR_REFERRER = "https://play.decentraland.org"; |
| | 16 | |
|
| 72 | 17 | | internal readonly RequestScheduler requestScheduler = new RequestScheduler(); |
| 72 | 18 | | private readonly Dictionary<string, RequestBase<AssetResponse>> cacheAssetResponses = new Dictionary<string, Req |
| 72 | 19 | | private readonly Dictionary<string, RequestBase<AssetsResponse>> cacheSeveralAssetsResponse = new Dictionary<str |
| 72 | 20 | | private readonly Dictionary<string, RequestBase<SingleAssetResponse>> cacheSingleAssetResponses = new Dictionary |
| | 21 | |
|
| | 22 | | private BatchAssetsRequestHandler openBatchAssetsRequestHandler = null; |
| | 23 | |
|
| 216 | 24 | | public RequestController() { requestScheduler.OnRequestReadyToSend += SendRequest; } |
| | 25 | |
|
| 144 | 26 | | public void Dispose() { requestScheduler.OnRequestReadyToSend -= SendRequest; } |
| | 27 | |
|
| | 28 | | public RequestBase<AssetResponse> FetchNFT(string contractAddress, string tokenId) |
| | 29 | | { |
| 0 | 30 | | if (cacheAssetResponses.TryGetValue(RequestAssetInBatch.GetId(contractAddress, tokenId), out RequestBase<Ass |
| | 31 | | { |
| 0 | 32 | | return request; |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | var newRequest = new RequestAssetInBatch(contractAddress, tokenId); |
| | 36 | |
|
| 0 | 37 | | AddToCache(newRequest); |
| | 38 | |
|
| 0 | 39 | | newRequest.OnFail += OnRequestFailed; |
| | 40 | |
|
| 0 | 41 | | if (openBatchAssetsRequestHandler == null || !openBatchAssetsRequestHandler.isOpen) |
| | 42 | | { |
| 0 | 43 | | openBatchAssetsRequestHandler = new BatchAssetsRequestHandler(this); |
| 0 | 44 | | requestScheduler.EnqueueRequest(openBatchAssetsRequestHandler); |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | openBatchAssetsRequestHandler.AddRequest(newRequest); |
| | 48 | |
|
| 0 | 49 | | return newRequest; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public RequestBase<AssetsResponse> FetchOwnedNFT(string address) |
| | 53 | | { |
| 0 | 54 | | if (cacheSeveralAssetsResponse.TryGetValue(RequestOwnedNFTs.GetId(address), out RequestBase<AssetsResponse> |
| | 55 | | { |
| 0 | 56 | | return request; |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | var newRequest = new RequestOwnedNFTs(address); |
| | 60 | |
|
| 0 | 61 | | AddToCache(newRequest); |
| | 62 | |
|
| 0 | 63 | | newRequest.OnFail += OnRequestFailed; |
| | 64 | |
|
| 0 | 65 | | var requestHandler = new OwnedNFTRequestHandler(newRequest, this); |
| 0 | 66 | | requestScheduler.EnqueueRequest(requestHandler); |
| | 67 | |
|
| 0 | 68 | | return newRequest; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public RequestBase<SingleAssetResponse> FetchSingleNFT(string contractAddress, string tokenId) |
| | 72 | | { |
| 0 | 73 | | if (cacheSingleAssetResponses.TryGetValue(RequestAssetSingle.GetId(contractAddress, tokenId), out RequestBas |
| | 74 | | { |
| 0 | 75 | | return request; |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | var newRequest = new RequestAssetSingle(contractAddress, tokenId); |
| | 79 | |
|
| 0 | 80 | | AddToCache(newRequest); |
| | 81 | |
|
| 0 | 82 | | newRequest.OnFail += OnRequestFailed; |
| | 83 | |
|
| 0 | 84 | | var requestHandler = new SingleAssetRequestHandler(newRequest, this); |
| 0 | 85 | | requestScheduler.EnqueueRequest(requestHandler); |
| | 86 | |
|
| 0 | 87 | | return newRequest; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private void SendRequest(IRequestHandler requestHandler) |
| | 91 | | { |
| 0 | 92 | | string url = requestHandler.GetUrl(); |
| | 93 | |
|
| | 94 | | if (VERBOSE) |
| | 95 | | Debug.Log($"RequestController: Send Request: {url}"); |
| | 96 | |
|
| 0 | 97 | | Dictionary<string, string> headers = new Dictionary<string, string>(); |
| | 98 | |
|
| | 99 | | #if (UNITY_EDITOR) || (UNITY_STANDALONE) |
| 0 | 100 | | headers.Add("User-Agent", EDITOR_USER_AGENT); |
| 0 | 101 | | headers.Add("referrer", EDITOR_REFERRER); |
| | 102 | | #endif |
| | 103 | |
|
| | 104 | | // NOTE: In this case, as this code is implementing a very specific retries system (including delays), we us |
| | 105 | | // custom WebRequest system without retries (requestAttemps = 1) and let the current code to ap |
| 0 | 106 | | WebRequestAsyncOperation asyncOp = (WebRequestAsyncOperation) Environment.i.platform.webRequest.Get( |
| | 107 | | url, |
| | 108 | | requestAttemps: 1, |
| | 109 | | disposeOnCompleted: true, |
| | 110 | | headers: headers); |
| | 111 | |
|
| 0 | 112 | | asyncOp.completed += operation => |
| | 113 | | { |
| 0 | 114 | | UnityWebRequest unityWebRequest = operation.webRequest; |
| 0 | 115 | | bool shouldTryToRetry = true; |
| 0 | 116 | | string serverResponse = unityWebRequest.downloadHandler.text; |
| | 117 | |
|
| | 118 | | if (VERBOSE) |
| | 119 | | Debug.Log($"RequestController: Request completed: success? {unityWebRequest.result == UnityWebReques |
| | 120 | |
|
| 0 | 121 | | if (unityWebRequest.result == UnityWebRequest.Result.Success) |
| | 122 | | { |
| 0 | 123 | | requestHandler.SetApiResponse(serverResponse, |
| | 124 | | () => |
| | 125 | | { |
| | 126 | | if (VERBOSE) |
| | 127 | | Debug.Log($"RequestController: Request Succeeded: {url}"); |
| | 128 | |
|
| 0 | 129 | | shouldTryToRetry = false; |
| 0 | 130 | | }, |
| | 131 | | error => |
| | 132 | | { |
| 0 | 133 | | serverResponse = $"RequestController: Parse Request Error: {url}: {error}"; |
| | 134 | |
|
| | 135 | | if (VERBOSE) |
| | 136 | | Debug.Log(serverResponse); |
| 0 | 137 | | }); |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | if (shouldTryToRetry) |
| | 141 | | { |
| 0 | 142 | | if (requestHandler.CanRetry()) |
| | 143 | | { |
| 0 | 144 | | requestHandler.Retry(); |
| 0 | 145 | | } |
| | 146 | | else |
| | 147 | | { |
| 0 | 148 | | requestHandler.SetApiResponseError(serverResponse); |
| | 149 | | } |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | unityWebRequest.Dispose(); |
| 0 | 153 | | }; |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | private void OnRequestFailed(RequestBase<AssetResponse> request) |
| | 157 | | { |
| 0 | 158 | | request.OnFail -= OnRequestFailed; |
| | 159 | |
|
| 0 | 160 | | RemoveFromCache(request); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | private void OnRequestFailed(RequestBase<AssetsResponse> request) |
| | 164 | | { |
| 0 | 165 | | request.OnFail -= OnRequestFailed; |
| | 166 | |
|
| 0 | 167 | | RemoveFromCache(request); |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | private void OnRequestFailed(RequestBase<SingleAssetResponse> request) |
| | 171 | | { |
| 0 | 172 | | request.OnFail -= OnRequestFailed; |
| | 173 | |
|
| 0 | 174 | | RemoveFromCache(request); |
| 0 | 175 | | } |
| | 176 | |
|
| | 177 | | private void AddToCache(RequestBase<AssetResponse> request) |
| | 178 | | { |
| 0 | 179 | | if (cacheAssetResponses.ContainsKey(request.requestId)) |
| 0 | 180 | | return; |
| | 181 | |
|
| 0 | 182 | | cacheAssetResponses.Add(request.requestId, request); |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | private void AddToCache(RequestBase<AssetsResponse> request) |
| | 186 | | { |
| 0 | 187 | | if (cacheSeveralAssetsResponse.ContainsKey(request.requestId)) |
| 0 | 188 | | return; |
| | 189 | |
|
| 0 | 190 | | cacheSeveralAssetsResponse.Add(request.requestId, request); |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | private void AddToCache(RequestBase<SingleAssetResponse> request) |
| | 194 | | { |
| 0 | 195 | | if (cacheSingleAssetResponses.ContainsKey(request.requestId)) |
| 0 | 196 | | return; |
| | 197 | |
|
| 0 | 198 | | cacheSingleAssetResponses.Add(request.requestId, request); |
| 0 | 199 | | } |
| | 200 | |
|
| 0 | 201 | | private void RemoveFromCache(RequestBase<AssetResponse> request) { cacheAssetResponses.Remove(request.requestId) |
| | 202 | |
|
| 0 | 203 | | private void RemoveFromCache(RequestBase<AssetsResponse> request) { cacheSeveralAssetsResponse.Remove(request.re |
| | 204 | |
|
| 0 | 205 | | private void RemoveFromCache(RequestBase<SingleAssetResponse> request) { cacheSingleAssetResponses.Remove(reques |
| | 206 | | } |
| | 207 | | } |