< 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:28
Coverable lines:62
Total lines:370
Line coverage:54.8% (34 of 62)
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%2100%
GetAsync()0%12300%
PostAsync()0%12300%
GetAssetBundleAsync()0%12300%
GetAssetBundleAsync()0%12300%
GetTextureAsync()0%12300%
GetAudioClipAsync()0%12300%
Get(...)0%110100%
Post(...)0%2100%
GetAssetBundle(...)0%110100%
GetAssetBundle(...)0%2100%
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 Cysharp.Threading.Tasks;
 2using System;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine;
 6using UnityEngine.Networking;
 7
 8namespace DCL
 9{
 10    public class WebRequestController : IWebRequestController
 11    {
 12        private IWebRequestFactory getWebRequestFactory;
 13        private IWebRequestAssetBundleFactory assetBundleFactory;
 14        private IWebRequestTextureFactory textureFactory;
 15        private IWebRequestAudioFactory audioClipWebRequestFactory;
 16        private IPostWebRequestFactory postWebRequestFactory;
 17
 62918        private readonly List<WebRequestAsyncOperation> ongoingWebRequests = new();
 19
 62920        private WebRequestController(
 21            IWebRequestFactory getWebRequestFactory,
 22            IWebRequestAssetBundleFactory assetBundleFactory,
 23            IWebRequestTextureFactory textureFactory,
 24            IWebRequestAudioFactory audioClipWebRequestFactory,
 25            IPostWebRequestFactory postWebRequestFactory
 26        )
 27        {
 62928            this.getWebRequestFactory = getWebRequestFactory;
 62929            this.assetBundleFactory = assetBundleFactory;
 62930            this.textureFactory = textureFactory;
 62931            this.audioClipWebRequestFactory = audioClipWebRequestFactory;
 62932            this.postWebRequestFactory = postWebRequestFactory;
 62933        }
 34
 61335        public void Initialize() { }
 36
 37        public static WebRequestController Create()
 38        {
 62939            WebRequestController newWebRequestController = new WebRequestController(
 40                new GetWebRequestFactory(),
 41                new WebRequestAssetBundleFactory(),
 42                new WebRequestTextureFactory(),
 43                new WebRequestAudioFactory(),
 44                new PostWebRequestFactory()
 45            );
 46
 62947            return newWebRequestController;
 48        }
 49
 50        public void Initialize(
 51            IWebRequestFactory genericWebRequest,
 52            IWebRequestAssetBundleFactory assetBundleFactoryWebRequest,
 53            IWebRequestTextureFactory textureFactoryWebRequest,
 54            IWebRequestAudioFactory audioClipWebRequest)
 55        {
 056            getWebRequestFactory = genericWebRequest;
 057            assetBundleFactory = assetBundleFactoryWebRequest;
 058            textureFactory = textureFactoryWebRequest;
 059            audioClipWebRequestFactory = audioClipWebRequest;
 060        }
 61
 62        public async UniTask<UnityWebRequest> GetAsync(
 63            string url,
 64            DownloadHandler downloadHandler = null,
 65            Action<UnityWebRequest> onSuccess = null,
 66            Action<UnityWebRequest> onfail = null,
 67            int requestAttemps = 3,
 68            int timeout = 0,
 69            CancellationToken cancellationToken = default,
 70            Dictionary<string, string> headers = null)
 71        {
 072            return await SendWebRequest(getWebRequestFactory, url, downloadHandler, onSuccess, onfail, requestAttemps,
 73                timeout, cancellationToken, headers);
 074        }
 75
 76        public async UniTask<UnityWebRequest> PostAsync(
 77            string url,
 78            string postData,
 79            DownloadHandler downloadHandler = null,
 80            Action<UnityWebRequest> onSuccess = null,
 81            Action<UnityWebRequest> onfail = null,
 82            int requestAttemps = 3,
 83            int timeout = 0,
 84            CancellationToken cancellationToken = default,
 85            Dictionary<string, string> headers = null)
 86        {
 087            postWebRequestFactory.SetBody(postData);
 088            return await SendWebRequest(postWebRequestFactory, url, downloadHandler, onSuccess, onfail, requestAttemps,
 89                timeout, cancellationToken, headers);
 090        }
 91
 92        public async UniTask<UnityWebRequest> GetAssetBundleAsync(
 93            string url,
 94            Action<UnityWebRequest> onSuccess = null,
 95            Action<UnityWebRequest> onfail = null,
 96            int requestAttemps = 3,
 97            int timeout = 0,
 98            CancellationToken cancellationToken = default)
 99        {
 0100            return await SendWebRequest(assetBundleFactory, url, null, onSuccess, onfail, requestAttemps,
 101                timeout, cancellationToken);
 0102        }
 103
 104        public async UniTask<UnityWebRequest> GetAssetBundleAsync(
 105            string url,
 106            Hash128 hash,
 107            Action<UnityWebRequest> onSuccess = null,
 108            Action<UnityWebRequest> onfail = null,
 109            int requestAttemps = 3,
 110            int timeout = 0,
 111            CancellationToken cancellationToken = default)
 112        {
 0113            assetBundleFactory.SetHash(hash);
 0114            return await SendWebRequest(assetBundleFactory, url, null, onSuccess, onfail, requestAttemps,
 115                timeout, cancellationToken);
 0116        }
 117
 118        public async UniTask<UnityWebRequest> GetTextureAsync(
 119            string url,
 120            Action<UnityWebRequest> onSuccess = null,
 121            Action<UnityWebRequest> onfail = null,
 122            int requestAttemps = 3,
 123            int timeout = 0,
 124            bool isReadable = true,
 125            CancellationToken cancellationToken = default,
 126            Dictionary<string, string> headers = null)
 127        {
 0128            textureFactory.isReadable = isReadable;
 0129            return await SendWebRequest(textureFactory, url, null, onSuccess, onfail, requestAttemps,
 130                timeout, cancellationToken, headers);
 0131        }
 132
 133        public async UniTask<UnityWebRequest> GetAudioClipAsync(
 134            string url,
 135            AudioType audioType,
 136            Action<UnityWebRequest> onSuccess = null,
 137            Action<UnityWebRequest> onfail = null,
 138            int requestAttemps = 3,
 139            int timeout = 0,
 140            CancellationToken cancellationToken = default)
 141        {
 0142            audioClipWebRequestFactory.SetAudioType(audioType);
 0143            return await SendWebRequest(audioClipWebRequestFactory, url, null, onSuccess, onfail, requestAttemps,
 144                timeout, cancellationToken);
 0145        }
 146
 147
 148        private async UniTask<UnityWebRequest> SendWebRequest<T>(
 149            T requestFactory,
 150            string url,
 151            DownloadHandler downloadHandler,
 152            Action<UnityWebRequest> onSuccess,
 153            Action<UnityWebRequest> onFail,
 154            int requestAttemps,
 155            int timeout,
 156            CancellationToken cancellationToken,
 157            Dictionary<string, string> headers = null) where T : IWebRequestFactory
 158        {
 159            requestAttemps = Mathf.Max(1, requestAttemps);
 160            for (int i = requestAttemps - 1; i >= 0; i--)
 161            {
 162                UnityWebRequest request = requestFactory.CreateWebRequest(url);
 163                request.timeout = timeout;
 164
 165                if (headers != null)
 166                {
 167                    foreach (var item in headers)
 168                        request.SetRequestHeader(item.Key, item.Value);
 169                }
 170
 171                if (downloadHandler != null)
 172                    request.downloadHandler = downloadHandler;
 173
 174                UnityWebRequestAsyncOperation asyncOp = request.SendWebRequest();
 175                await asyncOp.WithCancellation(cancellationToken);
 176
 177                if (request.WebRequestSucceded())
 178                {
 179                    onSuccess?.Invoke(asyncOp.webRequest);
 180                    return asyncOp.webRequest;
 181                }
 182                else if (!request.WebRequestAborted() && request.WebRequestServerError())
 183                {
 184                    if (requestAttemps > 0)
 185                        continue;
 186                    else
 187                    {
 188                        onFail?.Invoke(asyncOp.webRequest);
 189                        return asyncOp.webRequest;
 190                    }
 191                }
 192                else
 193                {
 194                    onFail?.Invoke(asyncOp.webRequest);
 195                    return asyncOp.webRequest;
 196                }
 197            }
 198
 199            throw new Exception("WebRequestController SendWebRequest: Unexpected error");
 200        }
 201
 202        [Obsolete]
 203        public IWebRequestAsyncOperation Get(
 204            string url,
 205            DownloadHandler downloadHandler = null,
 206            Action<IWebRequestAsyncOperation> OnSuccess = null,
 207            Action<IWebRequestAsyncOperation> OnFail = null,
 208            int requestAttemps = 3,
 209            int timeout = 0,
 210            bool disposeOnCompleted = true,
 211            Dictionary<string, string> headers = null)
 212        {
 176213            return SendWebRequest(getWebRequestFactory, url, downloadHandler, OnSuccess, OnFail, requestAttemps, timeout
 214        }
 215
 216        [Obsolete]
 217        public IWebRequestAsyncOperation Post(
 218            string url,
 219            string postData,
 220            DownloadHandler downloadHandler = null,
 221            Action<IWebRequestAsyncOperation> OnSuccess = null,
 222            Action<IWebRequestAsyncOperation> OnFail = null,
 223            int requestAttemps = 3,
 224            int timeout = 0,
 225            bool disposeOnCompleted = true,
 226            Dictionary<string, string> headers = null)
 227        {
 0228            postWebRequestFactory.SetBody(postData);
 0229            return SendWebRequest(postWebRequestFactory, url, downloadHandler, OnSuccess, OnFail, requestAttemps, timeou
 230        }
 231
 232        [Obsolete]
 233        public IWebRequestAsyncOperation GetAssetBundle(
 234            string url,
 235            Action<IWebRequestAsyncOperation> OnSuccess = null,
 236            Action<IWebRequestAsyncOperation> OnFail = null,
 237            int requestAttemps = 3,
 238            int timeout = 0,
 239            bool disposeOnCompleted = true)
 240        {
 29241            return SendWebRequest(assetBundleFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnCo
 242        }
 243
 244        [Obsolete]
 245        public IWebRequestAsyncOperation GetAssetBundle(
 246            string url,
 247            Hash128 hash,
 248            Action<IWebRequestAsyncOperation> OnSuccess = null,
 249            Action<IWebRequestAsyncOperation> OnFail = null,
 250            int requestAttemps = 3,
 251            int timeout = 0,
 252            bool disposeOnCompleted = true)
 253        {
 0254            assetBundleFactory.SetHash(hash);
 0255            return SendWebRequest(assetBundleFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnCo
 256        }
 257
 258        [Obsolete]
 259        public IWebRequestAsyncOperation GetTexture(
 260            string url,
 261            Action<IWebRequestAsyncOperation> OnSuccess = null,
 262            Action<IWebRequestAsyncOperation> OnFail = null,
 263            int requestAttemps = 3,
 264            int timeout = 0,
 265            bool disposeOnCompleted = true,
 266            bool isReadable = true,
 267            Dictionary<string, string> headers = null)
 268        {
 229269            textureFactory.isReadable = isReadable;
 229270            return SendWebRequest(textureFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnComple
 271        }
 272
 273        [Obsolete]
 274        public IWebRequestAsyncOperation GetAudioClip(
 275            string url,
 276            AudioType audioType,
 277            Action<IWebRequestAsyncOperation> OnSuccess = null,
 278            Action<IWebRequestAsyncOperation> OnFail = null,
 279            int requestAttemps = 3,
 280            int timeout = 0,
 281            bool disposeOnCompleted = true)
 282        {
 38283            audioClipWebRequestFactory.SetAudioType(audioType);
 38284            return SendWebRequest(audioClipWebRequestFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, dis
 285        }
 286
 287        private WebRequestAsyncOperation SendWebRequest<T>(
 288            T requestFactory,
 289            string url,
 290            DownloadHandler downloadHandler,
 291            Action<IWebRequestAsyncOperation> OnSuccess,
 292            Action<IWebRequestAsyncOperation> OnFail,
 293            int requestAttemps,
 294            int timeout,
 295            bool disposeOnCompleted,
 296            Dictionary<string, string> headers = null,
 297            WebRequestAsyncOperation asyncOp = null
 298        ) where T : IWebRequestFactory
 299        {
 472300            int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps);
 301
 472302            UnityWebRequest request = requestFactory.CreateWebRequest(url);
 472303            request.timeout = timeout;
 304
 472305            if (headers != null)
 306            {
 0307                foreach (var item in headers)
 0308                { request.SetRequestHeader(item.Key, item.Value); }
 309            }
 310
 472311            if (downloadHandler != null)
 163312                request.downloadHandler = downloadHandler;
 313
 472314            WebRequestAsyncOperation resultOp = asyncOp;
 315
 472316            if (resultOp == null)
 472317                resultOp = new WebRequestAsyncOperation(request);
 318            else
 0319                resultOp.SetNewWebRequest(request);
 320
 472321            resultOp.disposeOnCompleted = disposeOnCompleted;
 472322            ongoingWebRequests.Add(resultOp);
 323
 472324            UnityWebRequestAsyncOperation requestOp = resultOp.SendWebRequest();
 325
 472326            requestOp.completed += (asyncOp) =>
 327            {
 328                if (!resultOp.isDisposed)
 329                {
 330                    if (resultOp.webRequest.WebRequestSucceded())
 331                    {
 332                        OnSuccess?.Invoke(resultOp);
 333                        resultOp.SetAsCompleted(true);
 334                    }
 335                    else if (!resultOp.webRequest.WebRequestAborted() && resultOp.webRequest.WebRequestServerError())
 336                    {
 337                        remainingAttemps--;
 338
 339                        if (remainingAttemps > 0)
 340                        {
 341                            resultOp.Dispose();
 342                            resultOp = SendWebRequest(requestFactory, url, downloadHandler, OnSuccess, OnFail, remaining
 343                        }
 344                        else
 345                        {
 346                            OnFail?.Invoke(resultOp);
 347                            resultOp.SetAsCompleted(false);
 348                        }
 349                    }
 350                    else
 351                    {
 352                        OnFail?.Invoke(resultOp);
 353                        resultOp.SetAsCompleted(false);
 354                    }
 355                }
 356
 357                ongoingWebRequests.Remove(resultOp);
 358            };
 359
 472360            return resultOp;
 361        }
 362
 363
 364        public void Dispose()
 365        {
 1506366            foreach (var webRequest in ongoingWebRequests)
 120367                webRequest.Dispose();
 633368        }
 369    }
 370}

Methods/Properties

WebRequestController(DCL.IWebRequestFactory, DCL.IWebRequestAssetBundleFactory, DCL.IWebRequestTextureFactory, DCL.IWebRequestAudioFactory, DCL.IPostWebRequestFactory)
Initialize()
Create()
Initialize(DCL.IWebRequestFactory, DCL.IWebRequestAssetBundleFactory, DCL.IWebRequestTextureFactory, DCL.IWebRequestAudioFactory)
GetAsync()
PostAsync()
GetAssetBundleAsync()
GetAssetBundleAsync()
GetTextureAsync()
GetAudioClipAsync()
Get(System.String, UnityEngine.Networking.DownloadHandler, System.Action[IWebRequestAsyncOperation], System.Action[IWebRequestAsyncOperation], System.Int32, System.Int32, System.Boolean, System.Collections.Generic.Dictionary[String,String])
Post(System.String, System.String, UnityEngine.Networking.DownloadHandler, System.Action[IWebRequestAsyncOperation], System.Action[IWebRequestAsyncOperation], System.Int32, System.Int32, System.Boolean, System.Collections.Generic.Dictionary[String,String])
GetAssetBundle(System.String, System.Action[IWebRequestAsyncOperation], System.Action[IWebRequestAsyncOperation], System.Int32, System.Int32, System.Boolean)
GetAssetBundle(System.String, UnityEngine.Hash128, System.Action[IWebRequestAsyncOperation], System.Action[IWebRequestAsyncOperation], System.Int32, System.Int32, System.Boolean)
GetTexture(System.String, System.Action[IWebRequestAsyncOperation], System.Action[IWebRequestAsyncOperation], System.Int32, System.Int32, System.Boolean, System.Boolean, System.Collections.Generic.Dictionary[String,String])
GetAudioClip(System.String, UnityEngine.AudioType, System.Action[IWebRequestAsyncOperation], System.Action[IWebRequestAsyncOperation], System.Int32, System.Int32, System.Boolean)
SendWebRequest[T](T, System.String, UnityEngine.Networking.DownloadHandler, System.Action[IWebRequestAsyncOperation], System.Action[IWebRequestAsyncOperation], System.Int32, System.Int32, System.Boolean, System.Collections.Generic.Dictionary[String,String], DCL.WebRequestAsyncOperation)
Dispose()