void IRequestHandler.SetApiResponse(string responseJson, Action onSuccess, Action <string> onError)
        {
            AssetsResponse response = null;

            try
            {
                response = Utils.FromJsonWithNulls <AssetsResponse>(responseJson);
            }
            catch (Exception e)
            {
                onError?.Invoke(e.Message);
            }

            if (response == null)
            {
                return;
            }

            if (VERBOSE)
            {
                Debug.Log($"OwnedNFTRequestHandler: ({GetHashCode()}) {request.requestId} resolved");
            }

            request.Resolve(response);
            onSuccess?.Invoke();
        }
Esempio n. 2
0
        IEnumerator Fetch()
        {
            bool shouldRetry = true;
            int  retryCount  = 0;

            string url = API_URL_ASSETS + assetContractAddress;

            do
            {
                if (OpenSeaRequestController.VERBOSE)
                {
                    Debug.Log($"RequestGroup: Request to OpenSea {url}");
                }

                // NOTE(Santi): In this case, as this code is implementing a very specific retries system (including delays), we use our
                //              custom WebRequest system without retries (requestAttemps = 1) and let the current code to apply the retries.
                WebRequestAsyncOperation requestOp = WebRequestController.i.Get(url: url, requestAttemps: 1, disposeOnCompleted: false);
                yield return(requestOp);

                AssetsResponse response = null;
                if (requestOp.isSucceded)
                {
                    response = Utils.FromJsonWithNulls <AssetsResponse>(requestOp.webRequest.downloadHandler.text);
                }

                if (OpenSeaRequestController.VERBOSE)
                {
                    Debug.Log($"RequestGroup: Request resolving {response != null} {requestOp.webRequest.error} {url}");
                }

                if (response != null)
                {
                    shouldRetry = false;
                    OnSuccess?.Invoke(response);
                    requestOp.Dispose();
                }
                else
                {
                    shouldRetry = retryCount < REQUEST_RETRIES;
                    if (!shouldRetry)
                    {
                        OnError?.Invoke("Error " + requestOp.webRequest.downloadHandler.text);
                        requestOp.Dispose();
                    }
                    else
                    {
                        retryCount++;

                        if (OpenSeaRequestController.VERBOSE)
                        {
                            Debug.Log($"RequestGroup: Request retrying {url}");
                        }

                        yield return(new WaitForSeconds(GetRetryDelay()));

                        requestOp.Dispose();
                    }
                }
            } while (shouldRetry);
        }
        public void Resolve(AssetsResponse response)
        {
            AssetResponse asset = null;

            for (int i = 0; i < response.assets.Length; i++)
            {
                asset = response.assets[i];
                if (asset.token_id == tokenId && String.Equals(asset.asset_contract.address, assetContractAddress, StringComparison.OrdinalIgnoreCase))
                {
                    if (OpenSeaRequestController.VERBOSE)
                    {
                        Debug.Log($"Request: resolved {this.ToString()}");
                    }
                    assetResponse = asset;
                    break;
                }
            }
            if (assetResponse == null)
            {
                error = $"asset {assetContractAddress}/{tokenId} not found in api response";
                if (OpenSeaRequestController.VERBOSE)
                {
                    Debug.Log($"Request: for {assetContractAddress}/{tokenId} not found {JsonUtility.ToJson(response)}");
                }
            }
            resolved = true;
        }
        void IRequestHandler.SetApiResponse(string responseJson, Action onSuccess, Action <string> onError)
        {
            AssetsResponse response = null;

            try
            {
                response = Utils.FromJsonWithNulls <AssetsResponse>(responseJson);
            }
            catch (Exception e)
            {
                onError?.Invoke(e.Message);
            }

            if (response == null)
            {
                return;
            }

            RequestAssetInBatch request = null;
            AssetResponse       asset   = null;

            int batchCount = requests.Count;

            for (int i = batchCount - 1; i >= 0; i--)
            {
                request = requests[i];
                for (int j = 0; j < response.assets.Length; j++)
                {
                    asset = response.assets[j];
                    bool isMatch = asset.token_id == request.tokenId &&
                                   String.Equals(asset.asset_contract.address, request.contractAddress, StringComparison.OrdinalIgnoreCase);

                    if (isMatch)
                    {
                        if (VERBOSE)
                        {
                            Debug.Log($"BatchAssetsRequestHandler: ({GetHashCode()}) resolved {request.contractAddress}/{request.tokenId}");
                        }

                        request.Resolve(asset);
                        requests.RemoveAt(i);
                        break;
                    }
                }
            }

            // Retry possible unresolved requests
            if (requests.Count > 0)
            {
                RetryBatch(requests);
            }

            onSuccess?.Invoke();
        }
        IEnumerator Fetch()
        {
            bool shouldRetry = true;
            int  retryCount  = 0;

            string url = API_URL_ASSETS + assetContractAddress;

            do
            {
                using (UnityWebRequest request = UnityWebRequest.Get(url))
                {
                    if (OpenSeaRequestController.VERBOSE)
                    {
                        Debug.Log($"RequestGroup: Request to OpenSea {url}");
                    }
                    yield return(request.SendWebRequest());

                    AssetsResponse response = null;

                    if (!request.isNetworkError && !request.isHttpError)
                    {
                        response = Utils.FromJsonWithNulls <AssetsResponse>(request.downloadHandler.text);
                    }

                    if (OpenSeaRequestController.VERBOSE)
                    {
                        Debug.Log($"RequestGroup: Request resolving {response != null} {request.error} {url}");
                    }
                    if (response != null)
                    {
                        shouldRetry = false;
                        OnSuccess?.Invoke(response);
                    }
                    else
                    {
                        shouldRetry = retryCount < REQUEST_RETRIES;
                        if (!shouldRetry)
                        {
                            OnError?.Invoke("Error " + request.downloadHandler.text);
                        }
                        else
                        {
                            retryCount++;
                            if (OpenSeaRequestController.VERBOSE)
                            {
                                Debug.Log($"RequestGroup: Request retrying {url}");
                            }
                            yield return(new WaitForSeconds(GetRetryDelay()));
                        }
                    }
                }
            } while (shouldRetry);
        }
        IEnumerator Fetch(float delayRequest)
        {
            yield return(new WaitForSeconds(delayRequest));

            CloseGroup();

            bool shouldRetry = false;
            int  retryCount  = 0;

            string url = API_URL_ASSETS + requestUrl;

            do
            {
                using (UnityWebRequest request = UnityWebRequest.Get(url))
                {
                    if (OpenSeaRequestController.VERBOSE)
                    {
                        Debug.Log($"RequestGroup: Request to OpenSea {url}");
                    }
                    yield return(request.SendWebRequest());

                    AssetsResponse response = null;

                    if (!request.isNetworkError && !request.isHttpError)
                    {
                        response = Utils.FromJsonWithNulls <AssetsResponse>(request.downloadHandler.text);
                    }

                    if (OpenSeaRequestController.VERBOSE)
                    {
                        Debug.Log($"RequestGroup: Request resolving {response != null} {request.error} {url}");
                    }
                    if (response != null)
                    {
                        shouldRetry = false;
                        //if we have one element in the group, is the one failing and we dont group it again
                        if (response.assets.Length != 0 || requests.Count <= 1)
                        {
                            using (var iterator = requests.GetEnumerator())
                            {
                                while (iterator.MoveNext())
                                {
                                    iterator.Current.Value.Resolve(response);
                                }
                            }
                        }
                        else
                        {
                            //There are invalids NFTs to fetch, we split the request into 2 smaller groups to find the ofender
                            SplitGroup();
                        }
                    }
                    else
                    {
                        shouldRetry = retryCount < REQUEST_RETRIES;
                        if (!shouldRetry)
                        {
                            using (var iterator = requests.GetEnumerator())
                            {
                                while (iterator.MoveNext())
                                {
                                    iterator.Current.Value.Resolve(request.error);
                                }
                            }
                        }
                        else
                        {
                            retryCount++;
                            if (OpenSeaRequestController.VERBOSE)
                            {
                                Debug.Log($"RequestGroup: Request retrying {url}");
                            }
                            yield return(new WaitForSeconds(GetRetryDelay()));
                        }
                    }
                }
            } while (shouldRetry);
        }
Esempio n. 7
0
        IEnumerator Fetch(float delayRequest)
        {
            yield return(new WaitForSeconds(delayRequest));

            CloseGroup();

            bool shouldRetry = false;
            int  retryCount  = 0;

            string url = API_URL_ASSETS + requestUrl;

            do
            {
                using (UnityWebRequest request = UnityWebRequest.Get(url))
                {
                    if (OpenSeaRequestController.VERBOSE)
                    {
                        Debug.Log($"RequestGroup: Request to OpenSea {url}");
                    }
                    yield return(request.SendWebRequest());

                    AssetsResponse response = null;

                    if (!request.isNetworkError && !request.isHttpError)
                    {
                        response = Utils.FromJsonWithNulls <AssetsResponse>(request.downloadHandler.text);
                    }

                    if (OpenSeaRequestController.VERBOSE)
                    {
                        Debug.Log($"RequestGroup: Request resolving {response != null} {request.error} {url}");
                    }
                    if (response != null)
                    {
                        shouldRetry = false;
                        using (var iterator = requests.GetEnumerator())
                        {
                            while (iterator.MoveNext())
                            {
                                iterator.Current.Value.Resolve(response);
                            }
                        }
                    }
                    else
                    {
                        shouldRetry = retryCount < REQUEST_RETRIES;
                        if (!shouldRetry)
                        {
                            using (var iterator = requests.GetEnumerator())
                            {
                                while (iterator.MoveNext())
                                {
                                    iterator.Current.Value.Resolve(request.error);
                                }
                            }
                        }
                        else
                        {
                            retryCount++;
                            if (OpenSeaRequestController.VERBOSE)
                            {
                                Debug.Log($"RequestGroup: Request retrying {url}");
                            }
                            yield return(new WaitForSeconds(GetRetryDelay()));
                        }
                    }
                }
            } while (shouldRetry);
        }
Esempio n. 8
0
        IEnumerator Fetch(float delayRequest)
        {
            yield return(new WaitForSeconds(delayRequest));

            CloseGroup();

            bool shouldRetry = false;
            int  retryCount  = 0;

            string url = API_URL_ASSETS + requestUrl;

            do
            {
                if (OpenSeaRequestController.VERBOSE)
                {
                    Debug.Log($"RequestGroup: Request to OpenSea {url}");
                }

                // NOTE(Santi): In this case, as this code is implementing a very specific retries system (including delays), we use our
                //              custom WebRequest system without retries (requestAttemps = 1) and let the current code to apply the retries.
                WebRequestAsyncOperation asyncOp = WebRequestController.i.Get(url: url, requestAttemps: 1, disposeOnCompleted: false);
                yield return(asyncOp);

                AssetsResponse response = null;
                if (asyncOp.isSucceded)
                {
                    response = Utils.FromJsonWithNulls <AssetsResponse>(asyncOp.webRequest.downloadHandler.text);
                }

                if (OpenSeaRequestController.VERBOSE)
                {
                    Debug.Log($"RequestGroup: Request resolving {response != null} {asyncOp.webRequest.error} {url}");
                }

                if (response != null)
                {
                    shouldRetry = false;
                    //if we have one element in the group, is the one failing and we dont group it again
                    if (response.assets.Length != 0 || requests.Count <= 1)
                    {
                        using (var iterator = requests.GetEnumerator())
                        {
                            while (iterator.MoveNext())
                            {
                                iterator.Current.Value.Resolve(response);
                            }
                        }
                    }
                    else
                    {
                        //There are invalids NFTs to fetch, we split the request into 2 smaller groups to find the ofender
                        SplitGroup();
                    }

                    asyncOp.Dispose();
                }
                else
                {
                    shouldRetry = retryCount < REQUEST_RETRIES;
                    if (!shouldRetry)
                    {
                        using (var iterator = requests.GetEnumerator())
                        {
                            while (iterator.MoveNext())
                            {
                                iterator.Current.Value.Resolve(asyncOp.webRequest.error);
                            }
                        }

                        asyncOp.Dispose();
                    }
                    else
                    {
                        retryCount++;

                        if (OpenSeaRequestController.VERBOSE)
                        {
                            Debug.Log($"RequestGroup: Request retrying {url}");
                        }

                        asyncOp.Dispose();

                        yield return(new WaitForSeconds(GetRetryDelay()));
                    }
                }
            } while (shouldRetry);
        }