< Summary

Class:DCL.WebRequestController
Assembly:WebRequest
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/WebRequest/WebRequestController.cs
Covered lines:34
Uncovered lines:4
Coverable lines:38
Total lines:192
Line coverage:89.4% (34 of 38)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WebRequestController()0%110100%
Initialize()0%110100%
Create()0%110100%
Initialize(...)0%110100%
Get(...)0%110100%
GetAssetBundle(...)0%2100%
GetAssetBundle(...)0%110100%
GetTexture(...)0%110100%
GetAudioClip(...)0%110100%
SendWebRequest[T](...)0%5.465073.68%
Dispose()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/WebRequest/WebRequestController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.Networking;
 5
 6namespace DCL
 7{
 8    public class WebRequestController : IWebRequestController
 9    {
 10        private IWebRequest genericWebRequest;
 11        private IWebRequestAssetBundle assetBundleWebRequest;
 12        private IWebRequestTexture textureWebRequest;
 13        private IWebRequestAudio audioClipWebRequest;
 74814        private List<WebRequestAsyncOperation> ongoingWebRequests = new List<WebRequestAsyncOperation>();
 15
 16        public void Initialize()
 17        {
 73518        }
 19
 20        public static WebRequestController Create()
 21        {
 74822            WebRequestController newWebRequestController = new WebRequestController();
 23
 74824            newWebRequestController.Initialize(
 25                genericWebRequest: new WebRequest(),
 26                assetBundleWebRequest: new WebRequestAssetBundle(),
 27                textureWebRequest: new WebRequestTexture(),
 28                audioClipWebRequest: new WebRequestAudio());
 29
 74830            return newWebRequestController;
 31        }
 32
 33        public void Initialize(
 34            IWebRequest genericWebRequest,
 35            IWebRequestAssetBundle assetBundleWebRequest,
 36            IWebRequestTexture textureWebRequest,
 37            IWebRequestAudio audioClipWebRequest)
 38        {
 74839            this.genericWebRequest = genericWebRequest;
 74840            this.assetBundleWebRequest = assetBundleWebRequest;
 74841            this.textureWebRequest = textureWebRequest;
 74842            this.audioClipWebRequest = audioClipWebRequest;
 74843        }
 44
 45        public IWebRequestAsyncOperation Get(
 46            string url,
 47            DownloadHandler downloadHandler = null,
 48            Action<IWebRequestAsyncOperation> OnSuccess = null,
 49            Action<IWebRequestAsyncOperation> OnFail = null,
 50            int requestAttemps = 3,
 51            int timeout = 0,
 52            bool disposeOnCompleted = true,
 53            Dictionary<string, string> headers = null)
 54        {
 18055            return SendWebRequest(genericWebRequest, url, downloadHandler, OnSuccess, OnFail, requestAttemps, timeout, d
 56        }
 57
 58        public WebRequestAsyncOperation GetAssetBundle(
 59            string url,
 60            Action<IWebRequestAsyncOperation> OnSuccess = null,
 61            Action<IWebRequestAsyncOperation> OnFail = null,
 62            int requestAttemps = 3,
 63            int timeout = 0,
 64            bool disposeOnCompleted = true)
 65        {
 066            return SendWebRequest(assetBundleWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeO
 67        }
 68
 69        public WebRequestAsyncOperation GetAssetBundle(
 70            string url,
 71            Hash128 hash,
 72            Action<IWebRequestAsyncOperation> OnSuccess = null,
 73            Action<IWebRequestAsyncOperation> OnFail = null,
 74            int requestAttemps = 3,
 75            int timeout = 0,
 76            bool disposeOnCompleted = true)
 77        {
 2778            assetBundleWebRequest.SetHash(hash);
 2779            return SendWebRequest(assetBundleWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeO
 80        }
 81
 82        public WebRequestAsyncOperation GetTexture(
 83            string url,
 84            Action<IWebRequestAsyncOperation> OnSuccess = null,
 85            Action<IWebRequestAsyncOperation> OnFail = null,
 86            int requestAttemps = 3,
 87            int timeout = 0,
 88            bool disposeOnCompleted = true,
 89            bool isReadable = true)
 90        {
 10891            textureWebRequest.isReadable = isReadable;
 10892            return SendWebRequest(textureWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnCom
 93        }
 94
 95        public WebRequestAsyncOperation GetAudioClip(
 96            string url,
 97            AudioType audioType,
 98            Action<IWebRequestAsyncOperation> OnSuccess = null,
 99            Action<IWebRequestAsyncOperation> OnFail = null,
 100            int requestAttemps = 3,
 101            int timeout = 0,
 102            bool disposeOnCompleted = true)
 103        {
 26104            audioClipWebRequest.SetAudioType(audioType);
 26105            return SendWebRequest(audioClipWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnC
 106        }
 107
 108        private WebRequestAsyncOperation SendWebRequest<T>(
 109            T requestType,
 110            string url,
 111            DownloadHandler downloadHandler,
 112            Action<IWebRequestAsyncOperation> OnSuccess,
 113            Action<IWebRequestAsyncOperation> OnFail,
 114            int requestAttemps,
 115            int timeout,
 116            bool disposeOnCompleted,
 117            Dictionary<string, string> headers = null,
 118            WebRequestAsyncOperation asyncOp = null
 119        ) where T : IWebRequest
 120        {
 341121            int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps);
 122
 341123            UnityWebRequest request = requestType.CreateWebRequest(url);
 341124            request.timeout = timeout;
 341125            if (headers != null)
 126            {
 0127                foreach (var item in headers)
 128                {
 0129                    request.SetRequestHeader(item.Key, item.Value);
 130                }
 131            }
 132
 341133            if (downloadHandler != null)
 167134                request.downloadHandler = downloadHandler;
 135
 341136            WebRequestAsyncOperation resultOp = asyncOp;
 137
 341138            if (resultOp == null)
 341139                resultOp = new WebRequestAsyncOperation(request);
 140            else
 0141                resultOp.SetNewWebRequest(request);
 142
 341143            resultOp.disposeOnCompleted = disposeOnCompleted;
 341144            ongoingWebRequests.Add(resultOp);
 145
 341146            UnityWebRequestAsyncOperation requestOp = resultOp.SendWebRequest();
 341147            requestOp.completed += (asyncOp) =>
 148            {
 149                if (!resultOp.isDisposed)
 150                {
 151                    if (resultOp.webRequest.WebRequestSucceded())
 152                    {
 153                        OnSuccess?.Invoke(resultOp);
 154                        resultOp.SetAsCompleted(true);
 155                    }
 156                    else if (!resultOp.webRequest.WebRequestAborted() && resultOp.webRequest.WebRequestServerError())
 157                    {
 158                        remainingAttemps--;
 159                        if (remainingAttemps > 0)
 160                        {
 161                            Debug.LogWarning($"Retrying web request: {url} ({remainingAttemps} attemps remaining)");
 162                            resultOp.Dispose();
 163                            resultOp = SendWebRequest(requestType, url, downloadHandler, OnSuccess, OnFail, remainingAtt
 164                        }
 165                        else
 166                        {
 167                            OnFail?.Invoke(resultOp);
 168                            resultOp.SetAsCompleted(false);
 169                        }
 170                    }
 171                    else
 172                    {
 173                        OnFail?.Invoke(resultOp);
 174                        resultOp.SetAsCompleted(false);
 175                    }
 176                }
 177
 178                ongoingWebRequests.Remove(resultOp);
 179            };
 180
 341181            return resultOp;
 182        }
 183
 184        public void Dispose()
 185        {
 1562186            foreach (var webRequest in ongoingWebRequests)
 187            {
 26188                webRequest.Dispose();
 189            }
 755190        }
 191    }
 192}