< Summary

Class:DCLServices.Lambdas.LambdasService
Assembly:LambdasService
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/Lambdas/LambdasService.cs
Covered lines:3
Uncovered lines:42
Coverable lines:45
Total lines:147
Line coverage:6.6% (3 of 45)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Post[TResponse, TBody](...)0%2100%
Get[TResponse](...)0%2100%
GetUrl(...)0%90900%
TryParseResponse[TResponse](...)0%2100%
GetLambdasUrl()0%2100%
PrintError[TResponse](...)0%2100%
Initialize()0%110100%
Dispose()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/Lambdas/LambdasService.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using MainScripts.DCL.Helpers.SentryUtils;
 4using Sentry;
 5using System;
 6using System.Text;
 7using System.Threading;
 8using UnityEngine;
 9using UnityEngine.Networking;
 10using UnityEngine.Pool;
 11using Transaction = Sentry.Transaction;
 12
 13namespace DCLServices.Lambdas
 14{
 15    public class LambdasService : ILambdasService
 16    {
 17        private ICatalyst catalyst;
 18
 19        private Service<IWebRequestController> webRequestController;
 20        private Service<IWebRequestMonitor> urlTransactionMonitor;
 21
 22        public UniTask<(TResponse response, bool success)> Post<TResponse, TBody>(
 23            string endPointTemplate,
 24            string endPoint,
 25            TBody postData,
 26            int timeout = ILambdasService.DEFAULT_TIMEOUT,
 27            int attemptsNumber = ILambdasService.DEFAULT_ATTEMPTS_NUMBER,
 28            CancellationToken cancellationToken = default,
 29            params (string paramName, string paramValue)[] urlEncodedParams)
 30        {
 031            var postDataJson = JsonUtility.ToJson(postData);
 032            var url = GetUrl(endPoint, urlEncodedParams);
 033            var wr = webRequestController.Ref.Post(url, postDataJson, requestAttemps: attemptsNumber, timeout: timeout, 
 034            var transaction = urlTransactionMonitor.Ref.TrackWebRequest(wr.asyncOp, endPointTemplate, data: postDataJson
 35
 036            return SendRequestAsync<TResponse>(wr, cancellationToken, endPoint, transaction, urlEncodedParams);
 37        }
 38
 39        public UniTask<(TResponse response, bool success)> Get<TResponse>(
 40            string endPointTemplate,
 41            string endPoint,
 42            int timeout = ILambdasService.DEFAULT_TIMEOUT,
 43            int attemptsNumber = ILambdasService.DEFAULT_ATTEMPTS_NUMBER,
 44            CancellationToken cancellationToken = default,
 45            params (string paramName, string paramValue)[] urlEncodedParams)
 46        {
 047            var url = GetUrl(endPoint, urlEncodedParams);
 048            var wr = webRequestController.Ref.Get(url, requestAttemps: attemptsNumber, timeout: timeout, disposeOnComple
 049            var transaction = urlTransactionMonitor.Ref.TrackWebRequest(wr.asyncOp, endPointTemplate, finishTransactionO
 50
 051            return SendRequestAsync<TResponse>(wr, cancellationToken, endPoint, transaction, urlEncodedParams);
 52        }
 53
 54        private async UniTask<(TResponse response, bool success)> SendRequestAsync<TResponse>(
 55            IWebRequestAsyncOperation webRequestAsyncOperation,
 56            CancellationToken cancellationToken,
 57            string endPoint,
 58            DisposableTransaction transaction,
 59            (string paramName, string paramValue)[] urlEncodedParams)
 60        {
 61            using var disposable = transaction;
 62            await webRequestAsyncOperation.WithCancellation(cancellationToken);
 63
 64            if (!webRequestAsyncOperation.isSucceeded)
 65                return (default, false);
 66
 67            string textResponse = webRequestAsyncOperation.webRequest.downloadHandler.text;
 68            webRequestAsyncOperation.Dispose();
 69
 70            var res = !TryParseResponse(endPoint, transaction, textResponse, out TResponse response) ? (default, false) 
 71            return res;
 72        }
 73
 74        internal string GetUrl(string endPoint, params (string paramName, string paramValue)[] urlEncodedParams)
 75        {
 076            var urlBuilder = GenericPool<StringBuilder>.Get();
 077            urlBuilder.Clear();
 078            urlBuilder.Append(GetLambdasUrl());
 079            urlBuilder.Append('/');
 80
 081            var endPointSpan = endPoint.AsSpan();
 82
 083            if (endPoint.StartsWith('/') || endPoint.StartsWith('\\'))
 084                endPointSpan = endPointSpan[1..];
 85
 086            if (endPoint.EndsWith('/') || endPoint.EndsWith('\\') || endPoint.EndsWith('?'))
 087                endPointSpan = endPointSpan[..^1];
 88
 089            urlBuilder.Append(endPointSpan);
 90
 091            if (urlEncodedParams.Length > 0)
 92            {
 093                urlBuilder.Append('?');
 94
 095                for (var i = 0; i < urlEncodedParams.Length; i++)
 96                {
 097                    var param = urlEncodedParams[i];
 098                    urlBuilder.Append(param.paramName);
 099                    urlBuilder.Append('=');
 0100                    urlBuilder.Append(param.paramValue);
 101
 0102                    if (i < urlEncodedParams.Length - 1)
 0103                        urlBuilder.Append('&');
 104                }
 105            }
 106
 0107            var url = urlBuilder.ToString();
 0108            GenericPool<StringBuilder>.Release(urlBuilder);
 0109            return url;
 110        }
 111
 112        internal static bool TryParseResponse<TResponse>(string endPoint, DisposableTransaction transaction,
 113            string textResponse, out TResponse response)
 114        {
 115            try
 116            {
 0117                response = JsonUtility.FromJson<TResponse>(textResponse);
 0118                return true;
 119            }
 0120            catch (Exception e)
 121            {
 0122                response = default;
 0123                PrintError<TResponse>(endPoint, e.Message);
 0124                transaction.SetStatus(SpanStatus.DataLoss);
 0125                return false;
 126            }
 0127        }
 128
 129        internal string GetLambdasUrl()
 130        {
 131            // TODO (Santi): This should use catalyst.lambdasUrl instead the hardcode string
 0132            return "https://peer.decentraland.org/lambdas";
 133        }
 134
 135        private static void PrintError<TResponse>(string endPoint, string message)
 136        {
 0137            Debug.LogError($"Lambda {endPoint}, response {typeof(TResponse)}: {message}");
 0138        }
 139
 140        public void Initialize()
 141        {
 493142            catalyst = DCL.Environment.i.serviceLocator.Get<IServiceProviders>().catalyst;
 493143        }
 144
 493145        public void Dispose() { }
 146    }
 147}