< Summary

Class:DCL.Helpers.NFT.Markets.OpenSea_Internal.RequestController
Assembly:OpenSea_Internal
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/OpenSea/RequestController.cs
Covered lines:6
Uncovered lines:73
Coverable lines:79
Total lines:207
Line coverage:7.5% (6 of 79)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RequestController()0%110100%
Dispose()0%110100%
FetchNFT(...)0%20400%
FetchOwnedNFT(...)0%6200%
FetchSingleNFT(...)0%6200%
SendRequest(...)0%2100%
OnRequestFailed(...)0%2100%
OnRequestFailed(...)0%2100%
OnRequestFailed(...)0%2100%
AddToCache(...)0%6200%
AddToCache(...)0%6200%
AddToCache(...)0%6200%
RemoveFromCache(...)0%2100%
RemoveFromCache(...)0%2100%
RemoveFromCache(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/OpenSea/RequestController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.Networking;
 5
 6namespace 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
 7217        internal readonly RequestScheduler requestScheduler = new RequestScheduler();
 7218        private readonly Dictionary<string, RequestBase<AssetResponse>> cacheAssetResponses = new Dictionary<string, Req
 7219        private readonly Dictionary<string, RequestBase<AssetsResponse>> cacheSeveralAssetsResponse = new Dictionary<str
 7220        private readonly Dictionary<string, RequestBase<SingleAssetResponse>> cacheSingleAssetResponses = new Dictionary
 21
 22        private BatchAssetsRequestHandler openBatchAssetsRequestHandler = null;
 23
 21624        public RequestController() { requestScheduler.OnRequestReadyToSend += SendRequest; }
 25
 14426        public void Dispose() { requestScheduler.OnRequestReadyToSend -= SendRequest; }
 27
 28        public RequestBase<AssetResponse> FetchNFT(string contractAddress, string tokenId)
 29        {
 030            if (cacheAssetResponses.TryGetValue(RequestAssetInBatch.GetId(contractAddress, tokenId), out RequestBase<Ass
 31            {
 032                return request;
 33            }
 34
 035            var newRequest = new RequestAssetInBatch(contractAddress, tokenId);
 36
 037            AddToCache(newRequest);
 38
 039            newRequest.OnFail += OnRequestFailed;
 40
 041            if (openBatchAssetsRequestHandler == null || !openBatchAssetsRequestHandler.isOpen)
 42            {
 043                openBatchAssetsRequestHandler = new BatchAssetsRequestHandler(this);
 044                requestScheduler.EnqueueRequest(openBatchAssetsRequestHandler);
 45            }
 46
 047            openBatchAssetsRequestHandler.AddRequest(newRequest);
 48
 049            return newRequest;
 50        }
 51
 52        public RequestBase<AssetsResponse> FetchOwnedNFT(string address)
 53        {
 054            if (cacheSeveralAssetsResponse.TryGetValue(RequestOwnedNFTs.GetId(address), out RequestBase<AssetsResponse> 
 55            {
 056                return request;
 57            }
 58
 059            var newRequest = new RequestOwnedNFTs(address);
 60
 061            AddToCache(newRequest);
 62
 063            newRequest.OnFail += OnRequestFailed;
 64
 065            var requestHandler = new OwnedNFTRequestHandler(newRequest, this);
 066            requestScheduler.EnqueueRequest(requestHandler);
 67
 068            return newRequest;
 69        }
 70
 71        public RequestBase<SingleAssetResponse> FetchSingleNFT(string contractAddress, string tokenId)
 72        {
 073            if (cacheSingleAssetResponses.TryGetValue(RequestAssetSingle.GetId(contractAddress, tokenId), out RequestBas
 74            {
 075                return request;
 76            }
 77
 078            var newRequest = new RequestAssetSingle(contractAddress, tokenId);
 79
 080            AddToCache(newRequest);
 81
 082            newRequest.OnFail += OnRequestFailed;
 83
 084            var requestHandler = new SingleAssetRequestHandler(newRequest, this);
 085            requestScheduler.EnqueueRequest(requestHandler);
 86
 087            return newRequest;
 88        }
 89
 90        private void SendRequest(IRequestHandler requestHandler)
 91        {
 092            string url = requestHandler.GetUrl();
 93
 94            if (VERBOSE)
 95                Debug.Log($"RequestController: Send Request: {url}");
 96
 097            Dictionary<string, string> headers = new Dictionary<string, string>();
 98
 99#if (UNITY_EDITOR) || (UNITY_STANDALONE)
 0100            headers.Add("User-Agent", EDITOR_USER_AGENT);
 0101            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
 0106            WebRequestAsyncOperation asyncOp = (WebRequestAsyncOperation) Environment.i.platform.webRequest.Get(
 107                url,
 108                requestAttemps: 1,
 109                disposeOnCompleted: true,
 110                headers: headers);
 111
 0112            asyncOp.completed += operation =>
 113            {
 0114                UnityWebRequest unityWebRequest = operation.webRequest;
 0115                bool shouldTryToRetry = true;
 0116                string serverResponse = unityWebRequest.downloadHandler.text;
 117
 118                if (VERBOSE)
 119                    Debug.Log($"RequestController: Request completed: success? {unityWebRequest.result == UnityWebReques
 120
 0121                if (unityWebRequest.result == UnityWebRequest.Result.Success)
 122                {
 0123                    requestHandler.SetApiResponse(serverResponse,
 124                        () =>
 125                        {
 126                            if (VERBOSE)
 127                                Debug.Log($"RequestController: Request Succeeded: {url}");
 128
 0129                            shouldTryToRetry = false;
 0130                        },
 131                        error =>
 132                        {
 0133                            serverResponse = $"RequestController: Parse Request Error: {url}: {error}";
 134
 135                            if (VERBOSE)
 136                                Debug.Log(serverResponse);
 0137                        });
 138                }
 139
 0140                if (shouldTryToRetry)
 141                {
 0142                    if (requestHandler.CanRetry())
 143                    {
 0144                        requestHandler.Retry();
 0145                    }
 146                    else
 147                    {
 0148                        requestHandler.SetApiResponseError(serverResponse);
 149                    }
 150                }
 151
 0152                unityWebRequest.Dispose();
 0153            };
 0154        }
 155
 156        private void OnRequestFailed(RequestBase<AssetResponse> request)
 157        {
 0158            request.OnFail -= OnRequestFailed;
 159
 0160            RemoveFromCache(request);
 0161        }
 162
 163        private void OnRequestFailed(RequestBase<AssetsResponse> request)
 164        {
 0165            request.OnFail -= OnRequestFailed;
 166
 0167            RemoveFromCache(request);
 0168        }
 169
 170        private void OnRequestFailed(RequestBase<SingleAssetResponse> request)
 171        {
 0172            request.OnFail -= OnRequestFailed;
 173
 0174            RemoveFromCache(request);
 0175        }
 176
 177        private void AddToCache(RequestBase<AssetResponse> request)
 178        {
 0179            if (cacheAssetResponses.ContainsKey(request.requestId))
 0180                return;
 181
 0182            cacheAssetResponses.Add(request.requestId, request);
 0183        }
 184
 185        private void AddToCache(RequestBase<AssetsResponse> request)
 186        {
 0187            if (cacheSeveralAssetsResponse.ContainsKey(request.requestId))
 0188                return;
 189
 0190            cacheSeveralAssetsResponse.Add(request.requestId, request);
 0191        }
 192
 193        private void AddToCache(RequestBase<SingleAssetResponse> request)
 194        {
 0195            if (cacheSingleAssetResponses.ContainsKey(request.requestId))
 0196                return;
 197
 0198            cacheSingleAssetResponses.Add(request.requestId, request);
 0199        }
 200
 0201        private void RemoveFromCache(RequestBase<AssetResponse> request) { cacheAssetResponses.Remove(request.requestId)
 202
 0203        private void RemoveFromCache(RequestBase<AssetsResponse> request) { cacheSeveralAssetsResponse.Remove(request.re
 204
 0205        private void RemoveFromCache(RequestBase<SingleAssetResponse> request) { cacheSingleAssetResponses.Remove(reques
 206    }
 207}