diff --git a/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Utils/WalletDownloadHelper.cs b/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Utils/WalletDownloadHelper.cs index f3b204f6..90f7d4dd 100644 --- a/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Utils/WalletDownloadHelper.cs +++ b/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Utils/WalletDownloadHelper.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using Cysharp.Threading.Tasks; using MirageSDK.WalletConnect.VersionShared.Models.DeepLink; using MirageSDK.WalletConnect.VersionShared.Models.DeepLink.Helpers; -using Cysharp.Threading.Tasks; using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; @@ -12,32 +12,39 @@ namespace MirageSDK.WalletConnect.VersionShared.Utils { public static class WalletDownloadHelper { - private static Dictionary _walletEntries = new Dictionary(); + private static Dictionary _walletEntriesCache = new Dictionary(); - public static async UniTask FindWalletEntry(Wallets wallet, bool invalidateCache = false) + public static async UniTask FindWalletEntryByName(string walletName, bool invalidateCache = false) { - var supportedWallets = await FetchWalletList(downloadImages:false, invalidateCache:invalidateCache); - var walletName = wallet.GetWalletName(); + var supportedWallets = await FetchWalletList(downloadImages: false, invalidateCache: invalidateCache); var walletEntry = - supportedWallets.Values.FirstOrDefault(a => - string.Equals(a.name, walletName, StringComparison.InvariantCultureIgnoreCase)); + supportedWallets.Values.FirstOrDefault(predicate: a => + string.Equals(a: a.name, b: walletName, + comparisonType: StringComparison.InvariantCultureIgnoreCase)); return walletEntry; } - public static async UniTask> FetchWalletList(bool downloadImages, bool invalidateCache = false) + public static async UniTask FindWalletEntry(Wallets wallet, bool invalidateCache = false) + { + var walletName = wallet.GetWalletName(); + return await FindWalletEntryByName(walletName: walletName, invalidateCache: invalidateCache); + } + + public static async UniTask> FetchWalletList(bool downloadImages, + bool invalidateCache = false) { if (invalidateCache) { - _walletEntries = null; + _walletEntriesCache = null; } - - if (_walletEntries != null) + + if (_walletEntriesCache != null) { //if wallet already cached but it does not have images loaded then load them before returning if (downloadImages) { - foreach (var entry in _walletEntries.Values) + foreach (var entry in _walletEntriesCache.Values) { if (!entry.AllImagesLoaded) { @@ -45,39 +52,39 @@ public static async UniTask> FetchWalletList(boo } } } - - return _walletEntries; + + return _walletEntriesCache; } - - using (var webRequest = UnityWebRequest.Get("https://registry.walletconnect.org/data/wallets.json")) + + using (var webRequest = UnityWebRequest.Get(uri: "https://registry.walletconnect.org/data/wallets.json")) { // Request and wait for the desired page. await webRequest.SendWebRequest(); - #if UNITY_2020_2_OR_NEWER + #if UNITY_2020_2_OR_NEWER if (webRequest.result != UnityWebRequest.Result.Success) { - Debug.Log("Error Getting Wallet Info: " + webRequest.error); + Debug.Log(message: "Error Getting Wallet Info: " + webRequest.error); return null; } - #else + #else if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.Log("Error Getting Wallet Info: " + webRequest.error); return null; } - #endif + #endif var json = webRequest.downloadHandler.text; - var supportedWallets = JsonConvert.DeserializeObject>(json); + var supportedWallets = JsonConvert.DeserializeObject>(value: json); if (!downloadImages) { return supportedWallets; } - var filteredSupportedWallets = GetAllSupportedWallets(supportedWallets); + var filteredSupportedWallets = GetAllSupportedWallets(walletconnectSupportedWallets: supportedWallets); foreach (var wallet in filteredSupportedWallets.Values) { await wallet.DownloadImages(); @@ -92,8 +99,8 @@ private static Dictionary GetAllSupportedWallets( { var walletsSupportedBySDK = WalletNameHelper.GetSupportedWalletNames(); return walletconnectSupportedWallets - .Where(w => walletsSupportedBySDK.Contains(w.Value.name)) - .ToDictionary(i => i.Key, i => i.Value); + .Where(predicate: w => walletsSupportedBySDK.Contains(value: w.Value.name)) + .ToDictionary(keySelector: i => i.Key, elementSelector: i => i.Value); } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Runtime/Utils/WebHelper.cs b/Assets/MirageSDK/Runtime/Utils/WebHelper.cs index 405d3b64..79e4153a 100644 --- a/Assets/MirageSDK/Runtime/Utils/WebHelper.cs +++ b/Assets/MirageSDK/Runtime/Utils/WebHelper.cs @@ -49,34 +49,36 @@ public static async UniTask SendGetRequest( Dictionary headers = null ) { - using var webRequest = UnityWebRequest.Get(urlWithQuery); - if (headers != null) + using (var webRequest = UnityWebRequest.Get(urlWithQuery)) { - webRequest.AddHeaders(headers); - } + if (headers != null) + { + webRequest.AddHeaders(headers: headers); + } - webRequest.timeout = 5; + webRequest.timeout = 5; - var answer = await webRequest.SendWebRequest(); + var answer = await webRequest.SendWebRequest(); - var requestResult = answer.result; - var json = webRequest.downloadHandler.text; - if (requestResult == UnityWebRequest.Result.Success) - { - try - { - var result = JsonConvert.DeserializeObject(json); - return result; - } - catch (Exception e) + var requestResult = answer.result; + var json = webRequest.downloadHandler.text; + if (requestResult == UnityWebRequest.Result.Success) { - Debug.LogError($"Error while deserializing response: {e.Message}"); - throw; + try + { + var result = JsonConvert.DeserializeObject(json); + return result; + } + catch (Exception e) + { + Debug.LogError($"Error while deserializing response: {e.Message}"); + throw; + } } - } - Debug.LogError(webRequest.error); - throw new Exception(webRequest.error); + Debug.LogError(webRequest.error); + throw new Exception(webRequest.error); + } } private static async UniTask SendChangeRequest( @@ -114,16 +116,18 @@ public static async UniTask SendPostRequestURLEncoded( Dictionary headers = null ) { - using var webRequest = UnityWebRequest.Post(url, payload); - if (headers != null) + using (var webRequest = UnityWebRequest.Post(url, payload)) { - webRequest.AddHeaders(headers); - } + if (headers != null) + { + webRequest.AddHeaders(headers); + } - var answer = await webRequest.SendWebRequest(); - var json = webRequest.downloadHandler.text; + var answer = await webRequest.SendWebRequest(); + var json = webRequest.downloadHandler.text; - return ParseJsonResponse(json, answer); + return ParseJsonResponse(json, answer); + } } public static async UniTask SendPostRequestURLEncoded( @@ -132,13 +136,15 @@ public static async UniTask SendPostRequestURLEncoded( Dictionary headers = null ) { - using var webRequest = UnityWebRequest.Post(url, payload); - if (headers != null) + using (var webRequest = UnityWebRequest.Post(url, payload)) { - webRequest.AddHeaders(headers); - } + if (headers != null) + { + webRequest.AddHeaders(headers); + } - await webRequest.SendWebRequest(); + await webRequest.SendWebRequest(); + } } private static TResultType ParseJsonResponse(string json,