Skip to content

Commit

Permalink
wallet entry search minor modification MC-144 (#121)
Browse files Browse the repository at this point in the history
* wallet entry search minor modification MC-144

* downgrade using syntax statement to C# 7..3

* codestyle adjustment

* constructor call
  • Loading branch information
Antivortex authored Apr 13, 2023
1 parent a84ff69 commit 462b4a4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,72 +12,79 @@ namespace MirageSDK.WalletConnect.VersionShared.Utils
{
public static class WalletDownloadHelper
{
private static Dictionary<string, WalletEntry> _walletEntries = new Dictionary<string, WalletEntry>();
private static Dictionary<string, WalletEntry> _walletEntriesCache = new Dictionary<string, WalletEntry>();

public static async UniTask<WalletEntry> FindWalletEntry(Wallets wallet, bool invalidateCache = false)
public static async UniTask<WalletEntry> 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<Dictionary<string, WalletEntry>> FetchWalletList(bool downloadImages, bool invalidateCache = false)
public static async UniTask<WalletEntry> FindWalletEntry(Wallets wallet, bool invalidateCache = false)
{
var walletName = wallet.GetWalletName();
return await FindWalletEntryByName(walletName: walletName, invalidateCache: invalidateCache);
}

public static async UniTask<Dictionary<string, WalletEntry>> 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)
{
await entry.DownloadImages();
}
}
}
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<Dictionary<string, WalletEntry>>(json);
var supportedWallets = JsonConvert.DeserializeObject<Dictionary<string, WalletEntry>>(value: json);

if (!downloadImages)
{
return supportedWallets;
}

var filteredSupportedWallets = GetAllSupportedWallets(supportedWallets);
var filteredSupportedWallets = GetAllSupportedWallets(walletconnectSupportedWallets: supportedWallets);
foreach (var wallet in filteredSupportedWallets.Values)
{
await wallet.DownloadImages();
Expand All @@ -92,8 +99,8 @@ private static Dictionary<string, WalletEntry> 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);
}
}
}
72 changes: 39 additions & 33 deletions Assets/MirageSDK/Runtime/Utils/WebHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,36 @@ public static async UniTask<TResultType> SendGetRequest<TResultType>(
Dictionary<string, string> 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<TResultType>(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<TResultType>(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<TResultType> SendChangeRequest<TResultType>(
Expand Down Expand Up @@ -114,16 +116,18 @@ public static async UniTask<TResultType> SendPostRequestURLEncoded<TResultType>(
Dictionary<string, string> 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<TResultType>(json, answer);
return ParseJsonResponse<TResultType>(json, answer);
}
}

public static async UniTask SendPostRequestURLEncoded(
Expand All @@ -132,13 +136,15 @@ public static async UniTask SendPostRequestURLEncoded(
Dictionary<string, string> 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<TResultType>(string json,
Expand Down

0 comments on commit 462b4a4

Please sign in to comment.