| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using MainScripts.DCL.Helpers.SentryUtils; |
| | 4 | | using Sentry; |
| | 5 | | using System; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Networking; |
| | 10 | | using UnityEngine.Pool; |
| | 11 | | using Transaction = Sentry.Transaction; |
| | 12 | |
|
| | 13 | | namespace 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 | | { |
| 0 | 31 | | var postDataJson = JsonUtility.ToJson(postData); |
| 0 | 32 | | var url = GetUrl(endPoint, urlEncodedParams); |
| 0 | 33 | | var wr = webRequestController.Ref.Post(url, postDataJson, requestAttemps: attemptsNumber, timeout: timeout, |
| 0 | 34 | | var transaction = urlTransactionMonitor.Ref.TrackWebRequest(wr.asyncOp, endPointTemplate, data: postDataJson |
| | 35 | |
|
| 0 | 36 | | 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 | | { |
| 0 | 47 | | var url = GetUrl(endPoint, urlEncodedParams); |
| 0 | 48 | | var wr = webRequestController.Ref.Get(url, requestAttemps: attemptsNumber, timeout: timeout, disposeOnComple |
| 0 | 49 | | var transaction = urlTransactionMonitor.Ref.TrackWebRequest(wr.asyncOp, endPointTemplate, finishTransactionO |
| | 50 | |
|
| 0 | 51 | | 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 | | { |
| 0 | 76 | | var urlBuilder = GenericPool<StringBuilder>.Get(); |
| 0 | 77 | | urlBuilder.Clear(); |
| 0 | 78 | | urlBuilder.Append(GetLambdasUrl()); |
| 0 | 79 | | urlBuilder.Append('/'); |
| | 80 | |
|
| 0 | 81 | | var endPointSpan = endPoint.AsSpan(); |
| | 82 | |
|
| 0 | 83 | | if (endPoint.StartsWith('/') || endPoint.StartsWith('\\')) |
| 0 | 84 | | endPointSpan = endPointSpan[1..]; |
| | 85 | |
|
| 0 | 86 | | if (endPoint.EndsWith('/') || endPoint.EndsWith('\\') || endPoint.EndsWith('?')) |
| 0 | 87 | | endPointSpan = endPointSpan[..^1]; |
| | 88 | |
|
| 0 | 89 | | urlBuilder.Append(endPointSpan); |
| | 90 | |
|
| 0 | 91 | | if (urlEncodedParams.Length > 0) |
| | 92 | | { |
| 0 | 93 | | urlBuilder.Append('?'); |
| | 94 | |
|
| 0 | 95 | | for (var i = 0; i < urlEncodedParams.Length; i++) |
| | 96 | | { |
| 0 | 97 | | var param = urlEncodedParams[i]; |
| 0 | 98 | | urlBuilder.Append(param.paramName); |
| 0 | 99 | | urlBuilder.Append('='); |
| 0 | 100 | | urlBuilder.Append(param.paramValue); |
| | 101 | |
|
| 0 | 102 | | if (i < urlEncodedParams.Length - 1) |
| 0 | 103 | | urlBuilder.Append('&'); |
| | 104 | | } |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | var url = urlBuilder.ToString(); |
| 0 | 108 | | GenericPool<StringBuilder>.Release(urlBuilder); |
| 0 | 109 | | return url; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | internal static bool TryParseResponse<TResponse>(string endPoint, DisposableTransaction transaction, |
| | 113 | | string textResponse, out TResponse response) |
| | 114 | | { |
| | 115 | | try |
| | 116 | | { |
| 0 | 117 | | response = JsonUtility.FromJson<TResponse>(textResponse); |
| 0 | 118 | | return true; |
| | 119 | | } |
| 0 | 120 | | catch (Exception e) |
| | 121 | | { |
| 0 | 122 | | response = default; |
| 0 | 123 | | PrintError<TResponse>(endPoint, e.Message); |
| 0 | 124 | | transaction.SetStatus(SpanStatus.DataLoss); |
| 0 | 125 | | return false; |
| | 126 | | } |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | internal string GetLambdasUrl() |
| | 130 | | { |
| | 131 | | // TODO (Santi): This should use catalyst.lambdasUrl instead the hardcode string |
| 0 | 132 | | return "https://peer.decentraland.org/lambdas"; |
| | 133 | | } |
| | 134 | |
|
| | 135 | | private static void PrintError<TResponse>(string endPoint, string message) |
| | 136 | | { |
| 0 | 137 | | Debug.LogError($"Lambda {endPoint}, response {typeof(TResponse)}: {message}"); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | public void Initialize() |
| | 141 | | { |
| 493 | 142 | | catalyst = DCL.Environment.i.serviceLocator.Get<IServiceProviders>().catalyst; |
| 493 | 143 | | } |
| | 144 | |
|
| 493 | 145 | | public void Dispose() { } |
| | 146 | | } |
| | 147 | | } |